php - Foreach - Wrapping <li> </li> with two results inside, then repeating. - php

Working with php -
Im trying to run a "foreach" on an array, but i want to wrap every two results in li tags. Output will look like this.
<li>
<div>result 1</div>
<div>result 2</div>
</li>
<li>
<div>result 3</div>
<div>result 4</div>
</li>
<li>
<div>result 5</div>
<div>result 6</div>
</li>
how do i go about doing this?
Thanks!

$chunks = array_chunk($arr, 2);
foreach ($chunks as $chunk) {
// $chunk could have either 2 elements, or just one on the last iteration on an array with odd number of elements
echo '<li>';
foreach ($chunk as $value) {
echo '<div>' . $value . '</div>';
}
echo '</li>';
}

$results = array(1, 2, 3, 4, 5, 6);
echo "<li>";
foreach($results as $pos => $result) {
if ($pos > 2 && $pos % 2 == 0) {
echo "</li>\n<li>";
}
echo "<div>result $result</div>";
}
echo "</li>";
or more simply:
$results = array(1, 2, 3, 4, 5, 6);
$max = count($results);
for($i = 0; $i < $max; $i++) {
echo "<li>";
echo "<div>result " . $results[$i] . "</div>";
$i++;
echo "<div>result " . $results[$i] . "</div>";
echo "</li>";
}

$count = 0;
foreach ($array as $key=>$value) {
++$count;
if ($count == 1) {
echo "<li>";
echo "<div>" . $value."</div>";
} else {
echo "<div>" . $value."</div>";
echo "</li>";
$count = 0;
}
}

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>';
}
?>

Unable to display the exact HTML code with PHP for loop

for ($i = 0; $i < count($item); $i++) {
echo '<ul class="list1">';
if ($i <= 1) {
echo '<li>'.$item[$i].'</li>';
}
echo '</ul>';
echo '<ul class="list2">';
if ($i >= 2 && $i <= 7) {
echo '<li>'.$item[$i].'</li>';
}
echo '</ul>';
}
I tried to display the following HTML with only php(above code) however, it seems like it's not working as expected. How can I display the exact same thing with php?
<ul class="list1">
<li>
<? echo $item[0];?>
</li>
<li>
<? echo $item[1];?>
</li>
</ul>
<ul class="list2">
<li>
<? echo $item[2];?>
</li>
<li>
<? echo $item[3];?>
</li>
<li>
<? echo $item[4];?>
</li>
<li>
<? echo $item[5];?>
</li>
<li>
<? echo $item[6];?>
</li>
<li>
<? echo $item[7];?>
</li>
</ul>
do something like this,
<?php
$item = ['1','2','3','4','5','6','7','8'];
for ($i = 0; $i < count($item); $i++) {
if ($i < 1) {
echo '<ul class="list1">';
}
if ($i <= 1) {
echo '<li>'.$item[$i].'</li>';
}
if ($i == 1) {
echo '</ul>';
}
if ($i == 2) {
echo '<ul class="list2">';
}
if ($i >= 2 && $i <= 7) {
echo '<li>'.$item[$i].'</li>';
}
if ($i == 7) {
echo '</ul>';
}
}
?>
this is the simplified syntax for the expected output
echo '<ul class="list1">';
for ($i = 0; $i < count($item); $i++) {
echo '<li>'.$item[$i].'</li>';
if ($i == 1) {
echo '</ul><ul class="list2">'';
}
}
echo '</ul>';
Try use two for loop, first loop items from 0 to 1 in "list1", second loop items form 2 to 7 in "list 2"
echo '<ul class="list1">'
for ($i = 0; $i <= 1; $i++) {
echo '<li>'.$item[$i].'</li>'
}
echo '</ul>';
echo '<ul class="list2">'
for ($i = 2; $i <= 7; $i++) {
echo '<li>'.$item[$i].'</li>'
}
echo '</ul>';

How can i detect category page on Joomla?

