I have a Web site that I am building with PHP pages that are going to retrieve information from a MySQL Database. I want to be able to retrieve data from the "city" field and "count" the total number of rows "$city" is in, while displaying only one instance of the name of the "$city". I want the number of rows displayed in parentheses next to the city. In other words, I want the results to look like the following example where the data will be displayed in a 3-column Table formatted using HTML. What I have so far is at the bottom of this post. Please use it for reference. Upon reply, please be specific as I am a beginner! I can tell you more if you need me to. Thanks! To test the functionality yourself, just go to
http://www.businessdirectorylistings.com/tennessee.php. You will see for yourself what kind of results I am currently getting.
EXAMPLE > THIS IS WHAT I WANT THE RESULTS TO DISPLAY:
Your Search Found The Following Results...
Chicago (3) Houston (2) Nashville (5)
Panama City (4) Phoenix (3) Portland (2)
NOTE: The cities will have hyperlinks that link to a page of search results for that city.
EXAMPLE OF WHAT THE RESULTS CURRENTLY LOOK LIKE WITH THE CODE I HAVE:
1. Chicago
2. Chicago
3. Chicago
4. Houston
5. Houston
6. Nashville
7. Nashville
8. Nashville
9. Nashville
10. Nashville
11. Panama City
12. Panama City
13. Panama City
14. Panama City
15. Phoenix
16. Phoenix
17. Phoenix
18. Portland
19. Portland
<BEGIN HTML-PHP CODE>
<html>
<head>
<title>Get My Query</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
include("connectme.inc");
$connection = mysql_connect($host,$user,$password)
or die ("Couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$state = "$state";
$city = "$city";
$query = "SELECT * FROM tblCities WHERE city='$city' ORDER BY city";
$result = mysql_query($query)
or die ("Couldn't execute query");
$nrows = mysql_num_rows($result);
/* Display results in a table */
echo "<h1>Found The Following Results...</h1>";
echo "<table cellspacing='8'>";
echo "<tr><td colspan='4'></td></tr>";
for ($i=0;$i<$nrows;$i++)
{
$n = $i + 1; /* Add 1 so that records won't start displaying at 0 */
$row = mysql_fetch_array($result);
extract($row);
echo "<tr>\n
<td>$n.</td>\n
<td><a href='http://www.businessdirectorylistings.com/listingresults.php?city=$city&state=$state' title='Visit $bizname#39;s Web Site '>$bizname</a></td>\n
<td>($phonearea) $phone</td>\n
<td>$city, $state</td>\n
</tr>\n";
echo "<tr><td colspan='4'></td></tr>\n";
}
echo "</table>\n";
?>
</body>
</html>
</END HTML-PHP CODE>