Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2005, 12:16 PM
Registered User
 
Join Date: Apr 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I display search results...

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

 
Old April 25th, 2005, 04:59 PM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

try this:

Select city, count(*) as City_n FROM tblCities GROUP BY city;





----------------
Never bother to learn something not knowing which does not do you any harm, and never neglect to learn something whose negligence will increase your ignorance - Imam Jafar Sadeq





Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter double search results ABplace Beginning VB 6 3 June 28th, 2007 08:11 AM
How do I display search results... aspiretech9 PHP How-To 1 April 26th, 2005 10:12 AM
How do I display search results... aspiretech9 PHP Databases 1 April 25th, 2005 12:11 PM
How display search results ? URGENT arulkumar Classic ASP Databases 2 March 4th, 2005 10:26 AM
Customize Search Results bmains ASP.NET 1.0 and 1.1 Professional 4 January 15th, 2004 08:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.