JSHINER Posted March 5, 2007 Share Posted March 5, 2007 I have a database with information in it... 00001, 00002, 00002, 00003, etc. I currently display it as: foreach ($page['zips'] as $z) { echo '<li>', $z['zips'] , $z['date_and_time'],'</li>'; } How can I make it so IF there are two of the same zip it would print "00002 (3)"... the 3 meaning there are 3 entries containing that zip code. and IF only one - just display 00001. Link to comment https://forums.phpfreaks.com/topic/41307-help-with-displaying-results/ Share on other sites More sharing options...
skali Posted March 5, 2007 Share Posted March 5, 2007 query = 'select count(your_information_field) from your_table group by zips'; this query will give you count of the field you are trying to count group by zip code. Link to comment https://forums.phpfreaks.com/topic/41307-help-with-displaying-results/#findComment-200159 Share on other sites More sharing options...
boo_lolly Posted March 5, 2007 Share Posted March 5, 2007 something like this: <?php $sql = "SELECT * FROM your_table ORDER BY zips"; $query = mysql_query($sql); $i = 0; $current_zip = "" echo "<ul>\n"; while($row = mysql_fetch_array($query)){ if($current_zip != $row['zips']){ echo "<li>". $current_zip ." (". $i .")</li>\n"; $current_zip = $row['zips']; $i = 0; }else{ $current_zip = $row['zips']; $i++; } echo "</ul>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/41307-help-with-displaying-results/#findComment-200172 Share on other sites More sharing options...
boo_lolly Posted March 5, 2007 Share Posted March 5, 2007 sorry, forgot to add another conditional statement... this should work properly. <?php $sql = "SELECT * FROM your_table ORDER BY zips"; $query = mysql_query($sql); $i = 0; $current_zip = "" echo "<ul>\n"; while($row = mysql_fetch_array($query)){ if($current_zip != $row['zips']){ if($current_zip != ""){ echo "<li>". $current_zip ." (". $i .")</li>\n"; $current_zip = $row['zips']; $i = 0; } }else{ $current_zip = $row['zips']; $i++; } echo "</ul>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/41307-help-with-displaying-results/#findComment-200230 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.