Jump to content

display and count - query


gabrielkolbe

Recommended Posts

Hi, I have a property website, on it I have a drop down box where all the for cities. On the drop down box is displayed all the cities of the properties on the database, I also want to count how many properties there are for each city and then display the number next to the city, so that users can know how many properties are available for each city. ( I can count the amount of times each city appears in the database )

 

Here is my code so far, I would appreciate someone to help me here!!

Don't be confused by the code I have tried various ways of making this work, I can display the cities...

NOTE: That I would probably be easier to push the cities in an array, use the unique array function to only display 1 of each city, BUT before this count the amount of times a city appear....How to do this, i don't know..

 

<select name="city" class="formfields">

    <option value="All" selected>All Cities</option>

    <?

//$i = 0;

$cities = array();

$query = "SELECT * FROM right_prop_1 order by city";

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

array_push($cities, $row['city']);

//for ($i = 1; $i <= 10; $i++) {

//    echo $i;

//}

 

//if (array_key_exists($row['city'], $cities)) {

//$i=$i+1;

 

 

  echo '<option>' . $row['city'] .' [' . $i . ']</option>';

  }

}

 

 

?>

  </select>

Link to comment
Share on other sites

Sorry, my writing was not clear, here we go again..

 

Hi, I have a property website, on it I have a drop down box where all cities are displayed. I also want to count how many properties there are for each city and then display the number next to the city, so that users can know how many properties are available for each city.

 

Here is my code so far, I would appreciate someone to help me here!!

Don't be confused by the code I have tried various ways of making this work.

NOTE: That I would probably be easier to push the cities in an array, use the unique array function to only display 1 of each city, BUT before this, count the amount of times each city appears....

 

<select name="city" class="formfields">

    <option value="All" selected>All Cities</option>

    <?

//$i = 0;

$cities = array();

$query = "SELECT * FROM right_prop_1 order by city";

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

array_push($cities, $row['city']);

//for ($i = 1; $i <= 10; $i++) {

//    echo $i;

//}

 

//if (array_key_exists($row['city'], $cities)) {

//$i=$i+1;

 

 

  echo '<option>' . $row['city'] .' [' . $i . ']</option>';

  }

}

 

 

?>

  </select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.