Jump to content


Mysql Help


2 replies to this topic

#1 Vanquish

    Young Padawan

  • Members
  • Pip
  • 5 posts

Posted 26 May 2007 - 09:48 PM

im making a search for my sisters site and ive managed to get the citys to populate in an dropdown list but what im trying to do is only show 1 selection per city example

lets say i have 100 members and 50 are from chicago and 10 are from boston and so on how would i get the populated list to only show chicago once and boston once instead of 50 chicago and 10 boston

heres the code im using

<form method="post" action="index.php?id=local">
Select City
<select style="width:10%" name="location">
<?php
include("config.php");

$r = mysql_query("select * from users");

while($row = mysql_fetch_assoc($r))
{
echo "<option value='{$row['location']}'>{$row['location']}</option>";
}

?>
</select>
<input type="Submit" name="Submit" value="Submit">
</form>

and the search file

<?
include("config.php");

$search=$_POST["location"];

$result = mysql_query("SELECT * FROM users WHERE location LIKE '%$search%'");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //modify these to match your mysql table columns
  
   $name=$r["username"];
   $locate=$r["location"];

   echo "$name <br> $locate<br /><br />";
}
?>

Thanks to anyone who can help

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 27 May 2007 - 01:43 AM

$r = mysql_query("SELECT DISTINCT `location` FROM `users`");

while($row = mysql_fetch_assoc($r)){
echo '<option value="'.$row['location'].'">'.$row['location'].'</option>';
}
Should work, if not, i'm sure i can think of something a little more complicated.

p.s. For demonslay's sake, i do make frequent parse errors :(

Edited by rc69, 27 May 2007 - 01:44 AM.


#3 Vanquish

    Young Padawan

  • Members
  • Pip
  • 5 posts

Posted 27 May 2007 - 10:06 AM

That worked perfectly thank you very much!!!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users