Divide php for cycle in 2 columns - php

I have a code:
<?php for ($i = 0; $i < sizeof($categories); $i = $i + 4) { ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { ?>
<?php if (isset($categories[$j])) { ?>
<img src="<?php echo $categories[$j]['thumb']; ?>" title="<?php echo $categories[$j]['name']; ?>" alt="<?php echo $categories[$j]['name']; ?>" style="margin-bottom: 3px;" /><br />
<?php echo $categories[$j]['name']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
I want to place these categories in 2-column like this:
<div class="span-8">
<div class="product">
product1
</div>
</div>
<div class="span-8 last">
<div class="product">
product2
</div>
</div>
How can I do this?

<?php
for ($i = 0; $i < sizeof($categories); $i = $i + 4) {
for ($j = $i; $j < ($i + 4); $j++) {
if (isset($categories[$j])) {
if($colcount % 2){
$col1+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}else{
$col2+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}
$colcount++;
}
}
}
echo "<div class='span-8'>".$col1."</div><div class='span-8 last'>".$col2."</div>";
Or you could do it the easy way and make it a fluid <ul> that way they would do it automatically

I don't really see the correlation between the php and the html code, but I think you just need to output a different css class for each odd category:
$last = false;
foreach($categories as $c) {
//Output category html
?>
<div class="span <?=($last)?'last':''?>">.....</div>
<?
$last != $last;
}

Related

foreach display only first 3 items

We use a foreach code and we want to display only the first 3 items.
But for some reason our code does not work, it currently still display all items.
What am I missing here?
CODE:
<?php $items = $_order->getAllItems(); $i = 0; foreach($items as $i): if($i < 3) {?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $i++; } endforeach;?>
You need to use different variable inside foreach():-
<?php
$items = $_order->getAllItems();
$i = 0;
foreach($items as $itm):
if($i >= 3) {break;}else{?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($itm->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $i++; } endforeach;?>
A much better solution using array_slice():-
<?php
$items = $_order->getAllItems();
$item = array_slice($items, 0, 3); // get first three only
foreach($item as $itm):
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($itm->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php endforeach;?>
Sorry, read the question wrong. Here's the updated answer.
Your foreach iterater was same as the count variable $i
<?php
$items = $_order->getAllItems();
$i = 0;
foreach($items as $item) {
?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php
$i++;
if($i == 3) {
break; // because we don't want to continue the loop
}
}
?>
Use for seems like more pretty than foreach:
<?php $items = $_order->getAllItems();
for ($i = 0; $i < count($items) && $i < 3; $i++): ?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($items[$i]->getProductId())->getSmallImageUrl(); ?>"
border="0"/></div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($items[$i]->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php endfor; ?>
if you want to show only 3 items then you should break out of foreach:
if($counter >= 3) break;
else { //rest of the code ...
}
or simply use a for loop instead.
you are resetting your counter $i for every iteration in the loop, use another variable $counter
<?php $items = $_order->getAllItems(); $counter = 0; foreach($items as $i): if($counter < 3) {?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /></div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $counter++; } endforeach;?>
$count='1';
for ( $i = 0; $i <= 100; $i++ ) {
if ( $var[$i] == "value" ) {
print $i.'-'.$var[$i] . "<br>"; // print $i to display row number
if ( $count++ >= 3 ) {
break;
}else{
// have a cup of coffee ;)
}
}

How to merge two PHP for-loop

How do I merge two (for) in one code?
e.g first code
for ($i = 0, $n = count($options); $i < $n; $i ++) {}
and second code
for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++)
here is oll code
<?php if ($display_poll) { ?>
<form action="<?php echo JRoute::_('index.php');?>" method="post" name="poll_vote_<?php echo $poll->id;?>" id="poll_vote_<?php echo $poll->id;?>">
<?php for ($i = 0, $n = count($options); $i < $n; $i ++) { ?>
<label for="mod_voteid<?php echo $options[$i]->id;?>" class="<?php echo $tabclass_arr[$tabcnt].$params->get('moduleclass_sfx'); ?>" style="display:block; padding:2px;">
<input type="radio" name="voteid" id="mod_voteid<?php echo $options[$i]->id;?>" value="<?php echo $options[$i]->id;?>" alt="<?php echo $options[$i]->id;?>" <?php echo $disabled; ?> />
<?php echo $options[$i]->text; ?> <?php for ($u = 'rel_article1'; $u <= 'rel_article5'; $u++) { ?>
<a href="<?php echo $params->get($u); ?>" onClick="return popup(this, 'notes')">
<img src="/images/stories/add.png" alt="play"></a>
<?php
} ?
I want in one poll show onle play button
now shows this
here is my xml code:
You don't really need that second for. You already have an index with $i from your first loop. (Except that it starts from 0 instead of 1, but you can easily get it working)
Remove the second for, and try this :
// echo $params->get($u);
echo $params->get('rel_article' . ($i + 1));

PHP limit the loop based on available array element

I created the available seat with a for loop below:
<?php
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
And I want to add a booked class if the $i variable matches with the $k variables which taken from the booked seat array elements below:
$booked_seat = array('1','4','5','6','7');
the result:
Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
So I did this:
<?php
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center <?php echo ($i == $booked_seat[$i-1]) ? 'booked' : '' ?>' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
And i get the offset Error because the $booked_seat is not much as the $seats loop, how to limit the loop so it will not offset?
Thank you before
Try this:
$seats = 7;
for ($i=1; $i <= $seats; $i++) {
?>
<div class='col-xs-6'>
<div class='well text-center <?php echo (in_array($i, $booked_seat)) ? "booked" : "" ?>' id='<?php echo $i ?>'>
Seat No: <?php echo $i ?>
</div>
</div>
<?php
}
?>
warning: code is untested.
You could always flip the $booked_seat array, then test for the existence of the array element using isset(). Something like this:
<?php
$seats = 7;
$booked_seat = array('1','4','5','6','7');
$booked_a = array_flip($booked_seat);
for ($i=1; $i <= $seats; $i++) {
printf("seat: %d%s\n", $i, isset($booked_a[$i]) ? " booked" : "");
}
(This was my test, you can add your HTML to suit.)
Example: http://3v4l.org/5npsm
You should use the method sizeof().
So:
$num_seats = sizeof($seats);
Then a for loop with this.
for($i = 0; $i < $num_seats; $i++){
//code
}

html select box - load image for default selected option

Today I faced with a problem about html select
here is the code :
<select id="optdon" onchange="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { ?>
<option value="<?php echo $donname['don_id']; ?>"><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
function showimage(ids){
clearall();
if(ids.value != ''){
jQuery('#'+ids.value).show();
}
}
What is the project ? I need to select first option !!!
It is easy but when i set the first option selected it's image doesn't load in the page first load . option's image appears on changing options.
I have these two answers for selecting first option (when thre is just one option )
suggestion 1
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['don_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<select id="optdon" onload="showimage(this);" name="optdon" style="width:<?php echo $drop_width; ?>px;">
<option value="">--Select don--</option>
<?php $z = 0; $selected='selected' ?>
<?php foreach($for_don as $donname) { if(isset($donname['don_name']) && $donname['status'] == 1) { $z = $z + 1; ?>
<option <?php if($z==1 && $x == 1)echo $selected; ?> value="<?php echo $donname['don_id']; ?>" ><?php echo $donname['don_name']?></option>
<?php } } ?>
</select>
<br /><br />
<div align="center">
<?php $i = 0; foreach($for_don as $donimage) { if(isset($donimage['don_image'])) { ?>
<div class="<?php echo $i; ?>" style="display:none;" id="<?php echo $donimage['don_id']; ?>"><img src="<?php echo $donimage['don_image']?>" /></div>
<?php $i = $i + 1;} }?>
</div>
suggestion 2 :
<?php $x = 0; foreach($for_don as $donname){ if(isset($donname['donation_name']) && $donname['status'] == 1) { $x = $x + 1; }} ?>
<?php if($x == 1) { ?>
??????
<?php } else { ?>
thank you
Add this JavaScript:
var optdon = document.getElementById('optdon');
optdon.onchange.call(optdon);
This will trigger the onChange event handler, allowing the initial option to have its effect.

Is there an easy way to do 4 at a time in a php foreach loop

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.

Categories