Reputation: 63
I have a simple query.
I have an array, which has two variables, which I define this way:
$sArray[0]['Name'] = $sName;
$sArray[0]['ID'] = $sID;
The above is in a loop which feeds the data for sName and sID. Example of the Data is:
Name: John Smith ID: 123
Name: Sue Smith ID:234
I'd like to output the sName data in a multi Select HTML and store the ID as the value and Name as the option display. The code I have below so far only outputs the first entry in the list, so the looping is not right. So, please help. :)
<select class="form-select" multiple id="SelectName" name="name_list[]">
<?php
$selectLength = count($sArray);
for ($j=0 ; $j<$selectLength ; $j++) {
foreach ($sArray[$j] as $value) {
echo"<option value=$value name='name_list[]'>$value</option>"?>
<?php
}
} ?>
</select>
Below is the dump of the array contents:
array(1) { [0]=> array(2) { ["Name"]=> string(3) "Sue" ["ID"]=> string(3) "234" } } array(1) { [0]=> array(2) { ["Name"]=> string(4) "John" ["ID"]=> string(3) "123" } }
Thanks in advance.
UPDATE: As requested, below is the loop which populates the array. It basically reads an XML file which has Name and ID data and places it in the array.
$fetchdata = simplexml_load_string($xml);
foreach ($elm->res as $list) {
$Team = $Agentlist->xpath('team[@name="Stock"]');
if (count($Team) >= 1) {
$fName = (string)$list->fName;
$ID = (string)$list->ID;
$sArray[0]['Name'] = $fName;
$sArray[0]['ID'] = $ID;
var_dump($sArray);
}
}
Upvotes: 1
Views: 2090
Reputation: 2069
The cleaner way of achieving this would be using an ID as a key of an array with user data. However, if you have no control over the array structure, then just extract the ID as any other value. This way your array will be lighter and easier to read. You also should create an output function, in case you need to create select box multiple times, so you can reuse it.
Example:
$user_list[$id]['first_name'] = 'John';
$user_list[$id]['last_name'] = 'Doe';
function select_user($user_list=[], $name='user_list', $multiple=''){
$select = '<select name="'.$name.'" '.$multiple.'>'; //use attribute multiple for multiselect
foreach($user_list as $user_key=>$user){
$user_ID = $user_key; // Option 1: $user_ID as Key
$user_ID = $user['ID']; // Option 2: $user_ID as Value
$select .= '<option value="'.$user_ID.'">'.$user['first_name'].' '.$user['last_name'].'</option>';
}
$select .= '</select>';
return $select;
}
Then simply run this function where you want it:
$user_list = get_users_from_DataBase(); // example function for getting all users from DB
echo select_user($user_list); // Simple Select box
echo select_user($user_list,'user_list[]','multiple'); // Select Box with custom array name[]* & with attribute "multiple"
If you need more explanation, you can Google for:
PHP Multidimensional Associative Arrays or read using the link below from official PHP documentation here: http://php.net/manual/en/language.types.array.php
Upvotes: 0
Reputation: 1113
You can use a foreach
loop and inside the loop, the current object ($value
) will contain ID
and Name
which you can use to build the dropdown
<?php
$sArray[0]['Name'] = 'John';
$sArray[0]['ID'] = '123';
$sArray[1]['Name'] = 'Sue';
$sArray[1]['ID'] = '234';
?>
<select class="form-select" multiple id="SelectName" name="name_list[]">
<?php
foreach ($sArray as $value) {
echo'<option value="'.$value['ID'].'" name="name_list[]">'.$value['Name'].'</option>';
}
?>
</select>
Upvotes: 0
Reputation: 128
As I can see, all you need is a foreach for you array. Take a look
foreach($sArray as $value){
echo '<option value="'.$value['ID'].'">'.$value['Name'].'</option>';
}
Upvotes: 1