iceblox Posted November 2, 2007 Share Posted November 2, 2007 Hi Guys, I have this code to allow me to get the selected option selected in the dropdown. The option is stored in url variable the code i currently have is selecting everything. I am doing something wrong? <?php $connection = mysql_connect('localhost','user','pass'); mysql_select_db('mobile'); $query = "SELECT * FROM deals GROUP BY MakeName ASC"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { if($_GET['MakeName'] == $row['MakeName']) { $selected = 'selected'; } else { $selected = ''; } echo "<option value='".$row['11']."' $selected>".$row['11']."</option>"; } } else { echo ''; } ?> Quote Link to comment Share on other sites More sharing options...
otuatail Posted November 2, 2007 Share Posted November 2, 2007 You could put $selected = ''; above the if statment and not use elseor any brackets $selected = ''; if($_GET['MakeName'] == $row['MakeName']) $selected = 'selected'; Having said that, you could also echo out $row['MakeName'] to see if it is diffrent. There dosn't seem to anything wrong with the if statment like only having one = sign. if($_GET['MakeName'] = $row['MakeName']) That would cause it. What is the value of $_GET['MakeName'] ? you could override the value of it to 'ABC' then nothing should be selected. Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 2, 2007 Author Share Posted November 2, 2007 OK thanks for that ill have a look into it Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 3, 2007 Author Share Posted November 3, 2007 Hello Everyone, I finally got my drop down boxes working but i have a small issue, When someone selects something with two or more words the system has a bit of trouble. As the script has it as say "Sony+Ericsson" but the database knows it as "Sony Ericsson" I have tried to str_replace it but not had any luck can someone help me out please? <?php $query = "SELECT * FROM deals GROUP BY MakeName ASC"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { if($row['11']==$MakeName) { $selected = 'selected'; } else { $selected = ''; } echo "<option value=\"".$row['11']."\" $selected>".$row['11']." $sub</option>"; } } else { echo ''; } ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 well try this code <?php $connection = mysql_connect('localhost','user','pass'); mysql_select_db('mobile'); $query = "SELECT * FROM deals GROUP BY MakeName ASC"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { $arrSelected[$_GET['MakeName']] = " selected "; echo "<option value='".$row['11']."' ".@$arrSelected[$row['11']].">".$row['11']."</option>"; } } ?> Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 3, 2007 Author Share Posted November 3, 2007 Hmm That didnt work Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 this should work for sure , let me know if you face any problems <?php $connection = mysql_connect('localhost','user','pass'); mysql_select_db('mobile'); $query = "SELECT * FROM deals GROUP BY MakeName ASC"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { $arrSelected[urldecode($_GET['MakeName'])] = " selected "; echo "<option value='".$row['11']."' ".@$arrSelected[$row['11']].">".$row['11']."</option>"; } } ?> Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 3, 2007 Author Share Posted November 3, 2007 Hmm nope its just not selecting it as thinks it Sony+Ericsson... Any other ideas? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 is it Sony+Er... in your database or in your get parameter Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 3, 2007 Author Share Posted November 3, 2007 in the variable it is "Sony+Ericsson" and in the database it is "Sony Ericsson" Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 this line try $arrSelected[trim(urldecode($_GET['MakeName']))] = " selected "; Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 3, 2007 Author Share Posted November 3, 2007 That trimmed away the Ericsson part of the code.. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 try this and tell me what you get in the output (viewsource) <?php $connection = mysql_connect('localhost','user','pass'); mysql_select_db('mobile'); $query = "SELECT * FROM deals GROUP BY MakeName ASC"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { $arrSelected[urldecode($_GET['MakeName'])] = " selected "; print $arrSelected[urldecode($_GET['MakeName'])]."<BR>-<BR>-". $row['11']."-"; echo "<option value='".$row['11']."' ".@$arrSelected[$row['11']].">".$row['11']."</option>"; } } ?> Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 3, 2007 Author Share Posted November 3, 2007 This is the output <select name="MakeName" id="MakeName" onChange="ajaxSearchBrand()"> <option value="">Any Mobile Phone</option> selected <BR>-<BR>-3-<option value='3' >3</option> selected <BR>-<BR>-Blackberry-<option value='Blackberry' >Blackberry</option> selected <BR>-<BR>-E-ten-<option value='E-ten' >E-ten</option> selected <BR>-<BR>-HTC-<option value='HTC' >HTC</option> selected <BR>-<BR>-I-Mate-<option value='I-Mate' >I-Mate</option> selected <BR>-<BR>-Levi-<option value='Levi' >Levi</option> selected <BR>-<BR>-LG-<option value='LG' >LG</option> selected <BR>-<BR>-Lobster-<option value='Lobster' >Lobster</option> selected <BR>-<BR>-Mandarina Duck-<option value='Mandarina Duck' >Mandarina Duck</option> selected <BR>-<BR>-Motorola-<option value='Motorola' >Motorola</option> selected <BR>-<BR>-Nokia-<option value='Nokia' >Nokia</option> selected <BR>-<BR>-O2-<option value='O2' >O2</option> selected <BR>-<BR>-Orange-<option value='Orange' >Orange</option> selected <BR>-<BR>-PalmOne-<option value='PalmOne' >PalmOne</option> selected <BR>-<BR>-Qtek-<option value='Qtek' >Qtek</option> selected <BR>-<BR>-Samsung-<option value='Samsung' >Samsung</option> selected <BR>-<BR>-Sony Ericsson-<option value='Sony Ericsson' >Sony Ericsson</option> selected <BR>-<BR>-T-Mobile-<option value='T-Mobile' >T-Mobile</option> selected <BR>-<BR>-Toshiba-<option value='Toshiba' >Toshiba</option> selected <BR>-<BR>-Vodafone-<option value='Vodafone' >Vodafone</option> </select> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 3, 2007 Share Posted November 3, 2007 looking into it will let you know Quote Link to comment Share on other sites More sharing options...
iceblox Posted November 3, 2007 Author Share Posted November 3, 2007 Thanks for all your help! Mananged to get it to work ;o) Quote Link to comment 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.