I want to detect virtuemart category page in mod_breadcrumbs.
Breadcrumbs is loading all page and i just wanted write a message in category page.
My breadcrumbs code:
<div class="breadcrumbs<?php echo $moduleclass_sfx; ?>">
<?php
echo '<ul>';
for ($i = 0; $i < $count; $i ++) {
// If not the last item in the breadcrumbs add the separator
if ($i < $count -1) {
if (!empty($list[$i]->link)) echo '<li>'.$list[$i]->name.'</li>';
else echo '<li class="pathway">' . $list[$i]->name . '</li>';
if($i < $count -2) echo ' <li class="pathway separator">></li> ';
} else if ($params->get('showLast', 1)) { // when $i == $count -1 and 'showLast' is true
if($i > 0) echo ' <li class="pathway separator">></li> ';
echo '<li class="pathway">' . $list[$i]->name . '</li>';
}
}
echo '</ul>';
?>
</div>
Here's how you can check if you're on a category view:
$appInput = Jfactory::getApplication()->input;
if($appInput->getCmd('option')=='com_content' && $appInput->getCmd('view')=='category' ){
//add your code here
}

Creating row using php loop

I want to create a row if columns greater than 3 using PHP loop because i am using wordpress
My code is here
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
If columns are greater than 3, then it should create a new row like this
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
<div class="row">
<div class="column">column1</div>
</div>
Any help will be highly appreciated.
Thanks in advance
Sure - just use modulus:
<?php
$elements = array('foo', 'bar', 'rab', 'oof');
echo '<div class="row">';
foreach ($elements as $i => $element) {
if ($i > 0 && $i % 3 == 0) {
echo '</div><div class="row">';
}
echo '<div class="column">' . $element . '</div>';
}
echo '</div>';
?>
DEMO
Output:
<div class="row">
<div class="column">foo</div>
<div class="column">bar</div>
<div class="column">rab</div>
</div>
<div class="row">
<div class="column">oof</div>
</div>
You need something like this:
<?
echo '<div class="row">';
for ($i=0; $i<15;$i++){
if ($i%3 == 0 && $i != 0){
echo '</div><div class="row">';
}
echo '<div class="column">column '.($i+1).'</div>';
}
echo '</div>';
?>
WORKING CODE
There is alternative solution with function.
<?php
// Managing the Code -- Functions: Handling a Variable Number of Parameters
// building a row of 10 <td> columns with a variable number of items
function preferencesRow()
{
// initialize $output
$output = '';
// use "func_get_args()" to collect all parameters into an array
$params = func_get_args();
// used to make sure 10 columns are filled
$maxCols = 10;
// loop through the parameters
foreach ($params as $item) {
$output .= "<td width='80px' align='center'>$item</td>\n";
$maxCols--;
}
// fill in the rest of the row with empty columns
for ($x = 0; $x < $maxCols; $x++) {
$output .= "<td width='80px'> </td>\n";
}
return $output;
}
// NOTE: you can use "." or "," with echo
echo '<h1>Preferences</h1><hr />' . PHP_EOL;
echo '<table border=1>' . PHP_EOL;
echo '<tr><th>Tarzan</th>';
echo preferencesRow('Africa', 'jungles', 'tantor', 'mangani', 'cabin in the woods',
'hunting', 'swinging in trees', 'Jane');
echo '</tr>';
echo '<tr><th>Jane</th>';
echo preferencesRow('London', 'parties', 'dancing', 'social events', 'lavish estates');
echo '</tr>';
echo '</table>' . PHP_EOL;

Two elements print in one foreach loop than next loop start

I have an array
$foo = array(1,2,3,4,5,6,7,8,9);
when I use foreach loop for this
foreach($foo as $val):
print '<li>'.$val.'</li>';
endforeach
Out put is ,
<li> 1 </li>
<li> 2 </li>
<li> 3 </li>
<li> 4 </li>
<li> 5 </li>
But I want out put something like that
<li> 1, 2 </li>
<li> 3, 4 </li>
<li> 5, 6 </li>
<li> 7, 8 </li>
Is it possible?
$foo = array(1,2,3,4,5,6,7,8,9);
foreach (array_chunk($foo, 2) as $chunk) {
echo "<li>" . implode(', ', $chunk) . "</li>\n";
}
For php you can do:
for($i=0;$i<count($foo);$i+=2) {
echo "<li>{$foo[$i]}, {$foo[$i+1]}</li>";
}
This might be what you require:
foreach($foo as $key=>$val) {
if ($val&1) {
echo '<li>' . $val;
if($key == (count($foo)-1)){
echo '</li>';
}
} else {
echo ',' . $val . '</li>';
}
}
Please try this:
$foo = array(1,2,3,4,5,6,7,8,9);
$i=1;
$firstElement = "";
foreach($foo as $val):
if($i%2==0)
{
print '<li>'.$firstElement.','.$val.'</li>';
}
else
{
$firstElement = $val;
}
$i++;
endforeach

Categories