Make Such Array For TreeView in PHP Where parent chid hierarchy directly did not exists -
i have function gives me treelist if give him $treedata = array(id=1,name=abc, parentid=null). have table structure there no problem provide such array. problem starts when have no such structure in table client want treelist such structure parent child hierarchy not directly exists. table structure have
- types
- subjects
- topics
- lessons
- lessontypes
in lessontypes have typeid, subjectid, topicid, lessonid
i tried make array function not quite please see code:
private function maketreedataarray(){ $treedataarr = array(); $resourcetesttypesarr = array(); $topicarr = array(); $lessonarr = array(); $testtypeidprefix = 'testtype@'; $subjectidprefix = 'subject@'; $topicidprefix = 'topic@'; // $model = new resourcetesttype(); //get testtypes treedataarr $model = new option(); $testtypes = $model->where('type','testtype')->get(); foreach ($testtypes $k => $tt) { $testtypeid = $testtypeidprefix . $tt->id; $treedataarr[] = array('id' => $testtypeid, 'name'=>$tt->option, 'parentid' => null); $resourcetesttypesarr[] = $tt->resourcetesttypes()->get(); } // return ($resourcetesttypes); //****get subjects of testtype foreach ($resourcetesttypesarr $i =>$rtts) { foreach ($rtts $j => $rtt) { $subjectid = $subjectidprefix . $rtt->subjectid; $resourceid =$rtt->resourceid; $subjectname = $rtt->subject()->first()->name; $testtypeidasparentid = $testtypeidprefix.$rtt->testtypeid; $treedataarr[] = array('id' => $subjectid, 'name'=>$subjectname, 'parentid' => $testtypeidasparentid); //*** create dataset topics $topicid = $topicidprefix.$rtt->topicid; $topicidwithoutprefix = $rtt->topicid; $topicname = option::getoptionbyid('topics',$topicidwithoutprefix)->display; $topicarr[] = array('topicid' => $topicid, 'topicname'=>$topicname, 'subjectid' => $subjectid, 'lessonid' => $resourceid); } } // return ($topicarr); //get topics of subject foreach ($topicarr $i =>$t) { $topicid = $t['topicid']; $lessonid = $t['lessonid']; $treedataarr[] = array('id' => $topicid, 'name'=> $t['topicname'], 'parentid' => $t['subjectid']); $lessonname = lesson::find($lessonid)->name; $lessonarr[] = array('lessonid' => $lessonid, 'lessonname'=>$lessonname, 'topicid' => $topicid); } // return ($lessonarr); //get lessons of topics foreach ($lessonarr $i =>$l) { $treedataarr[] = array('id' => $l['lessonid'], 'name'=> $l['lessonname'], 'parentid' => 'topic@'.$l['topicid']); } $result = array_unique($treedataarr,sort_regular); // $result = array_unique($treedataarr); return $result; }
and result of function is:
- -> a-type
- -> a-subject of a-type
- -> a-topic of a-subject
- -> a-lesson of a-topic
- -> b-topic of a-subject
- -> a-lesson of b-topic
- -> b-type
- -> a-subject of a-type
notice: a-subject appear again here not correct should appear inside a-type happens others also. please me what's wrong code. in advance
Comments
Post a Comment