467,880 Members | 1,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,880 developers. It's quick & easy.

Having similar records only display once with a mysql database using PHP

Hey everyone!

I am completley new and I will start off by saying that I am not a programmer - figuring out this search took so long! I am debugging now and would appreciate any help :)

Anyways, I am creating a website where I have a database of pet listings in california. Each record lists the Type of Listing (eg. dog grooming or vet) the the Area it is in (so state, county etc.), a Name, Description, and Address.

I created a search so there are three pages. The first one has a pulldown menu where the user selects the type of listing they are looking for. Next, they select the area they wish to view, and then the records are all displayed that match those two critera (aka all dog grooming in san fran.)

Okay so here is my issue: There are about 45 Dog Grooming records, 240 Vets etc. And they ALL show up in the pulldown menus. Same with the areas.

is there a clause I can add to my code so once dog grooming is listed, it is no longer shown, yet still selects all of the records with dog grooming in it?

Please tell me if this does not make sense. And here is my code (please don't laugh, I am sure it is very messy! I am a designer :) not a coder ha. THANKS SO MUCH!


SEARCH.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $username="forpet";
  3. $password="dogrnnr2";
  4. $database="forpet";
  5.  
  6. mysql_connect(localhost,$username,$password);
  7. @mysql_select_db($database) or die("Unable to select database");
  8.  
  9. ?>
  10.  
  11. <form method="POST" action="search2.php">
  12.  
  13. <?php $sql = mysql_query("SELECT type FROM listings ORDER BY type ASC"); while ($row = mysql_fetch_array($sql)) { ?>
  14.  
  15. Type of Listing:
  16. <select name="type">
  17. <option name="<?php echo $row['type']; ?>" id="<?php echo $row['type']; ?>"> <?php echo $row['type']; ?> </option> <?php } ?>
  18. </select>
  19. <input type="submit" value="Continue Search"><br><br>
  20. </form>
SEARCH2.PHP
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $username="forpet";
  3. $password="dogrnnr2";
  4. $database="forpet";
  5.  
  6. mysql_connect(localhost,$username,$password);
  7. @mysql_select_db($database) or die("Unable to select database");
  8.  
  9. ?>
  10.  
  11. <form method="POST" action="results.php">
  12.  
  13. <?php
  14.  
  15. $sql = mysql_query("SELECT * FROM listings WHERE type LIKE '$type'");
  16. while ($row = mysql_fetch_array($sql)) { ?>
  17.  
  18. Type of Listing:
  19. <select name="type">
  20. <option name="<?php echo $type; ?>" id="<?php echo $type; ?>"> <?php echo $type; ?> </option> <?php } ?>
  21. </select>
  22. <br>
  23. <br>
  24.  
  25. <?php
  26.  
  27. $sql = mysql_query("SELECT * FROM listings WHERE type LIKE '$type'");
  28. while ($row = mysql_fetch_array($sql)) { ?>
  29.  
  30. Area/Location:
  31. <select name="area">
  32. <option name="<?php echo $row['area']; ?>" id="<?php echo $row['area']; ?>"> <?php echo $row['area']; ?> </option> <?php } ?>
  33. </select>
  34. <input name="" type="submit" value="Display Listings">
  35.  
  36. </form>
RESULTS.PHP
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $username="forpet";
  3. $password="dogrnnr2";
  4. $database="forpet";
  5.  
  6. mysql_connect(localhost,$username,$password);
  7. @mysql_select_db($database) or die("Unable to select database");
  8.  
  9. $query="SELECT * FROM listings WHERE area LIKE '$area' AND type LIKE '$type'";
  10. $result=mysql_query($query);
  11.  
  12. $num=mysql_numrows($result);
  13.  
  14. mysql_close();
  15.  
  16. echo "<b><center>Results for $type in $area</center></b><br><br>";
  17.  
  18. $i=0;
  19. while ($i < $num) {
  20.  
  21. $type=mysql_result($result,$i,"type");
  22. $area=mysql_result($result,$i,"area");
  23. $name=mysql_result($result,$i,"name");
  24. $content=mysql_result($result,$i,"content");
  25. $address=mysql_result($result,$i,"address");
  26.  
  27. echo "<b>$name</b><br>$content<br>$address<br><hr><br>";
  28.  
  29. $i++;
  30.  
  31. }
  32.  
  33. ?>
THANK YOU SO MUCH!
-Stephanie


For php, CODE tags should be like this.
[code=php]
Not Just
[code]

Anyway thanks for using [code] Tags.
Jul 6 '07 #1
  • viewed: 2531
Share:
3 Replies
okay i figured this out - all i needed was a DISTINCT clause. :)
Jul 7 '07 #2
ak1dnar
Expert 1GB
okay i figured this out - all i needed was a DISTINCT clause. :)
Hi Stephanie,
Glad to hear that you've figured it out your self.Welcome to TSDN anytime.
Jul 8 '07 #3
banning
okay i figured this out - all i needed was a DISTINCT clause. :)

LOL that makes me so mad, i hate it when its a stupid easy answer to stuff like this haha... i had the exact same problem and i was just thinking about how retarded it was that the solution has to be simple.

its a good thing i ran across your question on here i hate having to word my problems out for people on message boards n stuff lol

thanks :P
Aug 13 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by mikec87 | last post: by
7 posts views Thread by iahamed via WebmasterKB.com | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.