0

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.

Edited by mike_2000_17: Fixed formatting

3
Contributors
7
Replies
10
Views
6 Years
Discussion Span
Last Post by kgizo
0

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.

Edited by jrotunda85: Corrected Code Error

0

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?

0

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

0

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

0

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 />
0

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 :)

0

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

This question has already been answered. 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.