hello first sorry for my bad english.
here i have some problem with PHP. how i can create / print (if else) statement from ul and li tag.
I have a multidimentional array like this:
$arr = array(
array('id' => 1, 'parent' => 0, 'data' => '1.1');
array('id' => 2, 'parent' => 0, 'data' => '1.2');
array('id' => 3, 'parent' => 1, 'data' => '1.1.1');
array('id' => 4, 'parent' => 1, 'data' => '1.1.2');
array('id' => 5, 'parent' => 2, 'data' => '1.2.1');
array('id' => 6, 'parent' => 2, 'data' => '1.2.2');
array('id' => 7, 'parent' => 5, 'data' => '1.2.1.1');
array('id' => 8, 'parent' => 5, 'data' => '1.2.1.2');
);
Then from the array above i need some code that should echo / print statement like this (IN STRING FORMAT TO SAVE IN A VARIABLE):
$str ='if (1.1) {
if (1.1.1) {
} else {
}
} else {
if (1.2.1) {
if (1.2.1.1) {
}
else {
}
} else {
}
}';
appreciate for your help... :)
You have to make condition with variable like this:
if ($val == "1.1") {
//do the child if
}
PHP will not execute this:
if (1.1) {
}
First Step, make new array that's containing child node with key from parent id..
$new_arr = array();
foreach($arr as $a){
$new_arr[$a['parent']][] = $a;
}
After that, you can access new_array with recursively function..
function rec_child($arr){
global $new_arr;
foreach($arr as $a){
echo "<li>".$a['data']."</li>";
if(!empty($new_arr[$a['id']])){
echo "<ul>";
rec_child($new_arr[$a['id']]);
echo "</ul>";
}
echo "";
}
}
So, call rec_child function like this..
echo "<ul>";
rec_child($new_arr[0]);
echo "</ul>";
Hope this help.