Jump to content

[SOLVED] please help to generate html select from an array


tomasd

Recommended Posts

Hi,

I've got a following code:

<?php
$stack = Array
(
    1 => 'Accounting',
    2 => 'Banqueting',
    3 => 'Executive Office',
    4 => 'Front Office',
    5 => 'Housekeeping',
    6 => 'Human Resources',
    7 => 'Kitchen',
    8 => 'Loss Prevention',
    9 => 'Mini Bar',
    10 => 'Repairs & Maintenance',
    11 => 'Revenue',
    12 => 'Room Service',
    13 => 'Sales & Marketing'
);

$select = '<select name="user_department" id="user_department" size="1">'."\are\n";
foreach($stack as $key => $value)
{
    $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\are\n";
}
$select .= '</select>';

echo $select;

?>

Which generates me following html output:

<select name="user_department" id="user_department" size="1">
<option value="1">Accounting</option>
<option value="2">Banqueting</option>
<option value="3">Executive Office</option>
<option value="4">Front Office</option>
<option value="5">Housekeeping</option>
<option value="6">Human Resources</option>
<option value="7">Kitchen</option>
<option value="8">Loss Prevention</option>
<option value="9">Mini Bar</option>
<option value="10">Repairs & Maintenance</option>
<option value="11">Revenue</option>
<option value="12">Room Service</option>
<option value="13">Sales & Marketing</option>
</select>

 

I need to introduce a variable which would be equal to some int 1 to 13 which would make a selection so for instance if int = 4 I would be getting following html output:

<select name="user_department" id="user_department" size="1">
<option value="1">Accounting</option>
<option value="2">Banqueting</option>
<option value="3">Executive Office</option>
<option selected value="4">Front Office</option>
<option value="5">Housekeeping</option>
<option value="6">Human Resources</option>
<option value="7">Kitchen</option>
<option value="8">Loss Prevention</option>
<option value="9">Mini Bar</option>
<option value="10">Repairs & Maintenance</option>
<option value="11">Revenue</option>
<option value="12">Room Service</option>
<option value="13">Sales & Marketing</option>
</select>

 

How do I go about making that work?

Thanks ever so much for your help!

 

what about

 

<?php
$SelectedID = 4;


$select = '<select name="user_department" id="user_department" size="1">'."\are\n";

echo '<option value="'.$SelectedID.'">' . $stack[$SelectedID].'</option>'."\are\n";

foreach($stack as $key => $value)
{
if($key != $SelectedID)
{
    $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\are\n";
}
}
$select .= '</select>';
?>

Thanks for quick response (as usual:))

following code:

<?php
$stack = Array
(
    1 => 'Accounting',
    2 => 'Banqueting',
    3 => 'Executive Office',
    4 => 'Front Office',
    5 => 'Housekeeping',
    6 => 'Human Resources',
    7 => 'Kitchen',
    8 => 'Loss Prevention',
    9 => 'Mini Bar',
    10 => 'Repairs & Maintenance',
    11 => 'Revenue',
    12 => 'Room Service',
    13 => 'Sales & Marketing'
);

$SelectedID = 4;

$select = '<select name="user_department" id="user_department" size="1">'."\n";

echo '<option selected value="'.$SelectedID.'">' . $stack[$SelectedID].'</option>'."\n";

foreach($stack as $key => $value)
{
if($key != $SelectedID)
	{
	    $select .= "\t".'<option value="'.$key.'">' . $value.'</option>'."\n";
	}
}
$select .= '</select>';

echo $select;


?>

generates the following html:

<option selected value="4">Front Office</option>
<select name="user_department" id="user_department" size="1">
<option value="1">Accounting</option>
<option value="2">Banqueting</option>
<option value="3">Executive Office</option>
<option value="5">Housekeeping</option>
<option value="6">Human Resources</option>
<option value="7">Kitchen</option>
<option value="8">Loss Prevention</option>
<option value="9">Mini Bar</option>
<option value="10">Repairs & Maintenance</option>
<option value="11">Revenue</option>
<option value="12">Room Service</option>
<option value="13">Sales & Marketing</option>
</select>

