Jump to content

mysql_query and displaying the results help


madjack87

Recommended Posts

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.

 

 

 

 

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>';
}

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?

 

         

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>';
}
?>

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.