Jump to content

[SOLVED] Drop Down Boxes selecting selected option


iceblox

Recommended Posts

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 '';

}


?>

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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 '';

}


?>

Link to comment
Share on other sites

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>"; 
}

}
?>

Link to comment
Share on other sites

this should work for sure  :P, 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>"; 
}

}
?>

Link to comment
Share on other sites

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>"; 
}

}
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.