Page 1 of 1
Drop Down Menu
Posted: Mon Oct 12, 2009 10:00 pm
by michaelk46
I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.
THis was suggested to me:
Code: Select all
public function formOptions($options, $value = NULL)
{
$html = NULL;
foreach($options as $key => $content)
{
if ($key == $value) $select = ' selected="selected"';
else $select = NULL;
$html .=
'<option value="' . htmlspecialchars($key) . '"' . $select .
'>' . htmlspecialchars($content) . '</option>' . "\n";
}
return $html;
}
and this is the template:
Code: Select all
<select name="unit_type_id">
<option value="0">Organization Type</option>
<?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
</select>
I am on the verge of understanding it, but I can't quite grasp it.
Could one of y'all explain how this accomplishes that goal? Thanks in advance
Re: Drop Down Menu
Posted: Tue Oct 13, 2009 8:31 am
by markusn00b
michaelk46 wrote:I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.
THis was suggested to me:
Code: Select all
public function formOptions($options, $value = NULL)
{
$html = NULL;
foreach($options as $key => $content)
{
if ($key == $value) $select = ' selected="selected"';
else $select = NULL;
$html .=
'<option value="' . htmlspecialchars($key) . '"' . $select .
'>' . htmlspecialchars($content) . '</option>' . "\n";
}
return $html;
}
and this is the template:
Code: Select all
<select name="unit_type_id">
<option value="0">Organization Type</option>
<?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
</select>
I am on the verge of understanding it, but I can't quite grasp it.
Could one of y'all explain how this accomplishes that goal? Thanks in advance
This is why folks should document their code. Anyway, on to the explanation:
You make a call to the formOptions() method of whatever class (I'm assuming it's some controller), passing it 2 arguments: unitTypePickList, and unitTypeId - an array and an identifier, respectively.
The formOptions() method then creates an empty variable, $HTML, which will be used to store the rendered HTML. Duh. Then the method begins looping over the $options array (unitTypePickList) assigning the current index or the iteration to $key and the current index's value to $content. Within the foreach() loop, there is a conditional that checks whether $key == $value - this is where the 'selected' option element is decided; if a match is found, the $selected variable contains the HTML attribute 'selected="selected"'. Otherwise, it remains empty. Next, the HTML option element is constructed and concatenated onto the $HTML variable, including the $selected and $content variables in the output.
Once the loop is completed, the $HTML variable is returned to the caller (in this case, echo) and echo proceeds to output the data.
Simple.
Mark.
Re: Drop Down Menu
Posted: Tue Oct 13, 2009 8:22 pm
by michaelk46
I am still fairly new to PHP and that explanation has gone a long way in helping me understand, but I have a couple of questions...
there is a conditional that checks whether $key == $value - this is where the 'selected' option element is decided; if a match is found, the $selected variable contains the HTML attribute 'selected="selected"'. Otherwise, it remains empty.
This is where it still gets confusing to me... why would $key and $value be equal?
Once the loop is completed, the $HTML variable is returned to the caller (in this case, echo) and echo proceeds to output the data.
Second, does this line store the selected option in $html?
Re: Drop Down Menu
Posted: Wed Oct 21, 2009 12:23 pm
by markusn00b
michaelk46 wrote:I am still fairly new to PHP and that explanation has gone a long way in helping me understand, but I have a couple of questions...
there is a conditional that checks whether $key == $value - this is where the 'selected' option element is decided; if a match is found, the $selected variable contains the HTML attribute 'selected="selected"'. Otherwise, it remains empty.
This is where it still gets confusing to me... why would $key and $value be equal?
Once the loop is completed, the $HTML variable is returned to the caller (in this case, echo) and echo proceeds to output the data.
Second, does this line store the selected option in $html?
Sorry for my late reply, michaelk46.
The $value parameter is the index of the array ($options) that is intended to be the 'selected' option. The $key variable is what keeps track of the current index for the iteration of the foreach loop. So, it translates into human text as: if the current iteration's index is equal to the arbitrary $value, that is, the user defined element to be labelled as 'selected', append the selected text into the html. Does that help?
Second question: could you elaborate? What line would that be?
Mark.
Re: Drop Down Menu
Posted: Wed Oct 21, 2009 12:27 pm
by Mirge
markusn00b wrote:michaelk46 wrote:I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.
THis was suggested to me:
Code: Select all
public function formOptions($options, $value = NULL)
{
$html = NULL;
foreach($options as $key => $content)
{
if ($key == $value) $select = ' selected="selected"';
else $select = NULL;
$html .=
'<option value="' . htmlspecialchars($key) . '"' . $select .
'>' . htmlspecialchars($content) . '</option>' . "\n";
}
return $html;
}
and this is the template:
Code: Select all
<select name="unit_type_id">
<option value="0">Organization Type</option>
<?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
</select>
I am on the verge of understanding it, but I can't quite grasp it.
Could one of y'all explain how this accomplishes that goal? Thanks in advance
This is why folks should document their code. Anyway, on to the explanation:
Off-topic, but I read that... then read your signature. I chuckled.

Re: Drop Down Menu
Posted: Wed Oct 21, 2009 12:32 pm
by markusn00b
Mirge wrote:markusn00b wrote:michaelk46 wrote:I am trying to setup a page that uses a drop down menu to select which terms are used to search a database.
I also want to keep the same choice showing on the results page that was selected.
THis was suggested to me:
Code: Select all
public function formOptions($options, $value = NULL)
{
$html = NULL;
foreach($options as $key => $content)
{
if ($key == $value) $select = ' selected="selected"';
else $select = NULL;
$html .=
'<option value="' . htmlspecialchars($key) . '"' . $select .
'>' . htmlspecialchars($content) . '</option>' . "\n";
}
return $html;
}
and this is the template:
Code: Select all
<select name="unit_type_id">
<option value="0">Organization Type</option>
<?php echo $this->formOptions($this->unitTypePickList,$item->unitTypeId); ?>
</select>
I am on the verge of understanding it, but I can't quite grasp it.
Could one of y'all explain how this accomplishes that goal? Thanks in advance
This is why folks should document their code. Anyway, on to the explanation:
Off-topic, but I read that... then read your signature. I chuckled.

Are you insinuating I am a hypocrite? Off with his head!
Re: Drop Down Menu
Posted: Wed Oct 21, 2009 12:33 pm
by Mirge
The irony was awesome

Re: Drop Down Menu
Posted: Wed Oct 21, 2009 12:46 pm
by markusn00b
Mirge wrote:The irony was awesome

Of course I
do document my writings; I just like the phrase.
/on-topic.
Re: Drop Down Menu
Posted: Wed Oct 21, 2009 12:58 pm
by Mirge
markusn00b wrote:Mirge wrote:The irony was awesome

Of course I
do document my writings; I just like the phrase.
/on-topic.
I figured
