Jump to content

Displaying Grouped Data Correctly


dbnorton

Recommended Posts

Ok I have some data that I would like to have grouped by date. There are dates that have multiple entries for the same date.  I would like to have that data listed under one date. I would like it to look like this

 

Date

        Location    Weather

        Location    Weather

Date

        Location      Weather

 

Here is the code I have so far and it lists the data by date but only the data for first date in the table.  If there is data listed for the same date but different location it will not list that data.  What am I missing.  Any help would be great.

!

<?php

// Connect to server and select database.

include ("opendb.php");

$tbl_name="dbnwaterfowl";

 

$sql="SELECT huntDate, huntLocation, huntWeather, waterfowlSpecies, waterfowlNumberKilled, waterfowlBandedNumber, DATE_FORMAT(huntDate,'%m %d, %Y') FROM dbnwaterfowl GROUP BY `huntDate` ORDER BY `huntDate` ASC";

$result=mysql_query($sql);

?>

<td>

  <p> </p>

  <table width="100%" border="2" cellpadding="1" cellspacing="0" bordercolor="#FA6103" bgcolor="#FFFFFF">

<tr>

<td colspan="7"><div align="center">

  <p><img src="images/hl-logo.jpg" /></p>

  <p align="center"><strong><a href="login.php">Login</a> | <a href="insert.php">Log Hunt</a> | <a href="logout.php">Logout</a></strong></p>

</div></td>

</tr>

 

<tr>

<td align="center" bordercolor="#FA6103" ><span class="style4">Hunting Date</span></td>

<td align="center" bordercolor="#FA6103" ><span class="style4">Hunting Location</span></td>

<td align="center" bordercolor="#FA6103"><span class="style4">Weather</span></td>

<td align="center" bordercolor="#FA6103"><span class="style4">Waterfowl Species</span></td>

<td align="center" bordercolor="#FA6103"><span class="style4">Number Killed</span></td>

<td align="center" bordercolor="#FA6103"><span class="style4">Number of Bands</span></td>

<td align="center"> </td>

</tr>

<?php

while($rows=mysql_fetch_array($result)){

?>

<tr>

<td><? echo $rows['huntDate']; ?></td>

<td><? echo $rows['huntLocation']; ?></td>

<td><? echo $rows['huntWeather']; ?></td>

<td><? echo $rows['waterfowlSpecies']; ?></td>

<td><? echo $rows['waterfowlNumberKilled']; ?></td>

<td><? echo $rows['waterfowlBandedNumber']; ?></td>

<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>

</tr>

<?php

}

?>

</table>

</td>

</tr>

</table>

<?php

mysql_close();

?>

 

Link to comment
https://forums.phpfreaks.com/topic/212291-displaying-grouped-data-correctly/
Share on other sites

GROUP BY won't do it.  You probably want something like this:

 

$sql = "SELECT huntDate, huntLocation, huntWeather, waterfowlSpecies, waterfowlNumberKilled, waterfowlBandedNumber, DATE_FORMAT(huntDate,'%m %d, %Y')
	FROM dbnwaterfowl
	ORDER BY huntDate ASC";

$result = mysql_query($sql);

$date = '';

while($rows = mysql_fetch_array($result)) {
if($rows['huntDate'] != $date) {
	$date = $rows['huntDate'];
	echo $date;
}
// echo other fields
}

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.