how to allow more than 1 rows to appear horizontally - php

This is an example of what I want to achieve
but what I was able to display is 1 product per row.
This is what I have done and but is not working as intended.
<ol>
<?php
$no = 1;
foreach($projects as $project):?>
<?php if($project['finish'] == 'no'):?>
<li><?=$no?></li>
<li><?=$project['type']?></li>
<li><?=$project['date_started']?></li>
<li><?=$project['brief_description']?></li>
<li><?=$project['full_description']?></li>
<li>
<?php foreach($images as $image):?>
<?php if($image['project_id'] == $project['id']):?>
<img src="img/<?=$image['name']?>">
<?php endif;?>
<?php endforeach;?>
</li>
<li>
Contact admin
</li>
<?php endif;?>
if($no % 5 === 0):
echo "<br>";
endif;
<?php $no++; ?>
<?php endforeach;?>
How can I get the code to display 5 lists per row

<html>
<body>
<table>
<?php
echo '<tr>';
for($i=1;$i<100;$i++)
{
echo '<td>'.$i.'</td>';
if($i%5==0)
{
echo $i;
echo '</tr><tr>';
}
}
?>
</table>
</body>
</html>
This will you the idea

Related

Limit this PHP foreach statement to just 5 loops

How could I limit this foraech statement to just 5 loops? I think I should just use Break but I'm not sure where to put it.
<?php if(!empty($locations)): foreach($locations as $location): ?>
<?php if(empty($location["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>
You can use array_slice() first to get a new array with no more than 5 elements.
$locations = array_slice($locations, 0, 5);
Then everything unchanged.
There are three methods:
Method 1: foreach with a counter var
$counter = 1;
foreach($locations as $location) {
// use $location here
if($counter++ == 5) {
break;
}
}
Method 2: foreach using $key=>$val
foreach($locations as $key=>$val) {
// Your content goes here
if($key === 4) {
break;
}
}
Method 3: for loop
for($i = 0; $i < 5; $i++) {
// Use $locations[$i] here and do something with it
}
Using a counter variable into your loop you can control/limit to any number.
Example:
$counter = 0;
foreach($locations as $location):
if($counter++ == 5):
break;
// Your other content goes here
endif;
endforeach;
Add a variable ... increment it each iteration ... after reach 5 just break loop.
<?php $i = 1;?>
<?php if(!empty($locations)): foreach($locations as $location): ?>
<?php if(empty($location["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
<?php if ($i++ == 5) break; ?>
<?php endforeach; ?>
You can use for():
<?php if(!empty($locations)):
for($i=0; $i<5; $i++) {
$location = $locations[$i];
<?php if(empty($locations["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
}

PHP foreach: data as value less than a number

I need the code to be executed only if value of $entry[0] is less than 10, but it doesn't work.
I know the error is here: $data as $entry[0] < 10
The data is an excel worksheet and $entry[0] is serial numbers from 1 to 100 and $entry[1] is text field (domain names).
<?php $data = wp_excel_cms_get("top100"); ?>
<?php foreach($data as $entry[0] < 10): ?>
<?php echo $entry[0]." ";?>
<a href ="<?php echo "http://". $entry[1]; ?>" target="_blank">
<?php echo $entry[1];?></a><br />
<hr />
<?php endforeach; ?>
You are using foreach in a wrong way. First, iterate over data, then apply your logic in your sheet row data:
<?php $data = wp_excel_cms_get("top100"); ?>
<?php foreach($data as $entry): ?>
<?php if($entry[0] < 10): ?>
<?php echo $entry[0]." ";?><?php echo $entry[1];?><br />
<?php endif; ?>
<hr />
<?php endforeach; ?>
<?php
$data = wp_excel_cms_get("top100");
foreach($data as $entry[0] ){
if ($entry[0] < 10){ echo $entry[0]." ";?>
<?php echo $entry[1];?><br />
<?php } ?>
<hr>
<?php endforeach; ?>

PHP loop interject

Consider the following loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php endif; ?>
<?php endforeach; ?>
I wanted to wrap the first 12 items in a div and then the last 2 items in a div. The problem is there are not always 12 items exactly in the first div. There can be between 2 AND 12 items.
How would I manipulate this loop to achieve such? Many thanks
Just use a counter inside the loop to see how many times you've been through it.
<?php $count = 1; ?>
<?php $break= count($this->item->extra_fields) - 2; ?>
<?php echo "<div>"; ?>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
</div>
<?php $count++; ?>
<?php endif; ?>
<?php if ($count == $break) : ?>
<?php echo "</div><div>"; $count ==0; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>

How to show 3 <li> in one row

I need to show 3 images in each row in <ul> tag. I have multiple <ul> tags and in each <ul> tag, I am showing 6 images. Now i have to show 3 <li> in first row and next 3 <li> in second row. Then same for next <ul>. I need to break row after 3 <li>. This is my code :
<div class="sets">
<?php foreach ($sets as $set => $items) : ?>
<ul class="set test-set">
<?php $i=0; foreach ($items as $thumb) : ?>
<?php
/* Prepare Image */
$content = '<img src="'.$thumb['cache_url'].'" width="'.$thumb['width'].'" height="'.$thumb['height'].'" alt="'.$thumb['filename'].'" />';
?>
<?php if($i === 0):
echo '<li><div>'; ?>
<?php endif; ?>
<?php echo $content; ?>
<?php if($i === 2): $i = 0; ?>
<?php else: $i++; endif; ?>
</div></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div>
http://jsfiddle.net/z4Q48/
li{
float: left;
}
li:nth-child(3n+4){
clear: both;
}
see http://css-tricks.com/how-nth-child-works/

For loop, runs once?

I'm using OpenCart and I'm trying to achieve rendering out the meta_description (used to identify the comic publisher) and use it to make a drop-down list with sub-categories, or to give the illusion of it. Here is my code now, it's an adopted version of the current OpenCart code. ['class'] is how I grab the child categories meta_description.
Basically, the second for statement doesn't work - it only does the first one. I would appreciated any kind of support on this.
<div class="menu">
<div id="top"></div>
<span>
<ul id="nav">
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<div class="subs">
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<h3>DC Comics</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "DC Comics"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
<h3>Marvel</h3>
<?php for (; $i < $j; $i++) { ?>
<?php if($category['children'][$i]['class'] == "Marvel"){ ?>
<li>
<ul>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
</span>
</div>
Use different variables in loops, in you code $i is used in main loop and incremented in inner loops you can use foreach loop like ,
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if (is_array($category['children']) and isset($category['children'])) { ?>
<div class="subs">
<div>
<ul>
<?php
$li1='<li><h3>DC Comics</h3><ul>';
$li2='<li><h3>Marvel</h3><ul>';
foreach($category['children'] as $child)
{
if($child['class'] == "DC Comics")
{
$li1.='<li>'.$child['name'].'</li>';
}
if($child['class'] == "Marvel")
{
$li2.='<li>'.$child['name'].'</li>';
}
}
$li1.='</ul></li>';
$li2.='</ul></li>';
echo $li1;
echo $li2;
?>
</ul>
</div>
</div>
<?php } ?>
</li>
<?php } ?>

Categories