0

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:

enter image description here

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>
2
  • 2
    Of course, it's get duplicated, cause you're printing it inside a loop. Commented Mar 20, 2012 at 21:03
  • Okay, but when I take out the venue name and venue type outside the loop, it doesn't show anything for those two variables. Commented Mar 20, 2012 at 21:05

2 Answers 2

2

For grouping I commonly use the following pattern:

  • Create some variable to track previous values
  • If current value is the same, do nothing
  • Otherwise update tracking variable and echo new grouping header.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey kirilloid, thanks for your answer! However, isn't there anything simpler to just produce a list of these events without affecting the rest of the page? I want to put a whole heap of html (and mysql requested code) before and after the list.
Not sure, what "without affecting the rest of the page" means. Could you provide an example (better not in comments)?
Thanks kirilloid! I never thought about using two queries, that was what I needed :)
1

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

2 Comments

Hi Zork, I don't exactly understand what you mean by the first example. Sorry, I'm quite new to this.
Aha, okay thanks. I tried putting the things I didn't want looped outside the loop function, but in that case no mysql data was retrieved. I updated my question if it's possible to do the echo like I showed above :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.