Count row and repeat the same rows within Loop - php

I'm working on mansory gallery here images fetching into the row first row contains three images and then for the second row contains two images and so on.
Now currently all images coming inside same row. I want to add in a loop I had tried but unable to achieve the result. For inspiration link
<?php
include('admin/config.php');
$result = mysqli_query($db, "SELECT * FROM gallery order by id desc");
// var_dump($result->num_rows);
while ($row = mysqli_fetch_array($result)) {
echo "<div class='gallery-items'>";
echo "<div class='mansory-item'>";
echo "<a href='admin/images/".$row['path']."' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/".$row['path']."' class='item-img img-1'>";
echo "</a>";
echo "</div>";
echo "</div>";
}
?>
Current output
Expected output
Can anyone suggest me how should i get this output.

Check below snippet,
$inc = 4;
$i = 1;
while ($row = mysqli_fetch_array($result)) {
if (empty($temp) || $inc != $temp) {
$temp = $inc;
if ($inc == 4) {
echo "<div class='gallery-grid'>";
}
echo "<div class='gallery-items'>";
}
if ($i <= $inc) {
// echo $i . '<>';
echo "<div class='mansory-item'>";
echo "<a href='admin/images/" . $row['username'] . "' data-lightbox='gallery' class='ansa-thumb'>";
echo "<img src='admin/images/" . $row['username'] . "' class='item-img img-1'>";
echo "</a>";
echo "</div>";
$i++;
}
if ($i == $inc) {
echo "</div>";
if ($i == 3) {
echo "</div>";
}
$i = 1;
$inc = ($inc == 4 ? 3 : 4);
}
}

Related

PHP dynamically changing div issue

I have encountered a small issue, and I'm not sure what's the best way to solve this.
Using this code below I'm getting 5 items per <div> in 1st loop, but starting from the 2nd - 4 items per <div>.
<?php $count = 1;
while($data = mysqli_fetch_array($result)) {
if($count === 1) {
echo "<div id='".$data['title']."' class='images-container'>";
}
echo "<div id='img_div'>";
echo "<img src='uploads/" . $data['filename']."'>";
echo "<p class='img_title' id='".$data['title']."'>" .$data['title']. "</p>";
echo "<p id='img_desc'>" .$data['photo_description']. "</p>";
echo "<p>" .$data['price']. "</p>";
echo "</div>";
if($count % 5 === 0) {
echo "</div>";
$count = 1;
echo "<div class='images-container'>";
}
$count++;
}
?>
If I wouldn't need an id for "<div id='".$data['title']."' class='images-container'>"; I would know how to solve this, but since I need to have a specificid, I'm not really sure how.

how to make a table row contents to break lline? [duplicate]

I have the following code:
for($i=0; $i<count($gallery);$i++)
{
$temp = array();
$temp = $gallery[$i];
echo "<img src='". $temp->path . "' />";
}
Now this code prints the content in one row. I want to print only 3 per row and then create new row and print another 3 and so on. How can this be done?
appreciate the support :)
EDIT: error
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 5
Filename: views/profile.php
Line Number: 105
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/profile.php
Line Number: 106
You can do
$n = 3;
echo "<table><tr>";
for($i=0; $i<count($gallery);$i++){
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
if($i % $n ==0 && $i!=0 ){
echo "</tr><tr>";
}
}
echo '</tr></table>';
Edit:
If you want to do it the "right" way - by building the syntactically correct HTML, you need to do:
$n = 3;
echo "<table><tr>";
$gallery_count = count($gallery);
for($i=0; $i<$gallery_count; $i++){
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
if($i != 0){
if($i % $n == 0 && $i != $gallery_count-1){
echo "</tr><tr>";
}
else{
echo ""; //if it is the last in the loop - do not echo
}
}
}
//example - if the last 2 `td`s are missing:
$padding_tds = $gallery_count % $n;
if($padding_tds != 0 ){
$k = 0;
while($k < $padding_tds){
echo "<td> </td>";
}
}
echo '</tr></table>';
You just need to a modulus that checks how many have been printed - any multiple of 3 will add a break.
$x=0;
for($i=0; $i<count($gallery);$i++)
{
$x++;
$temp = array();
$temp = $gallery[$i];
echo "<img src='". $temp->path . "' />";
if (($x%3)==0) { echo "<br />"; }
}
I just redid it with tables, it's much neater, because each thing will be formatted to look correctly.
It's kind of messy because I just added a little if statement to release the tables.
<table>
<?php
$number_per_row = 3;
for($i=0; $i<count($gallery);$i++)
{
$temp = array();
$temp = $gallery[$i];
if(($i % $number_per_row) == 0) {
echo "<tr>";
}
?>
<td><?php echo "<img src='". $temp->path . "' />"; ?></td>
<?php
if(($i % $number_per_row) == $number_per_row - 1) {
echo "</tr>";
}
}
?>
</table>
$n = 3;
echo "<table>";
echo "<tr>";
for($i=0; $i<count($gallery);$i++){
if(($i % n ==0) && ($i != 0)){
echo "</tr><tr>";
}
$temp = array();
$temp = $gallery[$i];
echo "<td><img src='". $temp->path . "' /></td>";
}
echo "</tr>";
echo '</table>';``

