hi
i'm new to php, i already have a database called msansi its table called provinces i've already created a drop down list of all my nine provinces now what i want to do is, i want the cities associated with the provinces to display on the same page as the drop down list i.e if you click on gauteng the cities in gauteng have to appear on the same page.

here's my code

<html>
<head><title><?php echo $title;?></title></head>

<body>

<h1><?php echo $heading;?></h1>

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("msansi", $con);

$result = mysql_query("SELECT * FROM provinces");

 echo  "<select name=province>";
while($row = mysql_fetch_array($result))
{

{

    echo "<option value>" . $row['title']."</option>";

        }
        }
//<option>Select Province</option>}
 echo "</select>";

i tried doing it in javascript and the code is

<script language="javascript" type="text/javascript">
 if (<select name==$title)
  {
  document.write("<b>$row['BODY']</b>");
  BODY.getElementsBytitle('title');

  {
    else 
    return null;
  }
  }
</script>

</body>
</html>

where title is the column name of the provinces and BODY is the column name for the cities, i used a different framework called codeigniter.

If I understand you correctly, you are trying to populate a drop down list from your database? If so, try this:

<select name="xx" value="xx">
<?php

$con = mysql_connect('localhost','root','');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('msansi', $con); 

$query = "SELECT * FROM provinces";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
 
	echo "<option select value=\"0\">Select Province</option>";
 
	if (mysql_num_rows($result) > 0) { 
	 while ($row = mysql_fetch_row($result)) {
	echo "<option value=\"$row['value']\">$row['title']</option>";
		 }
	}else{ 
                 echo "No rows found!"; 
	} 
	mysql_free_result($result);
?>
</select><br />

You'll need to change the part $row in the second option to reflect what value in your database corresponds to that field. My reccomendation would be to assign a code to each province such that Province XX is stored in the database as 1234 or whatever.

Hi,

if i understand well the options of the select contain the names of the provinces, and you want the name of the cities related to the selected province to be displayed on the same page when the user makes a choice in the dropdown menu... That's correct?

You need to use javascript, and certainly php. That means Ajax, if you want to do so without reloading of the page...

How do you store the name of the cities related to the provinces?

thanxx @ jrotunda85 i'm sorry i'm only replyin to your response i'm gonna try the code out now. @ stefh your correct thats exactly what i want to do but i cant use ajax cause i'm using codelobster and it doesn't support ajax, and the name of the cities are stored under the column name BODY in the database table.thanxx 4 showing interest @ stefh

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\xampp\htdocs\CodeIgniter_1.7.3\system\application\views\ndivhuhoview.php on line 47 which is line 19 on your code
i do have an id field which is my primary key i tried changing value to id and $row to $row but it still shows the same error, my id field starts from 1 to 9 for all 9 provinces, BODY is the field for all the cities in each province and title for each province.
thanxx in advance @ jrotunda85

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\xampp\htdocs\CodeIgniter_1.7.3\system\application\views\ndivhuhoview.php on line 47 which is line 19 on your code
i do have an id field which is my primary key i tried changing value to id and $row to $row but it still shows the same error, my id field starts from 1 to 9 for all 9 provinces, BODY is the field for all the cities in each province and title for each province.
thanxx in advance @ jrotunda85

You shouldn't need to use () on echo since it's not a function but try this code below. You might also want to check the column names to ensure they match exactly (case sensitive).

<select name="xx">
<?php

$con = mysql_connect('localhost','root','');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('msansi', $con); 

$query = "SELECT * FROM provinces";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
 
	echo ("<option select value=\"0\">Select Province</option>");
 
	if (mysql_num_rows($result) > 0) { 
	 while ($row = mysql_fetch_row($result)) {
	echo ("<option value=\"$row['id']\">$row['title']</option>");
		 }
	}
	else{ 
                 echo "No rows found!"; 
	} 
	mysql_free_result($result);
?>
</select><br />

On second thought, I'm testing it and the error is in the way you are calling the columns that are in the array. instead of calling them by name, call them by number. So, for example:

<select name="xx">
<?php
 
$con = mysql_connect('localhost','root','');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
 
mysql_select_db('msansi', $con); 
 
$query = "SELECT id, title FROM provinces";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
 
	echo ("<option select value=\"0\">Select Province</option>");
 
	if (mysql_num_rows($result) > 0) { 
	 while ($row = mysql_fetch_row($result)) {
	echo ("<option value=\"$row[0]\">$row[1]</option>");
		 }
	}
	else{ 
                 echo "No rows found!"; 
	} 
	mysql_free_result($result);
?>
</select><br />

Notice two changes. The first on Line 12, instead of selecting everything, you select only the columns you want to display. The second on Line 19 where you call the array value instead of the name (keep in mind that 0 refers to the first column, 1 to the second, so on. So 0 is referring to your id column and 1 to your title).

Hope this helps :)

you're a genius man it worked thanxx alot 4 your help!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.