Jump to content

[SOLVED] Dropdown from mysql - retaining values


Alexhoward

Recommended Posts

Hi guys,

 

I can't seem to work this out?

 

can anyone help me?

 

want i'm trying to do is select a category from a dropdown which is populated from mysql, then select a sub-category from another dropdown populated from mysql from that selection.

 

i've got the mysql part down,

 

However, i'm having trouble retaining the values...

 

i don't really want to use javascript or anything...

 

hoping someone can give me a simple answer

 

Thanks in advance!

umm...

 

I think the problem is that my code is like this:

 

<?php

echo'<form><select name="cat" style="width:130px;">';

$res=mysql_query("select distinct cat from category order by cat");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
echo"<option>$row[cat]</option>";
}
echo'</select><input type="submit" value="Select"></form>';

?>

 

so there is only one physical option in the php

well you will need to check to see if the first has been submited

<?php
if(isset($_POST['cat'])){
echo'<form><select name="subcat" style="width:130px;">';
$cat = $_POST['cat'];
$res=mysql_query("select distinct subcat from category where cat = '$cat' order by subcat");
  if(mysql_num_rows($res)==0){
  echo "<option>there is no data in table..</option>\n";
  } else {
    while($row = mysql_fetch_assoc($res)){
    echo"<option value=\"{$row['subcat']}\" />{$row['subcat']}</option>";
    }
  }
  echo '</select><input type="submit" value="Select"></form>';
} else {
echo'<form><select name="cat" style="width:130px;">';
$res=mysql_query("select distinct cat from category order by cat");
  if(mysql_num_rows($res)==0){
  echo "<option>there is no data in table..</option>\n";
  } else {
    while($row = mysql_fetch_assoc($res)){
    echo"<option value=\"{$row['cat']}\" />{$row['cat']}</option>\n";
    }
  }
echo'</select><input type="submit" value="Select"></form>';
}
?>

 

Something like that. Not tested!

 

Ray

Alright Cool,

 

This code, brings up the category drop down, then when you select a value it replaces it with the sub-category dropdown.

 

Then if you select a sub-category it changes back to select a category...

 

this is nice, but what i'm really after is two dropdowns; category and subcategory, that retain their values once a selction is made, and the sub-category value is determined by the category value.

 

I will then use the two values to filter the results

 

Thanks for all your help so far

if(isset($_POST['cat'])){

echo'<form><select name="subcat" style="width:130px;">';

$cat = $_POST['cat'];

$res=mysql_query("select distinct subcat from category where cat = '$cat' order by subcat");

  if(mysql_num_rows($res)==0){

  echo "<option>there is no data in table..</option>\n";

  } else {

    while($row = mysql_fetch_assoc($res)){

    echo"<option value=\"{$row['subcat']}\" />{$row['subcat']}</option>";

    }

  }

  echo '</select><input type="submit" value="Select"></form>';

} else {

echo'<form><select name="cat" style="width:130px;">';

$res=mysql_query("select distinct cat from category order by cat");

  if(mysql_num_rows($res)==0){

  echo "<option>there is no data in table..</option>\n";

  } else {

    while($row = mysql_fetch_assoc($res)){

    echo"<option value=\"{$row['cat']}\" />{$row['cat']}</option>\n";

    }

  }

echo'</select><input type="submit" value="Select"></form>';

}

?>

 

That one.

Hi,

 

Thanks for your reply

 

I have my code set up like this

 

<?php

include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());


echo'<form><select name="cat" style="width:130px;">';

$res=mysql_query("select distinct cat from category order by cat");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
$selected = @$_POST['cat'] == $row['cat'] ? "selected" : "";
echo"<option value=\"{$row['cat']}\" />{$row['cat']}</option>\n";
}
echo'</select>';
echo'<input type="submit" value="select"></form>';

?>

 

but it doesn't seem to be retaining the value...?

 

have i done it correctly?

Alright Guys,

 

Right lets take this one step at a time...

 

Does anyone know how to retain values in a drop down populated from mysql...?

 

don;t worry about teh previous post about then using that value to populate another.

 

all i want to do is have what ever is selected

 

e.g.

 

http://website.php?cat=electronics

 

to then be selected when the page refreshes...

 

Thanks in advance

Sweet!

 

This is how you do it!

 

Thanks for everybodys help on this.

 

Thanks to craygo who did give me the correct answer, but missed the variable in the echo, however i should have spotted that ages a go,

 

Thanks again!

 

<?php

include("config.php");

//connect to the mysql server
$link = mysql_connect($host, $db, $pass)
or die ("Could not connect to mysql because ".mysql_error());

//select the database	
mysql_select_db($db)
or die ("Could not select database because ".mysql_error());


echo'<form><select name="cat" style="width:160px;">';

$res=mysql_query("select distinct cat from category order by cat");
if(mysql_num_rows($res)==0) echo "there is no data in table..";
else
for($i=0;$i<mysql_num_rows($res);$i++) {
$row=mysql_fetch_assoc($res);
$selected = @$_GET['cat'] == $row['cat'] ? "selected" : "";
echo"<option value=".$row['cat']." $selected>".$row['cat']."</option>";
}
echo'</select>';
echo'<input type="submit" value="select"></form>';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.