mysqli/PHP - first two rows never show up(table with row data going across columns then moving down)

My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>

Adding numbered rows/serial numbers to database query result set

Hello i'm having problems adding numbered rows/serial numbers to my database query result. I've used $number to collect the actual number of rows.
Then another problem i'm trying to avoid is: Numbering of Column Headers.
Thanks for the help.
<?php
$number = mysql_num_rows($query);
for ($serial = 0; $serial < $number; $serial++)
{
echo "<tr>". $serial ."</tr>";
}
for ($i = 0; $i < $number_cols; $i++)
{
echo "<th>" . mysql_field_name($query, $i) . "</th>\n";
}
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
for ($i = 0; $i < $number_cols; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{
echo "NULL";
}
else
{
echo $row[$i];
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo "</span>";
echo "</div>";
?>
trying my best to get something sane out of your terrific code.
<?php
$serial = 1;
while ($row = mysql_fetch_row($query))
{
echo "<tr align=center>\n";
echo "<td>";
echo $serial++;
echo "</td>\n";
foreach ($row as $value)
{
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}

PHP looping problem?

I have this script that displays a max of 5 images for each row, but for some reason my <ul> tag won't close correctly if the number of items isn't an exact multiple of 5. How can I correct this problem so the <ul> tag will close even if the number of listed images is less then 5?
Here is my PHP code.
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
echo "</ul>";
}
$row_count++;
}
}
below the loop, check if
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
echo "</ul>";
}
$row_count++;
}
if ( (($row_count % 5) > 0) && (($row_count % 5) < 4))
echo "</ul>";
}
$multiple = false;
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$row_count = 0;
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
if($row_count % 5 == 4) {
$multiple = true;
echo "</ul>";
} else {
$multiple = false;
}
$row_count++;
}
if($multiple == false) {
echo "</ul>";
}
}
if (!$dbc) {
print mysqli_error($mysqli);} else {
$row_count = 0;
//tank start
$total_rows = mysqli_num_rows($dbc);
//tank end
while($row = mysqli_fetch_array($dbc)){
if($row_count % 5 == 0){
echo "<ul>";
}
echo "<li><a href='" .$row["url"]. "' title='".$row['title']."'>";
echo "<img src='".$row['src']."'></a></li>";
//tank start
if($row_count % 5 == 4 || $row_count==$total_rows) {
//tank end
echo "</ul>";
}
$row_count++;
}
Here's my updated solution. I think it looks a little clearer. I do setup a few variables to get rid of some IF statements, and replace them with FOR loops. Mainly for readability and it's the way i thought of doing it.
$itemsperrow = 5;
$items = mysqli_num_rows($dbc);
$rows = $items / $itemsperrow;
$itemcount = 0;
if (!$dbc)
{
echo(mysqli_error($mysqli));
}
else
{
for ($int = 0; $int < $rows; $int++)
{
echo "<ul>";
for ($item = 0; $item < $itemsperrow; $item++)
{
if ($itemcount >= $items)
{
echo "</ul>";
exit;
}
else
{
$row = mysqli_fetch_array($dbc);
echo "<li><a href='". $row["url"] . "'>";
echo "<img src='" . $row["src"] . "' title='" . $row["title"] . "'/></a></li>";
$itemcount++;
}
}
echo "</ul>";
}
}

Categories