Jump to content

<option selected="selected">


unistake

Recommended Posts

Hi all,

 

I have a html drop down menu and want to show the initially selected value as the one stored in the mysql database that a member selected.

 

I have 180+ rows in mysql that a member could have selected and would like to basically do this:

 

IF (value of option == $row['value'] ) { echo "selected=\"selected\"; }

I could put this in every one of the rows it would be much better to have an if statement. As there are so many values I do not know where to begin.

 

Can someone point me in the right direction?

 

Thanks

Link to comment
Share on other sites

Would something like this work?

<?php 

include ("cxn.php");

$sql = "SELECT manufacturer FROM list";
$result = mysqli_query($cxn,$sql)
     or die ("Can't execute query!");
?>
<div align="left">
           <select name="manufacturer" id="manufacturer">
           <option value="” selected="selected">Select a Manufacturer..</option>

<?php

$manufacturer // FROM ANOTHER MYSQL DB

while ($row = mysqli_fetch_assoc($result)) {
    
     if ($manufacturer == $row['manufacturer'] { $sel = "selected=\"selected\""; }
    else { $sel = ""; }

     echo "<option ".$sel." value=\"".$row[manufacturer']."\'">".$row[manufacturer']."</option>"";
}
?>

Link to comment
Share on other sites

Something like this:

 

<?php
$sql = mysql_query("SELECT * FROM member_options WHERE member_id = '12345'");
$row = mysql_fetch_assoc($sql);
$selItm = $row['selItm'];

$sql = mysql_query("SELECT * FROM tbl");
$opt = '<select name="myselect">';
while($row = mysql_fetch_assoc($sql)){
    $opt .= '<option value="'.$row['itemvalue'].'"'.(($row['itemvalue']==$selItm) ? ' selected="selected"' : '').'</option>';
}
$opt .= '</select>';
echo $opt;
?>

Link to comment
Share on other sites

I think the problem with the original code is that you're quoting the ".$sel" value...  Perhaps this mod could work out.. It looks like the output would be:

 

<option value="foo" "selected="selected"">Foo</option>

 

That could be causing your html to break?

 

Try this:

 

echo '<option '.$sel.' value="'.$row['manufacturer'].'">'.$row['manufacturer'].'</option>'."\n";

 

There's many debates on single quotes and double quotes and I use single quotes to avoid confusion in HTML such as your initial example above.

 

Hope that helps, if not let us know.

 

Link to comment
Share on other sites

good tip phpfreak.

 

I have not tried the script yet but will do it your way. Thought there may be a better way.

 

On the " and ' debate. When CANT you replace double quotes with singles? I have had problems before echoing images for example or flash files.

 

Thanks

Link to comment
Share on other sites

good tip phpfreak.

 

I have not tried the script yet but will do it your way. Thought there may be a better way.

 

On the " and ' debate. When CANT you replace double quotes with singles? I have had problems before echoing images for example or flash files.

 

Thanks

 

Personally I gave up on double quotes a long time ago.  There technically shouldn't be any problem other than you have to constantly escape double quotes in your HTML and that drives me crazy.  There's many theories behind developing theme layers versus data layers and some people wouldn't even use HTML in your PHP code - they would use a straight up theme system for that that takes variables / strings and puts them into the template properly, so in your template you may have placeholders like {SOME_VARIABLE} that would be replaced.  That's a bit of a pain in the ass to me and I don't mind putting HTML within my PHP.

 

For example, instead of doing:

 

<?
$foo = 'Hi, I\'m FOOO!';

echo 'Who is foo? '.$foo.';
?>

 

Would be how I would do it.. or

 

<?
$foo = 'Hi I am Foo';
?>

Who is foo? <?=$foo?>

 

Would be another way..

 

I'm not sure if this directly answers your question, but I prefer single quotes...

Link to comment
Share on other sites

OK. This is what I have made. It does not show the first two records in the database and the field that should be 'selected' is not present.

 

Any ideas?

 

<select name="manufacturer" id="manufacturer">
<option value="”>Select a Manufacturer..</option>

<?php
// SELECTS AIRCRAFT DETAILS FROM THE DATABASE
$sql2 = "SELECT * FROM sales WHERE reg='$reg'";
$result2 = mysqli_query($cxn,$sql2)
or die ("Can't execute query 2!");
$row2 = mysqli_fetch_assoc($result2);

// OPENS THE CONNECTION TO THE AIRCRAFT LIST DATABASE TO COMPARE THE VALUES OF THE AIRCRAFT IN SUBJECT
$sql3 = "SELECT manufacturer FROM list ORDER BY id ASC ";
$result3 = mysqli_query($cxn,$sql3)
or die ("Can't execute query 3!");
$row3 = mysqli_fetch_assoc($result3);

while($row3 = mysqli_fetch_assoc($result3))
{
if ($row2['manufacturer'] != $row3['manufacturer']) {
	echo "<option value=\"".$row2['manufacturer'].">".$row3['manufacturer']."</option>";
}
else {
	echo "<option value=\"".$row2['manufacturer']." selected='selected'>".$row3['manufacturer']."</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.