0

Hi

If anyone could help with below code as I am new to php

<?php
$result = mysql_query("SELECT * FROM hotels ORDER BY city");

while($row = mysql_fetch_array($result))
  {
  echo $row['city'] . " " ;
  echo "<br />";
  }
?>

above code list all cities as below
newyork
newyork
newyork
newyork
newyork
newyork
Washington
Washington
Washington
Washington
Washington
Washington
Seattle
Seattle
Seattle
Seattle
Seattle
Kansas
Kansas
Kansas
Kansas
Kansas
Kansas
Kansas
.... continue

but i want to list like below and when a city link is clicked, all the hotels in that city will display on a city Page using the appropriate template

Newyork
washington
seattle
kansas ....

any help much appreciate

Thanks

Edited by nav33n: Please wrap your code in [code] tags for easy readability.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by wrivera
0

You should avoid using SELECT ALL in your queries whenever and wherever possible. If you're after the city, select the city - especially in this case ;)

Anyhow, this is what you are after:

$result = mysql_query("SELECT DISTINCT city FROM hotels ORDER BY city ASC");
0

Hi

If anyone could help with below code as I am new to php

<?php
$result = mysql_query("SELECT * FROM hotels ORDER BY city");

while($row = mysql_fetch_array($result))
  {
  echo $row['city'] . " " ;
  echo "<br />";
  }
?>

above code list all cities as below
newyork
newyork
newyork
newyork
newyork
newyork
Washington
Washington
Washington
Washington
Washington
Washington
Seattle
Seattle
Seattle
Seattle
Seattle
Kansas
Kansas
Kansas
Kansas
Kansas
Kansas
Kansas
.... continue

but i want to list like below and when a city link is clicked, all the hotels in that city will display on a city Page using the appropriate template

Newyork
washington
seattle
kansas ....

any help much appreciate

Thanks

<?php
$result = mysql_query("SELECT * FROM hotels ORDER BY city");
$city=;
while($row = mysql_fetch_array($result))
  {
 if($city!=$row['city']){
  echo "<a href='city.php?city={$row['city']}'>{$row['city']} </a><br />";
     }
   $city=$row['city'];
  }
?>

this will list the city out by itself and then when you click it will open up the city page passing in the city as a get variable you can then run you php based on that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.