Jump to content

Help With Displaying Results


JSHINER

Recommended Posts

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

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";
?>

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";
?>

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.