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
https://forums.phpfreaks.com/topic/191551-help-building-the-proper-loop/
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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.