Display Only 3 Foreach Result Per Row

display only 3 foreach result per row

Add a counter and start a new row every time it reaches a multiple of 3.

$counter = 0;
while($info = mysql_fetch_array($data)) {
if ($counter++ % 3 == 0) {
if ($counter > 0) {
$output .= "</tr>";
}
$output .= "<tr>";
}
// stuff
}
if ($counter > 0) {
$output .= "</tr>";
}

$output .= "</table>";

Please note: It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.

foreach display only first 3 items

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

Show different number of column per row in foreach loop

You can use nth-child selector along with CSS grid to get the desired result.

First Grid Style

.grid-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
grid-auto-rows: 200px;
}

img:nth-child(6n + 1),
img:nth-child(6n + 2){
grid-column: span 2;
}

img {
width: 100%;
height: 100%;
}
<div class="grid-container">
<img src="https://picsum.photos/id/10/400" />
<img src="https://picsum.photos/id/20/400" />
<img src="https://picsum.photos/id/30/400" />
<img src="https://picsum.photos/id/40/400" />
<img src="https://picsum.photos/id/50/400" />
<img src="https://picsum.photos/id/60/400" />
</div>

i have 6 products and i want to show 3 products on per row using php foreach

//initialize a counter 
$count = 0;

//start the table
echo("<table><tr>");
foreach ($products as $product) {

//if it's divisible by 5 then we echo a new row
if(($count % 5) == 0){

echo("</tr><tr>\n");
$count++;

}//if
else{
$count++;
}
echo("<td>$product<td>");

}//foreach

//close the table
echo("</tr></table>");

Css display 3 foreach results per row

Just make your .logcontainer float to the left, and every 3 display, clear the floating.

currentCol = 0
@foreach( var database in Model)
{

if (!(firstTime == database.DB))
{
<h3> @database.DB </h3>
currentCol = 0
}

<div class="logContainer" onclick="location.href='/logs/Details?databaseID=@database.DB&exceptionName=@database.Exception&exceptionsOccurred=@database.Count';">
<div class="counter"><b>@database.Count</b></div>
<div class="exceptionName"> Exceptions of Type: @database.Exception</div>
<div class="date">Siste: @database.LastOccurred</div>
</div>

currentCol += 1;
if (currentCol = 2) { //3 columns were displayed, switch row
currentCol = 0;
<br style="clear:both" />
}

firstTime = database.DB;
}

CSS :

.logContainer
{
width:500px;
float:left;
}

note: if the parent container of .logContainer is less than 1500px wide, you'll end up with a row of 2 columns followed by a row of 1 column.

Display 3 items per row - while loop - php/mysql

Declare a variable before your loop like: $currentRow = 1 and then inside, and at the end, of your loop add $currentRow++

You can then check if your row is divisible by 3 if($currentRow % 3 == 0) and put a break in.

Display all rows in column with foreach

After your update, code is not giving you first row, it's giving you one row just because of SUM() function in your query:

SELECT COUNT(*), sum(price),part_number,location,price FROM products

Note that, SUM() will returns the total sum of a numeric column.

So, in your case, you need to remove sum(price) function from your query than you will get the all rows.

You can use PHP for this purpose something like:

$sum = 0;
while($val = $stmt->fetch()) {
$sum += $val['price'];
}
echo $sum; // will return sum of all rows.

foreach loop returns only one result of post

You're retrieving an array of $posts but you're then only adding the first element $posts[0] to your $data array.

You need to loop them with foreach

$posts = get_posts(
array(
'cat' => $cat_id,
'tax_query' => array(
array(
'posts_per_page' => 4,
'taxonomy' => 'parsha',
'field' => 'term_id',
'terms' => $term->term_id,
)
)
)
);

foreach($posts as $post) {
$data = array(
'term_id' => $term->term_id,
'name' => $term->name,
'order' => $order,
'column' => $column,
'post_ids' => $post->ID,
'post_title' => $post->post_title,
'color_pdfs' => $pdfcolor,
'color_pdf_link' => $color_pdf,
);

if($column == 1) {
array_push($col1, $data);
}
else if($column == 2) {
array_push($col2, $data);
}
else if($column == 3) {
array_push($col3, $data);
}
else if($column == 4) {
array_push($col4, $data);
}
else if($column == 5) {
array_push($col5, $data);
}
}


Related Topics



Leave a reply



Submit