tags:

views:

51

answers:

2

Hi,

I have some trouble with this.

I have one database with the following tables: Countries -> All countries of the world are added Cities -> The cities are also added user_profile -> The profile of the user with the fields "country" & "city".

Everything works fine, even the populating of the boxes is working. But I don't know how to get the SELECTED value of the user for both country & city.

I have the following code:

Select Country: <br />
<select id="countries">
<option value="">--</option>

<?php
$sql = "SELECT country, title FROM countries ORDER BY title ASC";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['country']."\">".$row['title']."\n  ";
}
?>
</select>


Select City: <br />
<select id="cities">
<option value="">Select one</option>

<?php
$sql = "SELECT country, title FROM cities ".
"ORDER BY title ASC";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option class=\"".$row['country']."\"  value=\"".$row['title']."\">".$row['title']."\n  ";
}
?>
</select>

Now I need to check in the table user_profile which country & city he chose and show it to him.

I also use jchained (jquery plugin) to make both select boxes working together.

How can I achieve this? If I need to post anything else, please let me know.

Thanks in advance.

+1  A: 

Do you just mean you don't know what to do after they select the values? If so then you would just use the form to post the values to the new page. On the receiving page you can run a query to Update the user's profile.

Sorry if my response isn't detailed enough ><

edit Oh, I may see what you mean now. You're able to store it correctly but you want the correct option to be flagged as selected if it's what the user has previously set? If that's it then you can query for the value, then inside of the while loop just add a check to see if $user_country == $row['country'] and if so echo ' SELECTED '. Hope that helps!

Aaron Hathaway
Thanks for your answer. Yeah that's what I want to achieve, but I dont know how to implement the SQL query to do it. ;)
moonwalker
SELECT country, city FROM user_profile where id = $user_id;
Harmon Wood
Yes, as Harmon said.
Aaron Hathaway
@Harmon:When I do that I only get the country, city the user has chosen. But if he needs to edit his country or city he won't be able to, because there are no other countries/cities populated. I hope you understand what my dilemma is.I need to populate all the data from countries/cities and select the ones chosen by the user. So he can edit his country/city on his profile page.Thanks for your help!
moonwalker
+1  A: 

You have a lot of missing code here but I will try to help.

I am going to start by assuming that you have already stored the users selections in the user_profile table and that they are foreign keys to to correct relations.

To get the selected id's for repopulating the select boxes with the selected flag use:

SELECT country, city FROM user_profile where id = $user_id;

To get the text values you would do something like:

SELECT country.title as country, city.title FROM user_profile LEFT JOIN countries ON user_profile.country = country.id LEFT JOIN cities ON user_profile.city = cities.id WHERE user_profile.id = $user_id;

If you are not storing them as foreign key relations but instead by string title in the user_profile table you will need to do this:

SELECT country.title as country, city.title FROM user_profile LEFT JOIN countries ON user_profile.country = country.title LEFT JOIN cities ON user_profile.city = cities.title WHERE user_profile.id = $user_id;

You can then use these results to either display to the user or set the "SELECTED" flag in the options box.

Harmon Wood
moonwalker
He means just use that query to get what the user selected. Still perform the query to get all of the other possible countries and cities.
Aaron Hathaway
Thanks Aaron and Harmon!
moonwalker
Like this:$selected = "";while($row = mysql_fetch_array($rs)){ if ($user_row['country'] == $row['id']) : $selected = "selected"; echo "<option ".$selected." value=\"".$row['country']."\">".$row['title']."\n ";}
Harmon Wood
Basically do what I said then do what Aaron said.
Harmon Wood
Thanks Harmon. Works like a charm. :)
moonwalker