Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 25th, 2005, 12:02 PM
Registered User
 
Join Date: Apr 2005
Location: , , .
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&nbsp;$bizname#39;s&nbsp;Web&nbsp;Site '>$bizname</a></td>\n
<td>($phonearea)&nbsp;$phone</td>\n
<td>$city,&nbsp;$state</td>\n
</tr>\n";
echo "<tr><td colspan='4'></td></tr>\n";

}

echo "</table>\n";

?>

</body>
</html>

</END HTML-PHP CODE>

Reply With Quote
  #2 (permalink)  
Old April 26th, 2005, 10:12 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

First, help us out with a little code formatting... http://p2p.wrox.com/topic.asp?TOPIC_ID=11967

You haven't explained nearly enough of what you're doing to obtain an answer.

What's going on here?
Code:
$state = "$state";
$city = "$city";
What are you accomplishing by assigning $state to itself? $state = "$state"; is the same as saying $state = $state;, the statement is meaningless, all you're saying is the variable is the variable.

$query = "SELECT * FROM tblCities WHERE city='$city' ORDER BY city";
$result = mysql_query($query)

Ok, you're creating a string for the query, but that query is only used once. So why not eliminate the creation of an extra variable all together.
$result = mysql_query("SELECT * FROM tblCities WHERE city='$city' ORDER BY city");

It's difficult to tell from your logic how you are obtaining the results. What is the variable $city. Is it an integer? Is it a string? What about the table "tblCities", what are the different fields? How is it structured?

What I'm getting at is, you haven't given a comprehensive overview of the problem you're facing, and on top of that, you've left us sloppy code to light the way.

In further review of the code, it is also obvious that:
Code:
$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&nbsp;$bizname#39;s&nbsp;Web&nbsp;Site'>$bizname</a></td>\n
<td>($phonearea)&nbsp;$phone</td>\n
<td>$city,&nbsp;$state</td>\n
</tr>\n";
echo "<tr><td colspan='4'></td></tr>\n";
Isn't the source code that you're using to obtain:
Code:
 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
Have a read of: http://www.catb.org/~esr/faqs/smart-questions.html

If you still can't find the answer. Reformulate your question and ask again.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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 Beginning PHP 1 April 25th, 2005 04:59 PM
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



All times are GMT -4. The time now is 10:49 PM.


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