0

chil_tablemain_table2
i have two tables which i attached. user can add N level question answers. i am using CI.

i am using this code for generating ul li. and my array which fetch result set is like this.... but not working ...
for working ul li function i have to pass this array. so how to convert
$tree = array('A', 'B', 'C' => array('CA', 'CB'), 'D');

Array
(
    [0] => Array
        (
            [pkQMainAnsId] => 5
            [parent_id] => 0
            [QuestionAnswerText] => This is i am gussing my favor
            [AnswerOption] => Sooby
        )

    [1] => Array
        (
            [pkQMainAnsId] => 6
            [parent_id] => 0
            [QuestionAnswerText] => This is i am gussing my favor
            [AnswerOption] => dooby 
        )

    [2] => Array
        (
            [pkQMainAnsId] => 7
            [parent_id] => 0
            [QuestionAnswerText] => This is i am gussing my favor
            [AnswerOption] => zoooby
        )

    [3] => Array
        (
            [pkQMainAnsId] => 8
            [parent_id] => 0
            [QuestionAnswerText] => This my favo hero
            [AnswerOption] => Davidw
        )

    [4] => Array
        (
            [pkQMainAnsId] => 9
            [parent_id] => 0
            [QuestionAnswerText] => This my favo hero
            [AnswerOption] => shavida
        )

)




public function olLiTree($tree) {
        $out = '<ul id="tree" class="treeview">';

        foreach($tree as $key => $value) {
            $out.= '<li>';

            if (is_array($value)) {
                $out.= $key . $this->olLiTree($value);
            } else {
                $out.= $value;
            }

            $out.= '</li>';
        }

        $out.= '</ul>';

        return $out;
    }
2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Biiim
0

can't read what the top table is called but something like this:

$Q = "SELECT * FROM `toptable`";
$R = mysql_query($Q);
if($R !== false){
    $ularray = array();
    while($row = mysql_fetch_assoc($R)){
        $ularray[$row['FKGMainId']][] = $row;
    }
}else{
    die('no data: '.mysql_error());
}

foreach($ularray as $k=>$v){
    echo "{$k}<ul>\r\n";
    foreach($v as $li){
        echo "<li>{$li['QuestionAnswerText']}</li>\r\n";
    }
    echo "</ul>\r\n";
}

note the php vars will be case sensitive to the field names in mysql, unless you specify names in the select.

If you want the bottom tables data in the same query do a left join:

$Q = "SELECT * FROM `toptable` a LEFT JOIN `bottable` b ON a.FKGMainId = b.PKGMainId";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.