1

I have an array like this (stored in the $optDir variable):

Array
(
    [0] => Array
        (
            [id] => 8
            [title] => Atasan Pria
            [idparent] => 5
        )

    [1] => Array
        (
            [id] => 5
            [title] => Fashion
            [idparent] => 0
        )

    [2] => Array
        (
            [id] => 7
            [title] => Motor
            [idparent] => 0
        )

    [3] => Array
        (
            [id] => 6
            [title] => Mobil
            [idparent] => 0
        )

    [4] => Array
        (

            [id] => 9
            [title] => Hem
            [idparent] => 8
        )

)

And i have PHP Codes like this :

function optCatAds($pos=0, $level=0){                   
        global $optDir;  $opt = "";
        $n = count($optDir);
        for($i=0;$i<$n;$i++){           
            $l = $level*3;
            $sign=""; for ($z=0;$z<=$l;$z++){$sign.="&nbsp;";}
            if($optDir[$i]['idparent']==$pos){
            $opt .= '<option value="'.$optDir[$i][id].'">'.$sign.$optDir[$i][title].'</option>';
            optCatAds($optDir[$i][id], $level + 1);
            }
        }                               
        return $opt;
    }

When i call the optCatAds function, give output like below:

<option value="5">Fashion</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>

But, I want to made output like below :

<option value="5">Fashion</option>
<option value="8">  Atasan Pria</option>
<option value="9">    Hem</option>
<option value="6">Mobil</option>
<option value="7">Motor</option>

Conditions :

  1. Fashion, Mobil, Motor <-- parent

  2. Fashion have child Atasan Pria

  3. Atasan Pria have child Hem

Can someone help me? Thank to your help.

1
  • this order is based on what ??You can do it manually without loop Commented Dec 17, 2013 at 9:54

4 Answers 4

1

This will work fine for you

$optDir = Array(Array("id"=>8,"title"=>"Atasan Pria","idparent"=>5),
                Array("id"=>5,"title"=>"Fashion","idparent"=>0),
                Array("id"=>7,"title"=>"Motor","idparent"=>0),
                Array("id"=>6,"title"=>"Mobil","idparent"=>0),
                Array("id"=>9,"title"=>"Hem","idparent"=>8)
                );

function optCatAds($pos=0, $level=0)
{                   
    global $optDir;$opt;
    for($i=0;$i<count($optDir);$i++)
    {           
        $l = $level*3;
        $sign="";
        for ($z=0;$z<$l;$z++){$sign.="&nbsp;";}
        if($optDir[$i]['idparent']==$pos)
        {
            $opt .= '<option value="'.$optDir[$i]['id'].'">'.$sign.$optDir[$i]['title'].'</option>';
            $opt .= optCatAds($optDir[$i]['id'], $level + 1);
        }
    }                               
    return $opt;
}

$res = optCatAds($pos=0, $level=0);
echo "<select>{$res}</select>";
Sign up to request clarification or add additional context in comments.

2 Comments

i just added your level spaces too :)
u just forgott to add the $opt .= to your recursiv function line 12 and of course some ' in your code
0

Can you try foreach instead of for loop ,

foreach($optDir as $k=>$value){     
    $opt .= '<option value="'.$value->id.'">'.$sign.$value->title.'</option>';      
}

1 Comment

I want the order to follow the parent-child structure
0

Replace $optDir[$i][title] with $optDir[$i]['title']

and $optDir[$i][id] with $optDir[$i]['id']

Here you are making recursive call so you can not get complete

function optCatAds($pos=0, $level=0){                   
        global $optDir;  $opt = "";
        foreach($optDir as $each){
            if($each['id']==8) $level = 2;
            if($each['id']==9) $level = 3;
            $l = $level*3;
            $sign=""; for($z=0;$z<=$l;$z++){$sign.="&nbsp;";}
            if($each['idparent']==$pos){
                $opt .= '<option value="'.$each['id'].'">'.$sign.$each['title'].'</option>';
            }
        }                               
        return $opt;
    }

2 Comments

results remain the same. <option value="5">Fashion</option> <option value="6">Mobil</option> <option value="7">Motor</option>
I think it's less effective, how if there are a lot of id's?
0
<?php

$optDir = Array(
    Array(
        'id' => 8,
        'title' => 'Atasan Pria',
        'idparent' => 5
    ),
    Array(
        'id' => 5,
        'title' => 'Fashion',
        'idparent' => 0
    ),
    Array(
        'id' => 7,
        'title' => 'Motor',
        'idparent' => 0
    ),
    Array(
        'id' => 6,
        'title' => 'Mobil',
        'idparent' => 0
    ),
    Array(
        'id' => 9,
        'title' => 'Hem',
        'idparent' => 8
    )
);

function getOptionsRecursive($idParent = 0, $spaces = '') {
    global $optDir;
    $s = '';
    for($i = 0 ; $i < sizeof($optDir) ; $i++) {
        $current = $optDir[$i];
        if($current['idparent'] == $idParent) {
            $s .= '<option value="'.$current['id'].'">'.$spaces.$current['title'].'</option>';
            $s .= getOptionsRecursive($current['id'], $spaces.' ');
        }
    }
    return $s;
};

echo getOptionsRecursive();

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.