Outputting array contents as nested list in PHP - php

I have the array array ( [0] => array(1,2,3,4,5) [1] => array(6,7,8,9,10)) and I would like to display it like this:
<ul>
<li>
<a href=""/>FIRST ELEMENT OF THE array ==> 1</a>
<a href=""/>2ND ELEMENT OF THE TAB ==> 2</a>
<a href=""/>3THIRD ELEMENT==> 3</a>
<a href=""/>FORTH ELEMENT OF THE TAB ==> 4</a>
<a href=""/>FIFTH ELEMENT==> 5</a>
</li>
<li>
<a href=""/>6th ELEMENT==> 6</a>
<a href=""/>7th ELEMENT OF THE TAB ==> 7</a>
<a href=""/>8th ELEMENT==> 8</a>
<a href=""/>9th ELEMENT OF THE TAB ==> 9</a>
<a href=""/>10th ELEMENT OF THE TAB ==> 9</a>
</li>
</ul>
How can I achieve this in PHP? I am thinking of creating a sub array with array_slice.

Updated to take into account your actual array structure
Your solution is a simple nested foreach.
$tab = array(array(1,2,3,4,5), array(6,7,8,9,10));
echo '<ul>';
foreach ($tab as $chunks) {
echo '<li>';
foreach($chunks as $chunk) {
echo '' . $chunk . '';
}
echo '</li>';
}
echo '</ul>';

try
echo "<ul>";
$i=0;
$theCount = count($tab);
while($i<$theCount){
echo "<li>";
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
$i++;
echo " <a href=""/>FIRST ELEMENT OF THE TAB ==> {$tab[$i]}</a>";
echo "</li>";
$i++;
}
echo "</ul>";

Here is another way to do this (demo here):
<?php
$tab = array(1,2,3,4,5,6,7,8,9,10);
//how many <a> elements per <li>
$aElements = 2;
$totalElems = count($tab);
//open the list
echo "<ul><li>";
for($i=0;$i<$totalElems;$i++){
if($i != 0 && ($i%$aElements) == 0){ //check if I'm in the nTh element.
echo "</li><li>"; //if so, close curr <li> and open another <li> element
}
//print <a> elem inside the <li>
echo "<a href =''>".$tab[$i]."</a>";
}
//close the list
echo "</li></ul>";
?>
Tip explanation: $i%n (mod) equals 0 when $i is the nTh element (remainder of division is 0)
EDITED: made a general solution

<?php
for($i = 0 ; $i < count($tab) ; $i += 2) {
echo "<a href>" . $tab[$i] . "</a>";
echo "<a href>" . $tab[$i+1] . "</a>";
}
?>
Like that.

try this:
$sections = array_chunk(array(1,2,3,4,5,6,7,8,9,10), 2);
echo '<ul>';
foreach($sections as $value)
{
echo '<li>';
echo '<a href=""/>'.$value[0].' ELEMENT OF THE TAB ==> '.$value[0].'</a>';
echo '<a href=""/>'.$value[1].' ELEMENT OF THE TAB ==> '.$value[1].'</a>';
echo '</li>';
}
echo '</ul>';

Related

Is it possible to close </ul> and add new <ul> in the loop?

I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</ul>';
}
?>

while loop first output a different class

