Reputation: 1939
I'm a newbie in php & i'm trying to load dropdown list from a database. From the below code only the else
loop is working. The if
and elseif
is not working.
I don't know what's the mistake in the loop.
<html>
<head>
</head>
<body>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<select name="value">
<option value="1">CLUB/FEDERATION/LIGUE</option>
<option value="2">SPONSOR</option>
<option value="7">AGENCE CONSEIL</option>
</select>
<br>
<?php
$db = JFactory::getDBO();
if($_POST['value'] == '1') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=1";
$result = mysql_query($query);
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
echo "</select>";
}
elseif($_POST['value'] == '2') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=2";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
echo "</select>";
}
else {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=7";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
echo "</select>";
}
}
?>
</form>
</body>
</html>
Upvotes: 1
Views: 208
Reputation: 1016
You are not closing your while loop's in the first if statement and else if statement. Missing the closing '}'
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
Also in the 'else' statement you want to move the '}' bracket to before the line
echo "</select>";
Upvotes: 1
Reputation: 154
There are a few problems with the script.
First, there is no submit button (before the </form>
). That allows the content to be returned to the server.
Second, the ELSE will be executed even if there is no selection made on the first load of the page.
In each of the ifs, there seems to be missing a closing }
in the loop. And there is an extra one at the end.
Here is what I would do:
<html>
<head>
</head>
<body>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<select name="value">
<option value="1">CLUB/FEDERATION/LIGUE</option>
<option value="2">SPONSOR</option>
<option value="7">AGENCE CONSEIL</option>
</select>
<br>
<?php
$db = JFactory::getDBO();
if (array_key_exists('value', $_POST) && $_POST['value']) {
if($_POST['value'] == '1') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=1";
$result = mysql_query($query);
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
}
elseif($_POST['value'] == '2') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=2";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
}
else {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=7";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
}
}
?>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Upvotes: 1
Reputation: 7762
try this
<html>
<head>
</head>
<body>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<select name="value">
<option value="1">CLUB/FEDERATION/LIGUE</option>
<option value="2">SPONSOR</option>
<option value="7">AGENCE CONSEIL</option>
</select>
<br>
<?php
$db = JFactory::getDBO();
if($_POST['value'] == '1') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=1";
$result = mysql_query($query);
//$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
}elseif($_POST['value'] == '2') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=2";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
}else {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=7";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
echo "</select>";
}
?>
</form>
</body>
</html>
Upvotes: 0