PDA

View Full Version : <option selected attribute problem newbie help needed pls



helloise
Jan 12th, 2011, 01:31 PM
i have the following code:
$catcher_names = LpmCatcherPeer::getByAllNames();
foreach($catcher_names as $row)
{
?>
<option value="<?php echo $row->getName()."/".$row->getId();?>" selected="<?php echo $catcher_name; ?>"><?php echo $row->getName();?></option>
<?php

}

a service is connected to a catcher..so when i select a service the corresponding catcher must show in the dropdown list but it is not...the selected attribute part is not working and i have tested that $catcher_name does contain the correct name for the service i selected

please help???
thanks

abduraooft
Jan 12th, 2011, 02:42 PM
I guess you need something like
foreach($catcher_names as $row)
{
printf('<option value="%s" %s>%s</option>',$row->getName()."/".$row->getId(),($row->getName()==$catcher_name?'selected="selected"':""),$row->getName();)
}

PS: untested!

Kor
Jan 12th, 2011, 02:45 PM
Lets take it from the other hand. The HTML syntax looks like that:


<select>
<option value="value">text</option>
<option value="value" selected="selected">text</option>
<option value="value">text</option>
<option value="value">text</option>
...
</select>

As you can see, there can be only a single option element which can bear the selected attribute. In HTML, the selected attribute has no Boolean values. There's an agreement about the fact the selected attribute should have the value "selected" (like this: selected="selected"), but techically you may use any value - the result will be the same: the option will be selected. In other words, the unselected options must not bear any selected attribute.

Now the php code. It is not very clear for me what lays behind your variable $catcher_name. I see an array $catcher_names, but no $catcher_name is defined.

If you want to select one of the options, there must be a condition somewhere. Or the value should take a certain value, or the text should be a certain string. Which is your condition? Which option should be selected and why?

helloise
Jan 12th, 2011, 03:14 PM
the $catcher_names is an array....
$catcher_name is the service i selected's corresponding catcher name...i defined it prior to the code i have...

ok so only ONE option can have the selected attribute...i must then set it outside of the loop then??

please help??
my code again:


<tr>
<th><?php echo $form['catcher_id']->renderLabel();
$catcher_id = $form->getObject()->getCatcherId();
$catcher = LpmCatcherPeer::getByCatcherId($catcher_id);
$catcher_name = $catcher->getName(); //what ever catcher name is in this variable must be the selected/highlighted in the dropdown
?></th>
<td>
<?php echo $form['catcher_id']->renderError() ?>
<select name="services">
<?php
$catcher_names = LpmCatcherPeer::getByAllNames();
foreach($catcher_names as $row)
{
?>
<option value="<?php echo $row->getName()."/".$row->getId();?>" selected="" onchange="refreshPage(this.value)"><?php echo $row->getName();?></option>
<?php
}
?>
</select>
</td>
</tr>


please see code in red including the comment i made
so how must i set selected(code in blue)??? please

many thanks!

Kor
Jan 12th, 2011, 03:26 PM
You don't get it.

Once again: you are writing some OPTION elements in a loop, using a PHP array. OK. Now, which is the relationship between your variable $catcher_name and the elements of the array $catcher_names? Should be something like this?:


if($row->getName()==$catcher_name){
/* then make this option selected? */
}

or like this?


if($row->getId()==$catcher_name){
/* then make this option selected? */
}

Or what else? What variable should be compared with what other variable?

abduraooft
Jan 12th, 2011, 03:42 PM
Have you tried the code in my above post?

Kor
Jan 12th, 2011, 03:58 PM
Have you tried the code in my above post?
Agree. That should cover my first presumption ($row->getName()) :) And a nice shortcut code, as well. Just a question: Shouldn't be sprintf() ? After all we are dealing with a string.