difficulties with query
Posted: Tue Jul 07, 2009 9:55 pm
I am looking for some help with a project I am doing. What I am trying to accomplish is to create three drop down selections for identification of location in a form. A mysql database has already been established to organize countries, states, and cities. This question is directed towards the query that is for the state drop down. $countryfix is defined as the selected country from the country drop down menu. print_r($countryfix) works great!It keeps up with every country as selected, although when I run WHERE country ='$countryfix';" within the state query, it will not work. I have tried print_r($stateList) it returns as ( [0] => ) . When I manually type something into the query (WHERE country = 'United States of America'") it returns with the expected array. Might someone send me in the right direction so where that print_r($stateList) out the expected array when (WHERE country = '$countryfox';") is used in the state query, it would be greatly appreciated.
Code: Select all
<SCRIPT language=JavaScript>
function reload(form)
{
//Setting the variable with the value of selected country's ID
var val=populate.countryList.options[populate.countryList.options.selectedIndex].value;
// Sending the country id in the query string to retrieve the city list
self.location='flo.php?countryId=' + val ;
}
</script>
<form action="flo.php" name="populate">
<?php
mysql_connect("******************") or die(mysql_error());
mysql_select_db("_root") or die(mysql_error());
function getCountryList()
{ // Country List array
$querycountry = ("SELECT country FROM location_root") or die("something messed up");
$resultcountry = mysql_query($querycountry);
while ($rowcountry = mysql_fetch_array($resultcountry))
{
$categoriescountry[] = $rowcountry['country'];
}
return array_unique($categoriescountry);
}
// Retrieving the country list
$countryList = getCountryList();
// Setting the variable if the country is selected for its city list
$countryId = $_GET['countryId'];
//the selected selected country
@$countryfix = $countryList[$countryId];
// Retrieving the city list if a country is selected
$stateList = ($countryId) ? getStateList($countryId) : null;
if (!empty($countryList))
{
// Generating the country drop down menu
echo "<select onChange='reload(this.form)' name='countryList'>";
foreach ($countryList as $key => $value)
{
echo "<option value='$key'";
if ($countryId == $key)
echo "selected";
echo ">$value</option>";
}
echo "</select>";
}
?>
<br /> </form>
<form action="flo.php" name="populatestate">
<?
function getStateList()
{
// State List array
$resultstate = mysql_query("SELECT state FROM location_root WHERE country =' $countryfix ';") or die(mysql_error());
while ($rowstate = mysql_fetch_array($resultstate))
{
$categoriesstate[] = $rowstate['state'];
}
$stateList = array_unique($categoriesstate);
return $stateList;
}
if (!empty($stateList))
{
// Generating the city drop down menu if a country is selected
echo "<select onChange='reload(this.form)' name='stateList'>";
foreach ($cityList as $key => $value)
{
echo "<option value='$key'>$value</option>";
}
echo "</select>";
}
print_r($countryfix);
print_r($stateList);
?>
</form>