It looks like <option selected value="4">Front Office</option> needs to be moved down, also is there any way of placing it in the 4th place in dropdown list oposed to 1st place?

 

Cheers!

Tomas

 

 

Try this

 

(slighlty more advanced)

 

<?php
$stack = Array
(
    1 => 'Accounting',
    2 => 'Banqueting',
    3 => 'Executive Office',
    4 => 'Front Office',
    5 => 'Housekeeping',
    6 => 'Human Resources',
    7 => 'Kitchen',
    8 => 'Loss Prevention',
    9 => 'Mini Bar',
    10 => 'Repairs & Maintenance',
    11 => 'Revenue',
    12 => 'Room Service',
    13 => 'Sales & Marketing'
);

$SelectedID = 4;

$select = '<select name="user_department" id="user_department" size="1">'."\n";

foreach($stack as $key => $value)
{
    $Selected = ($key == $SelectedID)?"selected":"";
    $select .= "\t".'<option $Selected value="'.$key.'">' . $value.'</option>'."\n";
}
$select .= '</select>';

echo $select;


?>

Oppp edited the one above..

 

updated

 

Try this

 

(slighlty more advanced)

 

<?php
$stack = Array
(
    1 => 'Accounting',
    2 => 'Banqueting',
    3 => 'Executive Office',
    4 => 'Front Office',
    5 => 'Housekeeping',
    6 => 'Human Resources',
    7 => 'Kitchen',
    8 => 'Loss Prevention',
    9 => 'Mini Bar',
    10 => 'Repairs & Maintenance',
    11 => 'Revenue',
    12 => 'Room Service',
    13 => 'Sales & Marketing'
);

$SelectedID = 4;

$select = '<select name="user_department" id="user_department" size="1">'."\n";

foreach($stack as $key => $value)
{
    $Selected = ($key == $SelectedID)?"selected":"";
    $select .= "\t".'<option $Selected value="'.$key.'">' . $value.'</option>'."\n";
}
$select .= '</select>';

echo $select;


?>

they're all gone selected now :)

<select name="user_department" id="user_department" size="1">
<option $Selected value="1">Accounting</option>
<option $Selected value="2">Banqueting</option>
<option $Selected value="3">Executive Office</option>
<option $Selected value="4">Front Office</option>
<option $Selected value="5">Housekeeping</option>
<option $Selected value="6">Human Resources</option>
<option $Selected value="7">Kitchen</option>
<option $Selected value="8">Loss Prevention</option>
<option $Selected value="9">Mini Bar</option>
<option $Selected value="10">Repairs & Maintenance</option>
<option $Selected value="11">Revenue</option>
<option $Selected value="12">Room Service</option>
<option $Selected value="13">Sales & Marketing</option>
</select>

oops single quote

 

<?php
$stack = Array
(
    1 => 'Accounting',
    2 => 'Banqueting',
    3 => 'Executive Office',
    4 => 'Front Office',
    5 => 'Housekeeping',
    6 => 'Human Resources',
    7 => 'Kitchen',
    8 => 'Loss Prevention',
    9 => 'Mini Bar',
    10 => 'Repairs & Maintenance',
    11 => 'Revenue',
    12 => 'Room Service',
    13 => 'Sales & Marketing'
);

$SelectedID = 4;

$select = '<select name="user_department" id="user_department" size="1">'."\n";

foreach($stack as $key => $value)
{
    $Selected = ($key == $SelectedID)?"selected":"";
    $select .= "\t".'<option '.$Selected.' value="'.$key.'">' . $value.'</option>'."\n";
}
$select .= '</select>';

echo $select;


?>

Archived

This topic is now archived and is closed to further replies.

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