PHP while loop separate from every 2 rows into a div - php

I have a while loop from database, but i want to wrap every 2 rows into a div.
Currently i have this but is only wrapping first row.
<?php
$get = $connect->query("SELECT * FROM sabiaque ORDER by ordem");
$num = 0;
while ($f = $get->fetch_array(MYSQLI_ASSOC)) {
if ($num % 2) {
echo '<div class="divisor-slide">';
}
echo'
<a href="'.$f['link'].'"><div class="item-a">
<div class="box1-item-a">
<div class="i-wrap"><h1 class="i-white">'.utf8_decode($f['titulo']).'</h1><p>'.utf8_decode($f['subtitulo']).'</p></div>
</div>
<div class="box1-item-b">
<img src="../'.$f['ficheiro'].'" style="max-width: 100%; height: 100%;" />
</div>
</div></a>
';
if($num % 2) {
echo '</div>';
}
$num++;
}
?>

change this :
if($num%2) {
echo '<div class="divisor-slide">';
}
By this :
if( ($num % 2) == 0) {
echo '<div class="divisor-slide">';
}
And this :
if($num %2) {
echo '</div>';
}
To :
if( ($num % 2) == 0) {
echo '</div>';
}

I often find I get into a muddle using these special $num%2 whatsits (I'll get all excited in being clever in using them, then forget later what they do when I revisit the code). Why not simplify it like this (although it uses a little more code):
$num = 1;
while ($f = $get->fetch_array(MYSQLI_ASSOC)) {
if ( $num == 2 ) echo '<div class="divisor-slide">';
// rest of code
if ( $num == 2 ) echo '</div>';
$num++; if ( $num == 3 ) $num = 1;
}

Related

PHP For loop, how to loop code after 6 rows are displayed

So I am trying to wrap a div around my database results. The div must appear before first row and after the 6th row, it needs to close the div and loop it again. I tried to do it, but I can't manage to make it work properly. Any help would be appreciated.
for ($i=0; $i<count($row); $i++) {
if (($i) % 6 == 0) {
echo '<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6">
<img src="images/preview.jpg">
</div>';
if (($i) % 6 == 0) {
echo '</div>';
}
}
Also the code sample was not working at all (for/if/else not closed), see if this helps:
echo '<div class="swiper-slide">';
for ($i=0; $i<count($row); $i++)
{
if (($i>0) && ($i%6==0)) {
echo '</div>'."\n".'<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
}
echo '</div>';
test at eval.in
try this
<div class="swiper-slide"><!-- open it using HTML out of <??> php tags-->
<?php
for ($i=0; $i<count($row); $i++) {
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
if ($i % 6 == 0) {
//close and open new div for each group of 6
echo '</div>';
echo '<div class="swiper-slide">';
}//every 6th ... (you may need to change itto ( $i % 5 == 0)
}//for loop
echo '</div>';//close last opened, in case no more data or data less that 6
?>
Your second modulo operator compares the result to 9 instead of 0, and you have a } missing:
for ($i=0; $i<count($row); $i++) {
if (($i) % 6 == 0) {
echo '<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
if (($i) % 6 == 0) { // Corrected 9 to 0
echo '</div>';
}
}
Try this:
foreach ($row as $key=>$result) {
if ($key==0 || $key == 5) { //If this is the first or sixth row.
echo '<div class="swiper-slide">';
}
echo '<div class="col-lg-4 col-sm-6"><img src="images/preview.jpg"></div>';
if ($key==0 || $key == 5) {
echo '</div>';
}
}

Split query results in 2 div's

i have this php mysql query
<?php
$product = mysql_query('SELECT * FROM products LIMIT 6 ');
$pro = mysql_fetch_assoc($product);
?>
Now that query will return 6 products from database and what i want to do is echo 3 products inside a <div> and the other 3 products inside another <div> like this
<div class="first-3>
///Here i want to echo 3 products from the query from 1-3
<?php echo $pro['title']; ?>
</div>
<div class="second-3>
///Here i want to echo the rest 3 products of the query from 4-6
<?php echo $pro['title']; ?>
</div>
<?php
$num = 6;
$product = mysql_query('SELECT * FROM products LIMIT $num');
$firstDiv = "";
$secondDiv = "";
$i = 0;
while ($pro = mysql_fetch_assoc($product)) {
if ($i < ($num /2)) {
$firstDiv .= $pro['title'];
}
else {
$secondDiv .= $pro['title'];
}
$i++;
}
?>
And:
<div class="first-3>
<?php $firstDiv ?>
</div>
<div class="second-3>
<?php $secondDiv ?>
</div>
Iterate and output the values.
<?php
$product = mysql_query('SELECT * FROM products LIMIT 6 ');
$i = 1;
echo '<div class="first-3">';
while ( $pro = mysql_fetch_assoc($product) ) {
if ($i === 3) {
echo '</div><div class="second-3">';
}
echo $pro['title'];
$i++;
}
echo '</div>';
?>
Note that it's not safe to use mysql_query, you should be using mysqli or preferrably PDO.
mysql_fetch_assoc is used to retrieve a row in the resultset.
Doc: http://php.net/manual/es/function.mysql-fetch-assoc.php
A loop is required to iterate on each row.
A very simple example:
// Get a collection of 6 results
$products = mysql_query('SELECT * FROM products LIMIT 6 ');
// iterate over the 6 results
$i=0;
echo '<div class="first-3>';
while ($pro = mysql_fetch_assoc($products)) {
$i++;
// Print an item
echo $pro["title"];
// If 3 items are printed end first div and start second div
if($i==3){
echo '</div><div class="second-3">';
}
}
echo '</div>';
// Free the collection resources
mysql_free_result($products);
Just set up a counter to divide in groups of 3:
$count = 0;
while (...)
{
// your code
$count++;
if ( ($count % 3) === 0 )
{
echo '</div><div class="...">';
}
}
Please note that the mysql_* functions are deprecated and you should switch to PDO or mysqli.
$product = mysqli_query($conn, 'SELECT * FROM products LIMIT 6 ');
$results = array();
while($pro = mysqli_fetch_assoc($product)) {
$results[] = $pro;
}
echo '<div class="first-3">';
for($i = 0; $i < 3; $i++) {
echo $results[$i]['title'];
}
echo '</div><div class="second-3">';
for($i = 3; $i < 6; $i++) {
echo $results[$i]['title'];
}
echo '</div>';
Everytime you get the multiple of 3, ($k % 3 == 0) you will increment the flag variable, you can do then some conditions with the variable flag, i used here iterators because the hasNext() beauty.
Example
$pro = array(1, 2, 3, 4, 5, 6);
$flag = 0;
$pro = new CachingIterator(new ArrayIterator($pro));
foreach ($pro as $k => $v) {
// if multiples 3
if ($k % 3 == 0) {
$flag++;
if ($flag == 1) {
echo '<div class="first-3" style="border:1px solid black;margin-bottom:10px;">';
} else if ($flag == 2) {
echo '</div>'; // Closes the first div
echo '<div class="second-3" style="border:1px solid red">';
}else{ // if you have more than 6
echo '</div>';
}
}
// insert Data
echo $v . '<br/>';
if (!$pro->hasNext())
echo '</div>'; // if there is no more closes the div
}

Generate gallery loop

The Problem
I'm attempting to generate a gallery where it will display 3 "blocks" per row and alternate between large and small images.
For example:
Big | 4 Small | Big
4 Small | Big | 4 Small
One big image is the size of 4 small images.
What I've Tried
Here's an example of what I've tried so far.
$i = 0;
$r = 0;
$image = '';
foreach($gallery_images as $image_data) {
($i == 5 ? $i = 0 : '');
//If there's been 3 blocks added to the row, end the row and start a new one
if($r == 3) { $image .= '</div>'; $r = 0; }
//Start new row every 3 blocks
if($r == 0) { $image .= '<div class="row">'; }
//One big image, per 4 small in sequence
if($i == 0) {
$image .= '<div style="height: 300px; width:33.3%; background: red;float:left;"></div>';
++$r;
} else {
//If no small, start the block
if($i == 1) { $image .= '<div style="width:33.3%;height:300px;float:left;">'; }
//echo each small image
$image .= '<div style="height: 150px; width:50%;background: blue;float:left;"></div>';
//Once 4 images have been echoed, close the div
if($i == 4) { $image .= '</div>'; ++$r; }
}
++$i;
}
echo $image;
The first row displays fine, but then the next row messes up completely. I just can't see what I've missed in order for this to work.
The class of "row" is because I'm building this upon the foundation framework by Zurb in order to make it responsive.
The issue is that on the 2nd row(s), you do not increment $r to 1 until the end of the 4 small, so it continues to add <div class="row"> before each small image. You need to change your if block on the even rows. You can do this by adding 1 more counter, that keeps track of what row you are on -
by adding
$t=0;
and changing
//Start new row every 3 blocks
if($r == 0) { $image .= '<div class="row">'; }
to
//Start new row every 3 blocks
if($t%2==1){ // if we are on an even row
if($i == 1 && $r == 0) { $image .= '<div class="row">';}
}
else{ // if we are on an odd row
if($r == 0) { $image .= '<div class="row">'; }
}
you now have-
$i = 0;
$r = 0;
$image = '';
$t=0; // counter for odd/even row
foreach($gallery_images as $image_data) {
($i == 5 ? $i = 0 : '');
//If there's been 3 blocks added to the row, end the row and start a new one
if($r == 3) { $image .= '</div>'; $r = 0; ++$t; }
//Start new row every 3 blocks
if($t%2==1){ // if we are on an even row
if($i == 1 && $r == 0) { $image .= '<div class="row">';} // if it is the first small image group start the row
}
else{ // if we are on an odd row
if($r == 0) { $image .= '<div class="row">'; } // if it is the first large image
}
//One big image, per 4 small in sequence
if($i == 0) {
$image .= '<div style="height: 300px; width:33.3%; background: red;float:left;"></div>';
++$r;
} else {
//If no small, start the block
if($i == 1) { $image .= '<div style="width:33.3%;height:300px;float:left;">'; }
//echo each small image
$image .= '<div style="height: 150px; width:50%;background: blue;float:left;"></div>';
//Once 4 images have been echoed, close the div
if($i == 4) { $image .= '</div>'; ++$r; }
}
++$i;
}
echo $image;
you can see a phpFiddle example at http://phpfiddle.org/main/code/t25-kbe This is with 60 images, so there is 4 rows total, 2 of each pattern.
NEW ANSWER:
Ok this is how you wanted it:
<style type="text/css">
.big {
width:200px;
height:200px;
background:#03F;
}
.small {
width:50px;
height:50px;
background:#F00;
}
</style>
<?
$gallery_images = array(
1,2,3,4,5,6
);
echo '<div class="row">';
//count counts whether it's big or small
$count = 0;
//count2 counts whether it's the end of the row or not
$count2 = 0;
$row = 0;
foreach($gallery_images as $image_data) {
//assign these from image data
$big = "<img class='big'>";
$small = array(
"<img class='small'>","<img class='small'>","<img class='small'>","<img class='small'>"
);
//if it's 0 it's big else it's 1 and it's small
if($count === 0) {
echo $big;
$count++;
}else {
foreach($small as $s) {
echo $s;
};
$count = 0;
}
//if it's 2 then its the end of the row else increment
if($count2 === 2) {
echo "</div>";
echo "<div class='row'>";
$count2 = 0;
}else {
$count2 ++;
}
}
?>
NEW:
Ok you'd need to tinker with this, but I think it solves the issue:
<style type="text/css">
.big {
width:200px;
height:200px;
background:#03F;
}
.small {
width:50px;
height:50px;
background:#F00;
}
</style>
<?
$gallery_images = array(
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
);
//find out how many images
$total_images = count($gallery_images);
echo '<div class="row">';
//count counts whether it's big or small
$count = 0;
//count2 counts whether it's the end of the row or not
$count2 = 0;
$row = 0;
//overall count of images
$overall = 0;
for($i=0;$i<count($gallery_images);$i++) {
//assign these from image data
$big = "<img class='big' src='".$gallery_images[$i]."'>";
//increment overall
$overall++;
if($i+1 < $total_images) {
$small1 = $i+1;
$small[] = $small1; //would be $gallery_images[$small1]
$overall++;
}
if($i+1 < $total_images) {
$small2 = $i+2;
$small[] = $small2; //would be $gallery_images[$small2]
$overall++;
}
if($i+1 < $total_images) {
$small3 = $i+3;
$small[] = $small3; //would be $gallery_images[$small3]
$overall++;
}
if($i+1 < $total_images) {
$small4 = $i+4;
$small[] = $small4; //would be $gallery_images[$small4]
$overall++;
}
//if it's 0 it's big else it's 1 and it's small
if($count === 0) {
if($overall < $total_images) {
echo $big;
$count++;
}
}else {
if($small) {
foreach($small as $s) {
echo "<img class='small' src='".$s."' />";
};
$count = 0;
}
}
if($count2 === 2) {
echo "</div>";
echo "<div class='row'>";
$count2 = 0;
}else {
$count2 ++;
}
unset($small);
if($overall >= $total_images) {
echo "</div>";
}
}
?>

Execute code each time x <= y while looping

I'm not exactly sure how to explain this but basically I have an x variable equal to the number of rows in a database and each time it is equal to 4 or less, run a piece of code.
So say I have 5 rows, I want to execute the code twice because x is equal to 4 once and then equal to 1. Same if I had 6 rows (4+2) 7 rows (4+3) or 8 rows (4+4).
Then, when I reached 9 rows (4+4+1), it would execute the code thrice. Etc...
The code I'm trying to run is a piece of HTML with some more PHP inside it. I want to create a new <ul> with the said code inside it each time the number of rows is equal to 4 or less as explained above.
The code as it is right now:
<div class="row-fluid well">
<?php
$num_rows = mysql_num_rows($result);
if ($num_rows <= 4) {
?>
<ul class="minigames">
<?php
while ($row = mysql_fetch_array($result)) {
echo "<li class='span3'>";
if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (logged_in() === false && !empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "...<br /><br /><center><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) <= 100) {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else if (!empty($row['app_about']) === true && strlen($row['app_about']) >= 101) {
$app_about = substr(strip_tags($row['app_about']), 0, 100);
echo "...<br /><br /><center><i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
} else {
echo "<i class=\"icomoon-white icomoon-screen\"></i> Play in browser</center>'>";
}
?>
<img src="<?php echo $row['app_preview']; ?>">
<div class="minigames-caption">
<h4><?php echo $row['app_name']; ?></h4>
<p>By <?php echo $row['app_dev']; ?>.</p>
</div>
</a>
<?php
echo "</li>";
}
?>
<?php
}
?>
If the number of rows are limited, I would put the results into one array and then use array_chunk() to split them into portions.
$results = array();
while ($row = mysql_fetch_array($result)) {
$results[] = $row;
}
foreach (array_chunk($results, 4) as $chunk) {
echo '<ul>';
foreach ($chunk as $row) {
echo '<li>', /*....*/, '</li>';
}
echo '</ul>';
}
Divide x by 4 and round it up. Eg 4/4 = 1, 5/4 = 2, 9/4 = 3. Then, use a for loop.
var noOfTimes = //x/4 rounded up
for (var i = 0; i < noOfTimes; i++) {
//insert code here
}
Use the ceil function.
http://us3.php.net/manual/en/function.ceil.php
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < ceil($num_rows / 4); $i++) {
// do stuff
}

360 grid -- Finish out row with odd number of records

I'm trying to accomplish this, with the 360 grid system: http://imgur.com/4ZFll
From a database i'm getting products which will be displayed in lines with 4 on each.
It's working perfectly if there is exactly 4 products under each category, but if there is less than 4 products in a category, the design is messed up, because the div's not closed properly.
Problem is that sometimes there's only 3 or less products on a line.
Is there any of you who knows how to accomplish this?
for($i=0 ; $i<$countprod ; $i++){
$prevprod = $products[$i-1]['name'];
$curprod = $products[$i]['name'];
if($curprod != $prevprod){
echo '<div class="grid_12 alpha omega"><h2>'.$products[$i]['catname'].'</h2></div>';
}
if ($i == 0){ echo '<div class="grid_3 '; }
if ($i % 4 == 0) { echo ' alpha">'; }
elseif($i % 4 == 3) { echo '</div><div class="grid_3 omega">'; }
else{ echo '</div><div class="grid_3">';
}
echo $product[$i]['image'];
if ($i % 4 == 3) {
echo '</div><div class="clear"></div>';
echo '<div class="grid_3';
}
}
(sorry about the title, i didnt know what to call this question :) )
echo '<div class="grid_3';
You aren't closing this tag.
Have a try with
$countprod = count($product);
$prevprod = '';
$close_div = false;
for ($i=0; $i<$countprod; $i++){
$curprod = $products[$i]['name'];
if($curprod != $prevprod){
if ($close_div) echo '</div>';
echo '<div class="grid_12 alpha omega"><h2>'.$products[$i]['catname'].'</h2></div>';
}
if ($i % 4 == 0) {
echo '<div class="grid_3 alpha">';
$close_div = true;
}
elseif ($i % 4 == 3) {
echo '</div><div class="grid_3 omega">';
$close_div = true;
}
else {
echo '</div><div class="grid_3">';
$close_div = true;
}
echo $product[$i]['image'];
if ($i % 4 == 3) {
echo '</div><div class="clear"></div>';
$close_div = false;
}
$prevprod = $curprod;
}
$p = 10; // Current number of products
$ppr = 4; // Products per row
$x = $i % $ppr;
if($x != 0){
$countprod = $p + ($ppr - $x);
}
echo $countprod; // 12 (4 * 3)
Loop with FOR if there is no product just print empty DIV, if that's what you have asked...

Categories