Alexhoward Posted June 5, 2008 Share Posted June 5, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/ Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 use barands baaselect, it work great. Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558597 Share on other sites More sharing options...
Alexhoward Posted June 5, 2008 Author Share Posted June 5, 2008 wow! what a speedy reply! i'm not sure what you mean by that, i'll have a look on google cheers Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558599 Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 he is a moderator here http://members.aol.com/barryaandrew/baaselect/baaselectguide.html Ray Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558600 Share on other sites More sharing options...
Alexhoward Posted June 5, 2008 Author Share Posted June 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558609 Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 does the data come from 2 different tables?? so one dropdown is populated by one table and the other is populated from another?? Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558627 Share on other sites More sharing options...
Alexhoward Posted June 5, 2008 Author Share Posted June 5, 2008 Hi, no, just one table category: cat | subcat Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558636 Share on other sites More sharing options...
craygo Posted June 5, 2008 Share Posted June 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558651 Share on other sites More sharing options...
Alexhoward Posted June 5, 2008 Author Share Posted June 5, 2008 Thanks a lot! it was kind of working, but isn't now i'll keep playing and report back Cheers Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558671 Share on other sites More sharing options...
Alexhoward Posted June 5, 2008 Author Share Posted June 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558681 Share on other sites More sharing options...
DarkWater Posted June 5, 2008 Share Posted June 5, 2008 Remove the else { } (keep the code though), and include the rest of the code into the IF. Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558683 Share on other sites More sharing options...
Alexhoward Posted June 5, 2008 Author Share Posted June 5, 2008 Hi, Thanks for spending the time to look at this... there's a lot of else's could you be more specific...? sorry for being a noob Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558687 Share on other sites More sharing options...
DarkWater Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558693 Share on other sites More sharing options...
Alexhoward Posted June 5, 2008 Author Share Posted June 5, 2008 Alright, smart! no quite what i'm after still thou...? think i can get it form here thou, but do you know how to get it to retain the selected value...? Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-558712 Share on other sites More sharing options...
Alexhoward Posted June 6, 2008 Author Share Posted June 6, 2008 Hi, just to start this up again. Does anyone know how to reatin values in a dropdown populated from mysql..? Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-559380 Share on other sites More sharing options...
craygo Posted June 6, 2008 Share Posted June 6, 2008 you can do this $selected = @$_POST['cat'] == $row['cat'] ? "selected" : ""; echo"<option value=\"{$row['cat']}\" />{$row['cat']}</option>\n"; That will select the cat Widow Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-559386 Share on other sites More sharing options...
Alexhoward Posted June 6, 2008 Author Share Posted June 6, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-559411 Share on other sites More sharing options...
Alexhoward Posted June 7, 2008 Author Share Posted June 7, 2008 hi, thanks for all the help so far, but does anyone know how to do this...? cheers Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-559819 Share on other sites More sharing options...
Alexhoward Posted June 7, 2008 Author Share Posted June 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-559935 Share on other sites More sharing options...
Alexhoward Posted June 8, 2008 Author Share Posted June 8, 2008 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/108893-solved-dropdown-from-mysql-retaining-values/#findComment-560399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.