madjack87 Posted September 29, 2009 Share Posted September 29, 2009 Here is the table which I am working with. 5 Its All About Us 2009-10-31 Racine Rage will be here! 2 Its All About Us 2009-10-31 Halloween Party! 3 Its All About Us 2009-10-07 Poker Starts a 7pm FREE to Play 4 Its All About Us 2009-12-31 New Years 6 Players Park 2009-10-31 Octoberfest Here is the PHP code I am using. <?php $result = mysql_query("SELECT * FROM events ORDER BY date"); $curr_date = ''; $curr_bar = ''; while($row = mysql_fetch_array($result)){ if($row['date'] != $curr_date){ echo '<h3>' . $row['date'] . '</h3>'; echo '<h4>' . $row['bar'] . '</h4>'; $curr_date = $row['date']; } echo '<p>' . $row['event'] . '</p>'; } ?> Here is the result of the code <h3> 2009-10-07 <h4> Its All About Us <p> Poker Starts a 7pm FREE to Play <h3> 2009-10-31 <h4> Its All About Us <p> Racine Rage will be here! <p> Halloween Party! <p> Octoberfest <h3> 2009-12-31 <h4> Its All About Us <p> New Years Where I highlighted in Red is what I need to fix. I want it to list "Players Park" the bar that corresponds with that event to be listed in an <h4> tag above it. Link to comment https://forums.phpfreaks.com/topic/175962-mysql_query-and-displaying-the-results-help/ Share on other sites More sharing options...
lemmin Posted September 29, 2009 Share Posted September 29, 2009 Your code tells it to skip over that if the date is the same as the previous one. I think what you want to do is move the "<h4>" line outside of the if statement: while($row = mysql_fetch_array($result)){ if($row['date'] != $curr_date){ echo '<h3>' . $row['date'] . '</h3>'; $curr_date = $row['date']; } echo '<h4>' . $row['bar'] . '</h4>'; echo '<p>' . $row['event'] . '</p>'; } Link to comment https://forums.phpfreaks.com/topic/175962-mysql_query-and-displaying-the-results-help/#findComment-927198 Share on other sites More sharing options...
madjack87 Posted October 2, 2009 Author Share Posted October 2, 2009 When using the code you supplied I get this. <h3> 2009-10-31 <h4> Its All About Us Racine Rage will be here! <h4> Its All About Us Halloween Party! <h4> Players Park What I want is it to list the bar name only once per date. And you have it listing it for every event on that date. Is there a different way to code this? Link to comment https://forums.phpfreaks.com/topic/175962-mysql_query-and-displaying-the-results-help/#findComment-929134 Share on other sites More sharing options...
cags Posted October 2, 2009 Share Posted October 2, 2009 This code looks familiar... Is this what your after? <?php $result = mysql_query("SELECT * FROM events ORDER BY date"); $curr_date = ''; $curr_bar = ''; while($row = mysql_fetch_array($result)){ if($row['date'] != $curr_date){ echo '<h3>' . $row['date'] . '</h3>'; $curr_date = $row['date']; } if($row['bar'] != $curr_bar) { echo '<h4>' . $row['bar'] . '</h4>'; $curr_bar = $row['bar']; } echo '<p>' . $row['event'] . '</p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/175962-mysql_query-and-displaying-the-results-help/#findComment-929140 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.