my categories table
ID,PARENT,NAME,ORDER
ul > li > li ... like this i want sort my data with php function.
first i want take all in array. after use function.
$query = mysql_query("SELECT ID,PARENT,NAME,ORDER FROM categories");
$category = array();
if(mysql_num_rows($query)>0){
while ($rs= mysql_fetch_assoc($query))
$category[$rs['PARENT']][$rs['ORDER']] = array('id'=>$rs['ID'],'name'=>$rs['NAME']);
}
After how can i print according sortable menu ?
<ul>
<li>menu1
<ul>
<li>menu1a</li>
<li>menu1b</li>
</ul>
</li>
<li>menu2
<ul>
<li>menu2a</li>
<li>menu2b</li>
</ul>
</li>
<li>menu3
<ul>
<li>menu3a<ul>
<li>menu3a_a</li>
<li>menu3a_b</li>
</ul></li>
<li>menu3b</li>
</ul>
</li>
</ul>
I think you have to use recurse(0, $category); instead of recurse(0, $categories);
and for every category you have to call this function.
Related
i am literally stuck with getting these ul/li elements in one row.
Only first li has class, others are in another ul but without class or id. i tried multiple searches and loops, but can't get it right.
Below results returns too many rows where elements don't fit together.
Can someone help me?
idealy the result from below example would be:
'part', 'desc', 'qty'
'106128-001','LBL, BLNK,THERMAL PRINT','1'
'106128-004','LBL, BLNK THERMAL,RECT 2.40x4','1'
HTML example
<div id='tab1' class="section tab" data-position="auto" style="max-height:none;">
<ul class="links">
<li id="innertab1">106128-001</li>
</ul>
<ul id="combom0" class="tab1 cols2 compare">
<li style="border: none;">
<div>
<ul>
<li><strong>Description: </strong>LBL, BLNK,THERMAL PRINT</li>
<li><strong>Qty: </strong>1</li>
</ul>
</div>
</li>
</ul>
<ul class="links">
<li id="innertab1">106128-004</li>
</ul>
<ul id="combom1" class="tab1 cols2 compare">
<li style="border: none;">
<div>
<ul>
<li><strong>Description: </strong>LBL, BLNK THERMAL,RECT 2.40x4</li>
<li><strong>Qty: </strong>1</li>
</ul>
</div>
</li>
</ul>
My poor attempt with trying to count rows:
$search = $html->find('div[id=tab1]',0);
$i=0;
$alles=array();
foreach ($search->find('* li') as $test){
if(!empty($test->innertext)){
if($i>3){$i=0;}
$i++;
if($i==1){
echo $alles_li['part'] = strip_tags($test->innertext);
}
if($i==2){
// don't need this, bad stripping
strip_tags($test->innertext);
}
if($i==3){
$alles_li['desc'] = strip_tags($test->innertext);
}
if($i==4){
$alles_li['qty'] = strip_tags($test->innertext);
}
}
// if less than 3 elements don't add
if(count($alles_li)==3){
$alles[]=$alles_li;
}
}
In a typical HTML as
<ol>
<li>
<span>parent</span>
<ul>
<li><span>nested 1</span></li>
<li><span>nested 2</span></li>
</ul>
</li>
</ol>
I try to get the contents of <li> elements but I need to get the parent and those nested under ul separately.
If go as
$ols = $doc->getElementsByTagName('ol');
foreach($ols as $ol){
$lis = $ol->getElementsByTagName('li');
// here I need li immediately under <ol>
}
$lis is all li elements including both parent and nested ones.
How can I get li elements one level under ol by ignoring deeper levels?
There are two approaches to this, the first is how you are working with getElementsByTagName(), the idea would be just to pick out the first <li> tag and assume that it is the correct one...
$ols = $doc->getElementsByTagName('ol');
foreach($ols as $ol){
$lis = $ol->getElementsByTagName('li')[0];
echo $doc->saveHTML($lis).PHP_EOL;
}
This echoes...
<li>
<span>parent</span>
<ul>
<li><span>nested 1</span></li>
<li><span>nested 2</span></li>
</ul>
</li>
which should work - BUT is not exact enough at times.
The other method would be to use XPath, where you can specify the levels of the document tags you want to retrieve. This uses //ol/li, which is any <ol> tag with an immediate descendant <li> tag.
$xp = new DOMXPath($doc);
$lis = $xp->query("//ol/li");
foreach ( $lis as $li ) {
echo $doc->saveHTML($li);
}
this also gives...
<li>
<span>parent</span>
<ul>
<li><span>nested 1</span></li>
<li><span>nested 2</span></li>
</ul>
</li>
i am new to this,so can one give reference to create multilevel drop down list.
As explain belove.
for example,
I have to create two dropdown list one is category and another is sub category.
When i am select one field in category then sub categoey will give me only those field which is sub part of category..
if i am select category1 then
category-1----->in subcategory dropdown sub1,sub2 are display
category-2----->sub3,sub4,sub5
like wise....
Here is simple solution.
// HTML
<ul>
<li>
A >
<ul>
<li>A1 >
<ul>
<li>A1A >
<ul>
<li>A1A1</li>
<li>A1A2</li>
</ul>
</li>
<li>A1B</li>
</ul>
</li>
<li>A2</li>
</ul>
</li>
<li>B</li>
</ul>
// CSS
ul li > ul{
display:none;
}
ul li:hover >ul{
display:block;
}
jsfiddle example
HTML:
<ul>
<li><a></a>
<ul>
<li></li>
<li></li>
</ul>
</li>
<li>
...
</li>
</ul>
For parent ul:first-of-type, what would be the selector for it's (direct) child li elements, in order to parse the descendant li elements separately?
In Jquery you can simply use this selector : ul > li
Update:-
Using Simple DOM:-
<ul class="listitems">
<li><a></a>
<ul>
<li></li>
<li></li>
</ul>
</li>
<li>
...
</li>
</ul>
Simple HTML Dom code to get just the first level li items:
$html = file_get_html( $url );
$first_level_items = $html->find( '.listitems', 0)->children();
foreach ( $first_level_items as $item ) {
... do stuff ...
}
I have an array filled with "Category" objects, each one containing an id and a parent_id value. They're accessed like this:
$category->get("id"); //gets category id
$category->get("parent_id"); gets parent category id
I want to make a HTML UL list like this:
<ul>
<li>Category</li>
<li>Category
<ul>
<li>Child Category</li>
<li>Child Category</li>
<li>Child Category
<ul>
<li>Child Category</li>
<li>Child Category</li>
<li>Child Category</li>
</ul>
</li>
</ul>
</li>
<li>Category</li>
<li>Category</li>
<li>Category</li>
<li>Category</li>
</ul>
What sort of loop must be done to generate this?
The MPTT tree logic might be useful
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
without knowing what library you are using, it will be more of a pseudo code than working code but you should get the idea of how to use recursion to get the tree
first retrieve the main category (you could set that for example as category 0 is the top category. then loop through all items and fetch children. If node has children, call itself recursively
showCategory($rootcategory)
function showCategory($category) {
$children=fetchChildren();
if($children) //if category has children
{
echo('<ul>');
foreach($children as $child) {
showCategory($child);
}
echo('</ul>');
}
else {
echo('<li>' . $child['title'] . '</li>');
}
}