I have the following foreach loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php endforeach; ?>
The first 2 items are fine but the items 3-14 I need to wrap in a div so I can control the layout. I am wondering how I would do this? The problem is not all of the 3-14 items will be populated.
Any advice would be welcome ... thanks
You need a control variable to determine which iteration you are at, from there you can add divs or whatever you want to do from the third iteration onward. You could try something like this:
<?php $i = 0;
foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php $i++;
if ($i < 3) { ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php } else { ?>
Add divs, or whatever you want to do here.
<?php }
endforeach; ?>
So with your additions, since the original formula should work, don't mess with that part. Instead make an addition via a $style variable like so:
$i = 1;
foreach ($this->item->extra_fields as $key=>$extraField):
if($i == 1)
$style = 'id="largeImageWrap" class="pull-left"';
elseif($i == 2)
$style = 'id="sidePanelWrap"';
else
$style = 'class="row"';
// If less than or equal to 3, add <div>
if(($i <= 3))
$front = true;
// If greater than 14 <div>
elseif(($i > 14))
$front = true;
// Else no <div>
else
$front = false;
if($front == true) echo "<div $style>"; ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php
// If greater than 2 and less than 14 echo blank or </div>
echo (($i > 2) && ($i < 14))? "":"
</div>";
$i++;
endforeach;
Related
I'd like to set the following up as a loop (so that the pattern is repeated every 5 elements). I've tried to read up on it and I believe I need to set it up as an array, but I have no idea where to start.
Here's my code:
<div class="ccm-page-list">
<?php
$i= 0;i;
foreach ($pages as $page):
$i++;
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$img = $page->getAttribute('thumbnail');
$thumb = $ih->getThumbnail($img, 550, 550, true);
?>
<a href="<?php echo $url ?>" target="<?php echo $target ?>"> <div class="col-sm-4 grid-item">
<div class="item-img-blog" >
<img src="<?php echo $thumb->src ?>" width="auto" height="100%" alt="" />
<div <?php
if($i == 1) {
?>class="item-class-1" <?php }
if($i == 3) {
?>class="item-class-2" <?php }
if($i == 2) {
?>class="item-class-3" <?php }
if($i == 4) {
?>class="item-class-3" <?php }
if($i == 5) {
?>class="item-class-1" <?php }
?>>
<div class="text-container">
<h1><?php echo $description ?></h1>
<p><?php echo $date = $page->getCollectionDatePublic('d/m/Y'); ?></p>
</div>
</div>
</div>
</div></a>
<?php endforeach; ?>
</div>
I would really appreciate any help! Thanks in advance!
$i= 0;
$classes = array(1 => 1, 2 => 3, 3 => 2, 4 => 3, 5 => 1);
foreach ($pages as $page) {
$i++;
.....
echo 'class="item-class-'.$classes[$i % (count($classes) + 1)];
.....
}
You don't need an extra loop, you can use the modulo operator (aka, the remainder of a division) and some basic math to cope with the fact that you want to cycle 1->5:
$i= 0; // note that I removed the "i;" at the end of this line!
foreach ($pages as $page):
$cyclici = ($i % 5) + 1;
// then, use $cyclici everywhere you want to see that 1-5 1-5 pattern
...
if($cyclici == 1) {
...
// and increment $i at the *end* of the loop instead of the start.
$i++
endforeach;
EDITed to clarify the relationship between $i and $cyclici - $i is still incremented for each iteration of the loop, $cyclici is derived from that incrementing value to obtain the desired 1 2 3 4 5 1 2 3 4 5 ... sequence.
This while loop is supposed to continue until the end of the array or until counter reaches 6, what am I doing wrong here?
while($row = mysql_fetch_array($resultSet, MYSQL_ASSOC) && ($counter < 6))
{
?>
<?php echo $row['Item_NAME'] ?>
<img style="width:250px; height:250px;" src="<?php {echo "{$row['Item_IMAGE']}";} ?>">
<?php $counter = $counter + 1;
?>
<?php
}
Works without the && ($counter < 6)but shows the wrong number of images, adding this will show the correct number of boxes (where the images should be) but not retrieve the images or names from the array.
Thanks for any help.
Couldn't you just use break; inside, when the second condition isn't true anymore?
while($row = mysql_fetch_array($resultSet, MYSQL_ASSOC))
{
if($counter > 5){
break;
}
?>
<?php echo $row['Item_NAME'] ?>
<img style="width:250px; height:250px;" src="<?php {echo "{$row['Item_IMAGE']}";} ?>">
<?php $counter = $counter + 1;
?>
<?php
}
<?php foreach ($rows as $id => $row): ?>
Basically i want my output for the first 5 rows to be inside <div class = "firstcolumn">
Once I have reached my five rows I want it to generate a second <div class = "secondcolumn"> for the rest of the rows. Also the row outputs have to come inside the div tags. So I only generate one div that fills up with rows
Can anyone help me to achieve this?
Try this
$cnt = 1;//Counter variable
foreach ($rows as $id => $row){
if($cnt <= 5){
$var = '<div class = "firstcolumn">';
}
else if($cnt > 5){
$var = '<div class = "secondcolumn">'
}
$cnt++;
}
In keeping with your style of coding, a loop counter will do the trick.
<?php $i = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $i++; ?>
<?php $colName = $i > 5 ? "secondcolumn" : "firstcolumn"; ?>
<div class="<?php echo $colName; ?>">
<?php endforeach; ?>
$count = 1;
foreach ($rows as $id => $row)
{
if($count <= 5)
{
if($count==1){
?>
<div class = "firstcolumn">
<?php }?>
<?php print $row; ?>
<?php
}
else
{
if($count==6){
?>
<div class = "secondcolumn">
<?php }?>
<?php print $row; ?>
<?php
}
if($count == 5){
?>
</div>
<?php
}
$count++;
}
if($count != 1 && $count!=6){
?>
</div>
<?php
}
At the moment, with the code from below, I have the data shown like this:
http://img27.imageshack.us/img27/8083/29769986.jpg
but I want it to be shown like this:
http://img259.imageshack.us/img259/3233/24033830.jpg
The code for the data shown, as it is on the first image, is:
<div id="content">
<?php foreach ($categories as $category) { ?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
I order to get the "Hewlett-Packard" text under the "HTC" text, I've changed the "/ 4" into "/ 1", but I have no idea how to make the data to be shown into three columns (like on the second picture), instead of one, as it is now (as shown on the first picture).
Thanks in advance.
EDIT: What I actually need, is to count and to do the calculation on this code:
<?php foreach ($categories as $category) { ?>
.
.
.
<?php } ?>
So it needs to count the number of categoris, do the calculations, and present the code between into three columns.
Try this one.
<div id="content">
<div class="content-column">
<?php
$cols = 3; // Change to columns needed.
$catcount = count($categories);
$catpercol = ceil($catcount / $cols);
$c = 0;
foreach ($categories as $category) {
if ( $c == $catpercol ) {
$c = 0;
print "</div><div class='content-column'>";
}
?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php $c++; } ?>
</div>
</div>
Add .content-column { float: left; width: 33.33333%; } to your CSS.
Details:
$cols = 3; enables you to set the desired number of columns (note: you might need to change CSS accordingly).
$catcount = count($categories); gives you the total number of categories about to be rendered.
$catpercol = ceil($catcount / $cols); divides that total number evenly into the required number of columns with the last column having eventually less items than the others.
$c = 0; is your counter. It increases at the end of the outer foreach loop.
Within the loop, $cis checked if it matches the $catpercol number and if so, the current parent div is closed and a new one created. You end up with as many parent divs as you need columns. Just add appropiate CSS to make them appear besides each other.
understand the following code that according to your requirement, the code just give the hint to achieve that you want according to your given screen shoot http://img259.imageshack.us/img259/3233/24033830.jpg
echo "<table>";
echo "<tr>";
$i = 1;
do{
// column range
$range = 3;
echo "<td>" . $i;
if( $i % $range == 0 ){
echo "</tr>";
echo "<tr>";
}
echo "</td>";
$i++;
}while( $i <= 10 );
echo "</tr>";
echo "</table>";
Hope this will help you
I think it is possible to create three column layout via html/css and without any change of your PHP code. Just use float:left; width: 33%. You can also use absolute value for width property because of margins and borders.
I have this php foreach loop
<?php $all_news = $this->db->query("Select * from news"); ?>
<div class="inner">
foreach($all_news as $key=>$news){?>
<div class="news <?php echo ($key%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
But the problem is the $all_news may have 20 results or so but the design only allows me to put for 4 news blocks in each inner div...is there a way make this happen so i have only 4 news divs in each inner div
<?php
$all_news = $this->db->query("Select * from news");
echo '<div class="inner">';
$c = count($all_news);
for($i = 0; $i < $c; $i++){
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
if($i % 4 == 3)
echo '</div><div class="inner">';
}
echo '</div>';
?>
Change your query to only return 4 rows:
SELECT * FROM news LIMIT 4
Alternatively you can change your for-loop.
for($i = 0; $i < min(4, count($all_news)); $i++)
{?>
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$i]['title'];
<?}
[edit]
See what you mean now. Create two loops:
<?
$index = 0;
while ($index < count($all_news))
{
$news = $all_news[$index];
?>Start outer div<?
for ($item = 0; $item < 5; $item++)
{
?>Inner div with news item <? echo $news['title'];
}
?>End outer div<?
$index++;
}
You could use two for loops:
<?php $all_news = $this->db->query("Select * from news"); ?>
<?php for($i = 0, $l = count($all_news); $i < $l; $i+=4): ?>
<div class="inner">
<?php for($j = $i; $j < $i+4; $j++): ?>
<div class="news <?php echo ($j%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$j]['title'] ?>
<?php endfor;?>
</div>
<?php endfor;?>
Another option would be array_chunk [docs].
The laziest way would be to just check whether or not you've already done four in the current div on the fly. If you have, close the current div and start a new one:
<div class="inner">
<?php
foreach ($all_news as $key => $news) {
if ($key % 2) {
$oddEven = 'odd';
} else {
$oddEven = 'even';
if ($key && $key % 4 === 0) {
echo '</div><div class="inner">';
}
}
echo "<div class=\"news $oddEven\">";
// ...
}
?>
</div>
Note that this assumes $all_news has an element at 0, so it makes sure that it doesn't close the first, empty div.