I use a slider from codyhouse.co and integrate it in wordpress. My goal is to count the slides and output a navigation where the first navigation bullet gets the class selected.
The part of counting slides and outputting the navigation works. But i don't know how to to setup the code to get the first bullet in the navigation the class selected. This is my post loop:
while ($my_query->have_posts()) : $my_query->the_post();
<li if($counter==0) { echo 'class="selected"'; $counter++; }>
<div class="inner-hero">
$titleslider = get_post_meta($post->ID, 'textslider', true); ?>
<h2>echo $titleslider; ?></h2>
</div>
the_post_thumbnail();
</li>
$slider = $my_query->post_count;
if ($slider > 1) {
while ($i < $slider) {
$output .= "<li class=''>" ."<a href='#0'>" . "</a></li>";
$i++;
}
}
endwhile;
While loop in the post loop:
while ($i < $slider) {
$output .= "<li class=''>" ."<a href='#0'>" . "</a></li>";
$i++;
I use the class selected also for the slides, that works fine.
So what i did is, count the slides, check if there are more than one slide, if this is true show the navigation. But my next goal is to get the first li in the navigation the class selected in the while loop.
naviagtion outside the post loop.
<ul class="cd-slider-navigation">
echo $output;
</ul>
Suppose you have defined class 'test1' for the selected one and class 'test' for the other links.
Before your while loop starts, you'll have to define a variable named $j equal to 1.
Then the code goes here,
$j=1;
while ($i < $slider) {
if($j==1)
$output .= "<li class='test".$j."'>" ."<a href='#0'>" . "</a></li>";
else
$output .= "<li class='test'>" ."<a href='#0'>" . "</a></li>";
$i++;
$j++;
}

foreach loop where is the place of the closing ul?

I would like to display all existing categories and all posts.
I wrote a foreach loop, but I don't know where is the place of the closing ul tag.
My code:
<?php
$current_cat = 0;
$curr_nb_item = 0;
foreach($blog_widget as $row)
{
if($current_cat != $row->category_id)
{
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
else {
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
}
?>
Now I got this result in html:
<ul>1
<li>
cat 1 post n
</li>
<li>
cat 1 post h
</li>
<ul>2
<li>
cat 2 post x
</li>
<li>
cat 2 post y
</li>
I tried a lot of variation without result. I hope somebody could help for me. Many thanks.
This may not be the cleanest, but it should work. Modify the top of your if statement to look like this:
if($current_cat != $row->category_id)
{
if ($row != $blog_widget[0])
{
echo "</ul>";
}
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
// etc...
And then after the closing brace for your foreach loop:
echo "</ul>";

Make a menu with the id of the sections with php?

I'm very noob in php.
I want create a page with menu dynamically.
If in my code there are:
<section id="one" >...</section>
<section id="two" >...</section>
<section id="three" >...</section>
I want generate this menu:
<li ><a id="menu-one" href="#one"> <span> One </span></a></li>
<li ><a id="menu-two" href="#two"> <span> Two </span></a></li>
<li ><a id="menu-three" href="#three"> <span> Three </span></a></li>
It's very easy but i've tried without succeed in this way:
$html = file_get_html('http://mysite.it');
foreach($html->find('section') as $element){
$idSection=$element->id;
$menuElement .= '<li><a id="menu-'.$idSection.'" href="#'.$idSection.'" ><span>'.$idSection.'</span></a></li>';
echo '<ul>'.$menuElement.'</ul>';
}
EDIT: this could generate an array of section but doesn't work. Is it wrong?
$els = $document->getElementsByTagName('section');
Thanks!
Now you have ONE ul and many li
echo '<ul>'
foreach($html->find('section') as $element){
$idSection=$element->id;
$menuElement .= '<li><a id="menu-'.$idSection.'" href="#'.$idSection.'" ><span>'.$idSection.'</span></a></li>';
echo $menuElement;
}
echo '</ul>'
I think you want something like this ...
$sections = array(
'one' => 'Something',
'two' => 'Something Else',
'three' => 'Something Awesome'
);
// output <section>s
foreach ($sections as $k=>$v) {
echo '<section id="'. $v .'">'. $v .'</section>';
}
// output menu
foreach ($sections as $k=>$v) {
echo '<li ><a id="menu-'. $k .'" href="#'. $k .'"> <span>'. $v .'</span></a></li>';
}
Try this an little change to neokio code the gives what you're looking gor:
$sections = array('one','two','three');
// output <section>s
foreach ($sections as $section) {
echo '<section id="'. $section .'"><li ><a id="menu-'. $section .'" href="#'. $section .'"> <span>'. $section .'</span></a></li></section>';
}

Tabs to navigate page by page

I use bootstrap with JQuery and I would like to use tabs to navigate page by page.
Indeed, I have some results in a table. With SQL, I know how to do this by using LIMITE 1,10 for example. And by using a $_GET argument to navigate page by page...
But here, I would like use tabs instead.
Here is my code:
echo '<div class="tab-content">';
echo '<div class="tab-pane fade in active" id=1>';
while($row = $selectAllUsersM->fetch(PDO::FETCH_OBJ)){
$isActive = ($row->Activation) ? 'Desable' : 'Enable';
echo "<tr>"; echo '<td>'.$row->Firstname.'</td>';
echo '<td>'.$row->Name.'</td>';
echo '<td>'.$row->Nickname.'</td>';
echo '<td>'.$row->Email.'</td>';
echo '<td>'.timeToDDMMYYYY($row->DateInscription).'</td>';
echo '<td><a href="?param='.$row->Activation.'&id='.$row->IdUser.'" title='.$isActive.'>'.isActive($row->Activation).'</a></td>';
echo "</tr>";
}
echo '</div>';
echo '</div>';
Here is the example for pagination:
<ul class="pagination">
<li>«</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>»</li>
</ul>
I don't know how can I display 10 results on each page. Then, if 11 results, create a new page and a new tab. I hope you know what I mean.
Thank you for your help ;)
Well, I solved this by using a POST in Ajax.
Function to display results
<script>
function displayPilotesF(pageF) {
$.post("pilotesF.php",
{ page : pageF},
function(data){
//alert("Data Loaded: " + data);
$("#pilotesF").html(data);
}
);
}
</script>
I display the results in a div:
<div id="pilotesF"></div>
And on my page "pilotesF.php":
if(isset($_POST['page'])){
$page = $_POST['page'];
$selectAllUsersF= getAllInfoOfAllUsersF($page, nbPiloteFPerPage);
$selectAllUsersF->execute();
echo "<table class='table table-hover table-condensed'>";
echo "<th>Firstname</th>";
echo "<th>Name</th>";
echo "<th>Nickname</th>";
echo "<th>Email</th>";
echo "<th>Registration</th>";
echo "<th>State account</th>";
while($row = $selectAllUsersF->fetch(PDO::FETCH_OBJ)){
$isActive = ($row->Activation) ? 'Disable' : 'Enable';
echo "<tr>";
echo '<td>'.$row->Firstname.'</td>';
echo '<td>'.$row->Name.'</td>';
echo '<td>'.$row->Nickname.'</td>';
echo '<td>'.$row->Email.'</td>';
echo '<td>'.timeToDDMMYYYY($row->DateInscription).'</td>';
echo '<td><a href="?param='.$row->Activation.'&id='.$row->IdUser.'" title='.$isActive.'>'.isActive($row->Activation).'</a></td>';
echo "</tr>";
}
echo "</table>";
}
And for my pagination:
$nbPages= ceil($nbPiloteF/ nbPiloteFPerPage);
echo '<div class="text-center">';
echo '<ul class="pagination" id="tabs2">';
/***********************************/
for ($i = 1 ; $i <= $nbPages; $i++){
if($i == 1)
echo '<li class="active"><a data-toggle="tab" OnClick="displayPilotesF('.$i.');">'.$i.'</a></li>';
else
echo '<li><a data-toggle="tab" OnClick="displayPilotesF('.$i.');">'.$i.'</a></li>';
}
/***********************************/
echo '</ul>';
echo '</div>';
Maybe it could help some of you guys,
Thank you anyway, have a night! :)

Categories