php array iteration visually parent > child - php

managed to create array tree with childs. Looks like this.
Want to try printing it out with same levels.
<ul>
<li>cat1</li>
<ul><li>sub-cat</li>
<ul><li>subsub-cat</li>
<ul><li>subsubsub-cat</li>
<ul><li>subsubsubsub-cat</li></ul>
<li>subcub-cat2</li>
<ul></ul></ul></ul></ul>
<li>cat2</li>
</ul>
Need to use iterative angle. At least point me to to the right direction.
It doesn't have to be with UL LI, can be with -- -- or some sort similar, just need to print it out using iterative methods.

Try this piece of code:
<ul>
<?php parse($array); ?>
</ul>
<?php
function parse($array) {
foreach ($array as $value) {
?>
<li><?php echo $value->name; ?>
<?php if (!empty($value->child)) { ?>
<ul>
<?php parse($value->child); ?>
</ul>
<?php } ?>
</li>
<?php
}
}
?>
I've already faced this problem and used the same trick to handle.

It is not the exact solution but you can use something like this
foreach( $array as $item ){
echo $item['id'].'<br>';
while(isset($item['child'])){
$item = $item['child'];
echo $item['id'].'<br>';
}
}
here is the code sample that i generated
http://sandbox.onlinephpfunctions.com/code/068f8b2d79cb5e210a6ee4c8e14c1a8649ab0dea

Related

How to append php into an unordered list?

Hello how do you add php into an unordered list like below ?
<?php // Render the Category List
categoriesList(); ?>
<?php
into
<ul class="oi_smalldev_categories_list">
<li class="cat-item cat-item-7">
Coding
</ul>
Thank you.
This should work:
<ul>
<?php
$mycats = categoriesList();
foreach ($mycats as $cat) {
echo '<li>'. $cat. '</li>';
}
?>
</ul>
I'm guessing you want to take the results from the function and add them to your unordered list.
Basically, in your function you can create a string that is the HTML you want to output. In this case multiple tags.
You can then store it in a variable and call it with PHP further down the page.
Not the best way to handle it but I think this can do what you're after.
So, here's some pseudo code to handle that.
<?php // Render the Category List
$results = categoriesList();
?>
function categoriesList(){
return '<li>'.'category1'.'</li>'.'<li>'.'category2'.'</li>'.'<li>'.'category3'.'</li>';
}
<ul class="oi_smalldev_categories_list">
<?php echo $results; ?>
</ul>

How to add an item containing sub-items in each iteration of a loop?

Sorry if this seems very simple I'm rather new to php.
I'm drawing data from the database using a for each loop and want to add a list item for each result to a side bar while adding a new container div to the body.
What is the best way to code this?
Any help is much appreciated.
Thanks A
Edit...
My code so far...
<?php
$iidcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_iid_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
$gadcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_gad_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
?>
<ul>
<?php
foreach ( $iidcoversheets as $iidcoversheet )
{
?>
<li><?php echo $iidcoversheet->business name; ?></li>
<?php
}
?>
<?php
foreach ( $gadcoversheets as $gadcoversheet )
{
?>
<li><?php echo $gadcoversheet->business name; ?></li>
<?php
}
?>
</ul>
I understand how to add the <li> items but I'm strugging on how to add a div containing some html to another div not in the <ul> tag as it seems to me this will interrupt the code for the <ul>.
Any help would be really appreciated.
Thanks A
<div>
<ol>
<?php
foreach ($uls as $ul) {
?>
<li>
<?php echo $ul; ?>
</li>
<?php
}
?>
</ol>
</div>
Ok I came to the realisation that I could simply do another exact same loop later on in the page in the div I wanted but with different output. I was trying to do it all up front save the results and output them somewhere else, unnecessary.
Clearly I was having a daft moment earlier.
Thanks for all the input.
Regards
A

PHP separating multiple while loops in sequence

