Hello i have an Undefined Offset 0 . inside this code.
$q = mysql_query("SELECT * FROM category");
if (false === $q) {
echo mysql_error();
}
while ($r = mysql_fetch_row($q)) {
$names[$r[0]] = $r[2];
$children[$r[0]][] = $r[1];
}
function render_select($root=0, $level=-1) {
global $names, $children;
if ($root != 0)
echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
foreach ($children[$root] as $child)
render_select($child, $level+1);
}
echo '<select>';
render_select();
echo '</select>';
The exact line where the error is :
foreach ($children[$root] as $child)
render_select($child, $level+1);
This is for a selectbox with a tree format, i found this code in this question
More efficient hierarchy system
There is some ambiguity in your code here:
if ($root != 0)
echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
foreach ($children[$root] as $child)
render_select($child, $level+1);
If you are attempting to execute these three lines only if $root != 0, you will need to add curly braces like this:
if ($root != 0)
{
echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
foreach ($children[$root] as $child)
{
render_select($child, $level+1);
}
}
Otherwise, anytime render_select is called without a parameter (or with a first parameter value of '0') you will attempt to access the element of $children at array key '0'. As your error indicates, $children does not contain a value at that key.
Related
I've created this function below but i'm sure i'm doing it inefficiently ( less code ).
Is there a better way to do it?
function Select_Workorder_Type($Selected = NULL){
$q = DB::query("SELECT * FROM `tbl_workorders_type`");
$v = '<select id="Workorder_Type" name="Workorder_Type" class="Formfield_Select" required>';
foreach ($q as $r) {
if ($r['Workorders_Type_ID'] == $Selected) {
$s = 'selected="selected"';
}
$v .= '<option '.$s.' value="'.$r['Workorders_Type_ID'].'" >' . $r['Workorders_Type_Name'] . '</option>';
unset($s);
}
$v .= '</select>';
echo $v;
}
I have a random id generator which could be anywhere from 1 to x number of ids
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
I'm trying to go through each value and present it as an un-ordered list
<?php
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
$array = explode("|", $ranGen);
echo '<ul>' ;
foreach ($array as $value) {
echo '<li>' . trim($value) . '</li>';
}
echo '</ul>';
?>
I get the following results
<ul>
<li>IDENTIFIER </li>
<li> c0402347-8b93-49e4-991b-8213ea2921b1 </li>
<li> e8087fc5-ded7-43ab-858d-127fe23f90bc </li>
<li></li>
I do not want the last blank list - is there a way to do this
Test the value before you echo:
foreach ($array as $value) {
if( trim($value) != '') {
echo '<li>' . trim($value) . '</li>';
}
}
You have two options:
Check if the value is empty within the loop
Ignore the last part (if all of your input strings have that superfluous part at the end)
See this example:
<?php
$ranGen = "IDENTIFIER | c0402347-8b93-49e4-991b-8213ea2921b1| e8087fc5-ded7-43ab-858d-127fe23f90bc|" ;
$array = explode("|", $ranGen);
echo '<ul>';
foreach ($array as $value) {
// Option 1
if (trim($value) !== "") {
echo '<li>' . trim($value) . '</li>';
}
}
echo '</ul>';
// Option 2
$array = explode("|", $ranGen, -1);
echo '<ul>';
foreach ($array as $value) {
echo '<li>' . trim($value) . '</li>';
}
echo '</ul>';
?>
Replace this line : echo '<li>' . trim($value) . '</li>';
with this: echo trim($value) ? '<li>' . trim($value) . '</li>' : null;
Just filter it out:
$array = array_filter(explode("|", $ranGen));
I have the following code. I'm looking to do what's in the comment which is to populate a selectbox with a name of directories. How would I do that?
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
//extract directory names and populate selectbox
}
}
At maybe its simplest:
echo "<select name=\"something\">";
foreach(glob("repository/{$name[0]}/*", GLOB_ONLYDIR) as $dir) {
echo "<option>$dir</option>";
}
echo "</select>";
A quick and dirty solution, assuming result is the directory you want to show:
echo '<select id="directories" name="directories">';
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_dir($path . '/' . $result)) {
echo '<option value="' . $result . '">' . $result . '</option>';
}
}
echo '</select>';
Perhaps I've not understood the question but surely it's just:
<?php
$selectString = '<select>';
$path = 'repository/' . $name[0];
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' or $result === '..') {
continue;
}
if (is_dir($path . '/' . $result)) {
$selectString .= "<option>{$result}</option>";
}
}
$selectString .= "</select>"
You can then put $selectString wherever you need it.
i have this function for print categories in dropdown menu.
function Cat_Parent(){
$result = mysql_query("SELECT id, name, parent FROM cats ORDER BY name");
$items = array();
while($row = mysql_fetch_array($result))
{ $items[] = array('id' => $row['id'], 'label' => $row['name'], 'parent' => $row['parent']);
}
// Loop using references to build a tree
$childs = array();
foreach($items as &$item)
{
$childs[$item['parent']][] = &$item;
}
unset($item);
foreach($items as &$item)
{
if(isset($childs[$item['id']]))
{
$item['children'] = $childs[$item['id']];
}
}
// We now have a tree with 'children' key set on each node that has children
$tree = $childs[0];
// Prepare a template and recursive closure (note reference on &$print)
$tpl = '<option name="parent[]" value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
echo sprintf($tpl, $item['id'], $indent, $item['label']) . "\n";
if(isset($item['children']))
{
foreach($item['children'] as $child)
{
$print($child, $indent . '|--');
}
}
};
echo '<select name="parent"><option name="parent[]" value="0">------</option>';
// Call the function for each top-level node
foreach($tree as $row)
{
$print($row);
}
echo '</select>';
}
this worked but i need to selected value/id when$_GET['id'] = value Like This:
<select>
<option value="1">cat1</option>
<option selected value="2">cat2</option> <-- THIS Print Selected
<option value="3">subcat2</option>
<option value="4">cat3</option>
<option value="5">cat4</option>
</select>
in example $_GET['id'] = 2 , so selected option with value 2. how to select this?
Try this code in your script. Defines a variable with "selected" value if the value from GET is the same as the option value.
Notice the $sel variable.
// Prepare a template and recursive closure (note reference on &$print)
$tpl = '<option name="parent[]"%s value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
$sel = (isset($_GET['id']) && $_GET['id'] == $item['id']) ? 'selected' : ' ';
echo sprintf($tpl, $sel, $item['id'], $indent, $item['label']) . "\n";
if(isset($item['children']))
{
foreach($item['children'] as $child)
{
$print($child, $indent . '|--');
}
}
};
You're going to want to compare your values in the template like so:
$tpl = '<option name="parent[]" %s value="%s">%s %s</option>';
$print = function($item, $indent = '') use (&$print, $tpl)
{
echo sprintf($tpl, $item['id'] == 2 ? 'selected="selected"' : '', $item['id'], $indent, $item['label']) . "\n";
if(isset($item['children']))
{
foreach($item['children'] as $child)
{
$print($child, $indent . '|--');
}
}
};
I have an array: $categories = array("item1", "item2", "item3");
I also have three arrays: $item1Array = array("hi", "items");, $item2Array = array("hi", "items");, $item3Array = array("hi", "items");
I stated a foreach like this:
foreach ($categories as &$value) {
echo "<optgroup label='" . $value . "'>';
$nextArray = $value . "Array";
foreach($nextArray as &$nextValue) {
echo "<option value='" . $nextValue . "'>" . $nextValue . "</option>";
}
}
but it get an error Warning: invalid argument supplied for foreach().
Is there a way I can achieve this?
Yes, you could, by ${$nextArray}. but notice named variables is not good practice, you could use associative array instead.
And note you don't need to use reference in this case.
$categories = array("item1", "item2", "item3");
$item1Array = array("hi", "items");
$item2Array = array("hi", "items");
$item3Array = array("hi", "items");
foreach ($categories as $value) {
echo "<optgroup label='" . $value . "'>";
$nextArray = $value . "Array";
foreach(${$nextArray} as $nextValue) {
echo "<option value='" . $nextValue . "'>" . $nextValue . "</option>";
}
}
Of course, but as you can clearly see from the syntax highlighting of your post, you used a ' instead of a " at the end of the "optgroup" line.
Also, you could just use a nested array:
$categories = Array(
"item1"=>Array("hi","items"),
"item2"=>Array("hi","items"),
"item3"=>Array("hi","items"),
);
foreach($categories as $key=>$array) {
echo "<optgroup label='".$key."'>";
foreach($array as $value) {
echo "<option>".$value."</option>";
}
echo "</optgroup>";
}