Jump to content

Help building the proper loop


86Stang

Recommended Posts

I've got the following two tables:

 

EVENTS
-------------------
event_id
title
info


DATES
-------------------
id
event_date
event_id

 

and I'm pulling the data I need via this query:

 

$qry = "SELECT id,event_date,title,events.event_id
	FROM events
	INNER JOIN dates
	ON dates.event_id = events.event_id		
	ORDER BY event_date asc";

 

Using a WHILE loop off of that query, I can get that query to generate something akin to:

 

2010-02-07		Title 1
2010-02-09		Title 2
2010-02-09		Title 3
2010-02-09		Title 4
2010-02-11		Title 5
2010-02-12		Title 6

 

but what I need is a loop that will show each date only once with the info below it, like:

 

2010-02-07
Title 1
2010-02-09
Title 2
Title 3
Title 4
2010-02-11
Title 5
2010-02-12
Title 6	

 

I'm sure this is a quick kill for a lot of you out there so I'm hoping a kind soul can help me out.

Link to comment
Share on other sites

$output = '';
$current_date = '';

while($record = mysql_fetch_assoc($result))
{
    //Display date ONLY IF NEW
    if ($current_date != $record['event_date'])
    {
        $current_date = $record['event_date'];
        $output .= "<b>{$current_date}</b><br>\n";
    }
    $output .= "{$record['title']}<br>\n";
}

echo $output;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.