this is my first post here. I spent all of yesterday searching the net for an answer, but I think I've got a fairly unusual application of the while loop here so I didn't have much success.
Essentially I have a several categories, for example titled "Kosove" and "Shqiperi". Each of these has a number of sub-sections. Those sub-sections may or may not have content inside them.
I'm using while loops to display the sub-sections, providing they DO have content inside.
This is my code:
<ul>
<?php while(osc_has_list_regions($country = 'kv') ) { ?>
<li><?php echo osc_list_region_name(); ?> <em>(<?php echo osc_list_region_items(); ?>)</em></li>
<?php } ?>
</ul>
Notice the " $country = 'kv' " identifies the category as 'Kosove' in this case.
What is generated is:
Prishtine(1)
Vushtrri(1)
Which is fine.
The problem occurs when I want to also display the subsections for another category, e.g. Shqiperi. The code for this:
<ul>
<?php while(osc_has_list_regions($country = 'kv') ) { ?>
<li><?php echo osc_list_region_name(); ?> <em>(<?php echo osc_list_region_items(); ?>)</em></li>
<?php } ?>
</ul>
<br><br>
<ul>
<?php while(osc_has_list_regions($country = 'al') ) { ?>
<li><?php echo osc_list_region_name(); ?> <em>(<?php echo osc_list_region_items(); ?>)</em></li>
<?php } ?>
</ul>
Again notice the " $country = 'al' " identifies the category as 'Shqiperi' in this case.
What is generated is:
Prishtine(1)
Vushtrri(1)
Prishtine(1)
Vushtrri(1)
Which is not correct. The second part of the code has ignored the 'al' and just repeated the previous while loop content (i.e. the subsections for 'kv').
It SHOULD read:
Prishtine(1)
Vushtrri(1)
Berat(1)
As 'Berat' is the appropriate subsection for the 'Shqiperi' category.
If I have " $country = 'al' " as the first while loop condition, then it shows:
Berat(1)
Berat(1)
So obviously the order is important. It seems that the previous loop hasn't been properly closed.
I'm pretty new to PHP and I'm not even sure if I'm using the while loop appropriately here. Any suggestions how to fix this would be hugely appreciated.

PHP MySQL Order list

Can anyone help?
My code is like this:
<ol><li>{$student_Value}</li> <ol>'
it gives result:
1. Student101Name
1. Student102Name
1. Student103Name
I want something like:
Student101Name
Student102Name
Student103Name
Please help... Thank you!
Do not close the OL tag every time
OL = Ordered List
LI = List item
If you close and reopen the OL, it creates a new ordered list, and so restarting at 1.
<pre>
echo "<ol>";
foreach loop {
echo "<li>{$student_Value}</li>";
}
echo "</ol>";
</pre>
Good luck.
If you use MySQL to get the data you can add ORDER BY to the query to order the result!
Here more info how to use ORDER BY
And change the HTML Code to be:
<ol>
<li>$student_Value</li>
<li>$student_Value</li>
<li>$student_Value</li>
</ol>
PHP Code will be:
echo "<ol>";
echo " <li> $student_Value </li> ";//write all student values like this
//more student values
echo "</ol>";
It looks like your problem is more CSS related than PHP or SQL.
The reason is probably that your output is:
<ol><li>$student_Value</li></ol>
<ol><li>$student_Value</li></ol>
<ol><li>$student_Value</li></ol>
Each student should be a list item within the ordered list:
<?php
print "<ol>";
while ($row = $result->fetch_assoc())
{
print "<li>{$row['student']}</li>";
}
print "</ol>";
?>
You need something like that
<ol>
<?php
for($i=0; $i<count($studentValue); $i++)
{
?>
<li><?php echo $studentValue[$i]; ?></li>
<?php
}
?>
<ol>
I am giving you above example to tell you that under a 'ol' tag 'li' must be multiple to get your require result.

How do I list data from a specific database table as a list with links?

I'm working on my own simple Content Management System to implement on my projects for clients who want to do their own maintenance.
I'm going to contain only page titles and their content in a database table. All I want is to make a list which takes all the page titles and puts them into a <ul>. I believe I should use foreach, but I'm not too good at PHP, so I'd appreciate some help.
So how would I go about this?
<?php
$qry = mysql_query("SELECT * FROM links");
?>
<ul>
<?php while($row = mysql_fetch_array($qry)) { ?>
<li><?php echo $row['link_title']; ?></li>
<?php } ?>
</ul>
...in case your table with links has the colums 'link_url' and 'link_title'. But you get the idea, I guess.
If you have retrieved an array with the table's rows, you can probably do something like this:
The PHP-in-HTML version:
<ul>
<?php foreach( $pages as $page ): ?>
<li><?=$page['title']?></li>
<?php endforeach ?>
</ul>
The HTML-in-PHP version:
echo "<ul>";
foreach( $pages as $page ) {
echo "<li>$page['title']</li>";
}
echo "</ul>";
In both versions $pages is an array containing all the rows from the table.
Maybe you can adapt that to your needs?

Categories