whot
March 31, 2014, 4:07am
1
Hi, this is a code I seen from a post in 2011 and it works to display on my form. But I am trying to have the “option value” display the id in the array instead of the location name. Here is the original code:
$c = array (
array ( 'id' => 1001, 'parent' => 1001, 'show_type' => 'parent', 'location_name' => 'East Anglia' ) ,
array ( 'id' => 1081, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Bedfordshire' ) ,
array ( 'id' => 1002, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Cambridgeshire' ),
);
$newArray = array();
foreach($c as $item)
{
switch ($item['show_type'])
{
case 'parent':
isset($newArray[$item['id']]) || $newArray[$item['id']] = array();
$newArray[$item['id']]['location_name'] = $item['location_name'];
break;
case 'child':
isset($newArray[$item['parent']]) || $newArray[$item['parent']] = array();
isset($newArray[$item['parent']]['children']) || $newArray[$item['parent']]['children'] = array();
array_push($newArray[$item['parent']]['children'], $item['location_name']);
break;
}
}
<select name="test">
<?php
foreach($newArray as $items)
{
echo '<optgroup label="', $items['location_name'], '">';
foreach($items['children'] as $child)
{
echo '<option value="',[COLOR="#FF0000"] $child[/COLOR], '">', $child, '</option>';
}
echo '</optgroup>';
}
I am trying to get the red part of the code above for the option value to use the id values. Is this possible with this set of code?
You just need to build the array so the child’s ID is the key for that child’s location.
<?php
$c = array (
array ( 'id' => 1001, 'parent' => 1001, 'show_type' => 'parent', 'location_name' => 'East Anglia' ),
array ( 'id' => 1081, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Bedfordshire' ),
array ( 'id' => 1002, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Cambridgeshire' ),
array ( 'id' => 1005, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Cambridgeshire' ),
);
$newArray = array();
foreach($c as $item)
{
switch ($item['show_type'])
{
case 'parent':
isset($newArray[$item['id']]) || $newArray[$item['id']] = array();
$newArray[$item['id']]['location_name'] = $item['location_name'];
break;
case 'child':
isset($newArray[$item['parent']]) || $newArray[$item['parent']] = array();
isset($newArray[$item['parent']]['children']) || $newArray[$item['parent']]['children'] = array();
//array_push($newArray[$item['parent']]['children'], $item['location_name']);
$newArray[$item['parent']]['children'][$item['id']] = $item['location_name'];
break;
}
}
?>
<html>
<body>
<select name="test">
<?php
foreach($newArray as $items)
{
echo '<optgroup label="', $items['location_name'], '">';
foreach($items['children'] as $id => $child)
{
echo '<option value="', $id, '">', $child, '</option>';
}
echo '</optgroup>';
}
?>
</body>
</html>
whot
March 31, 2014, 11:02am
3
Drummin:
You just need to build the array so the child’s ID is the key for that child’s location.
<?php
$c = array (
array ( 'id' => 1001, 'parent' => 1001, 'show_type' => 'parent', 'location_name' => 'East Anglia' ),
array ( 'id' => 1081, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Bedfordshire' ),
array ( 'id' => 1002, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Cambridgeshire' ),
array ( 'id' => 1005, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Cambridgeshire' ),
);
$newArray = array();
foreach($c as $item)
{
switch ($item['show_type'])
{
case 'parent':
isset($newArray[$item['id']]) || $newArray[$item['id']] = array();
$newArray[$item['id']]['location_name'] = $item['location_name'];
break;
case 'child':
isset($newArray[$item['parent']]) || $newArray[$item['parent']] = array();
isset($newArray[$item['parent']]['children']) || $newArray[$item['parent']]['children'] = array();
//array_push($newArray[$item['parent']]['children'], $item['location_name']);
$newArray[$item['parent']]['children'][$item['id']] = $item['location_name'];
break;
}
}
?>
<html>
<body>
<select name="test">
<?php
foreach($newArray as $items)
{
echo '<optgroup label="', $items['location_name'], '">';
foreach($items['children'] as $id => $child)
{
echo '<option value="', $id, '">', $child, '</option>';
}
echo '</optgroup>';
}
?>
</body>
</html>
I am getting " " for option values when viewing the page source.
Sorry I didn’t notice you had commas in there instead of periods.
foreach($newArray as $items)
{
echo '<optgroup label="', $items['location_name'], '">';
foreach($items['children'] as $id => $child)
{
echo '<option value="' . $id . '">' . $child . '</option>';
}
echo '</optgroup>';
}
?>
There’s also an extra comma in the sample array.
$c = array (
array ( 'id' => 1001, 'parent' => 1001, 'show_type' => 'parent', 'location_name' => 'East Anglia' ),
array ( 'id' => 1081, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Bedfordshire' ),
array ( 'id' => 1002, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Cambridgeshire' ),
array ( 'id' => 1005, 'parent' => 1001, 'show_type' => 'child', 'location_name' => 'Cambridgeshire' )
);