Reputation: 7795
I'm trying to display a list of all events for a particular venue depending on the file name (ex: page.php?id=5
),
However the basic information, in this case Venue name and Venue Type get's duplicated with each event.
Here's how it looks:
And here's the code:
$id = (int) $_GET['id'];
$data = mysql_query("
SELECT
venues.*,
venue_types.TYPE_NAME,
events.EVENT_NAME,
events.EVENT_DESC
FROM
venues
INNER JOIN venue_types
ON venues.VENUE_TYPE = venue_types.ID
INNER JOIN events
ON events.VENUE_LOCATION = venues.ID
WHERE
events.VENUE_LOCATION = venues.ID
AND
venues.id = ".$id) or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Print "Venue name:" . $info['VENUE_NAME'] . "<BR>";
Print "Venue Type:" . $info['TYPE_NAME'] . "<BR><BR>";
echo "Event name:" . $info['EVENT_NAME'] . "<BR>";
echo "Event description:" . $info['EVENT_DESC'] . "<BR>";
Thanks for reading!
Edit: Info for kirilloid
What I meant that the list will be in the middle of the page, which will be surrounded by other content which might and might not be retrieved from MySQL that I do not want to duplicate. What I meant was that if there was something simpler where the code could look like:
echo "<html> la la la
<Body>
la la la MYSQL_ROW la la la
la la la
stuff I don't want looped";
echo "
------------------------
LOOPED LIST
------------------------
";
echo "
more stuff I don't want looped
la la la MYSQL_ROW_ALSO la la la
</body></html>
Upvotes: 0
Views: 2914
Reputation: 14304
For grouping I commonly use the following pattern:
BTW, I have found a bug on SO. Code is not formatted as code just after the list w/o such line.
$last_venue_name = null;
while (...) {
if ($last_venue_name != $info['VENUE_NAME']) {
$last_venue_name = $info['VENUE_NAME'];
Print "Venue name:" . $info['VENUE_NAME'] . "<BR>";
Print "Venue Type:" . $info['TYPE_NAME'] . "<BR><BR>";
}
Fetch all data into 2-dim array, i.e. like
$infos = array(
mysql_fetch_array($data),
mysql_fetch_array($data)
)
out stuff you don't want to be repeated:
echo $infos[0]['VENUE_NAME']
out loop stuff:
foreach($infos as $info) {
echo $info['EVENT_NAME']
And repeat again.
You'll need to check: if (!empty($infos))
before reading 0
element from array.
If I'd know, that there will be only one distinct VENUE_NAME
across all records, I'd do it with 2 separate SQL
queries.
Upvotes: 2
Reputation: 1109
you have to fetch the data the first time to output the Venue name and Venue Type and then put the rest in a while loop.
Alternatively put an if() around the venue name and type and use a boolean to ensure it only outputs once.
Editing to further explain (comment is too small!):
No problem, to put it simply, the query returns a set of data, which you are looping through one at a time. You may only call the data AFTER a fetch array command is issued. In your case, you call the data, which gives you a row according to your query, and then you call the data for your print. For each row that exists in the return set of the query, it runs through the loop.
Kirilloid gives a good example, and if you want i can put together one as well. It is pretty much the simplest way to do it. I actually shouldn't have mentioned that first example as in the long run its more confusing that helpful, so instead just ignore it! :D
Upvotes: 1