Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How can I do this loop with 4 cells across?

I have a loop that displays several photos. I need to display those photos four across before the cycle continues with another row.

How do I do that?

I have the code that I'm using below. Any help would be greatly appreciated!
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
				
						<?php
 
						include ("carter.inc");
 
						$cxn = mysqli_connect($host,$user,$password,$database)
						or die ("couldn't connect to server");
	
						$query = "select * from news order by article_date";
						$result = mysqli_query($cxn, $query)
						or die ("Couldn't execute query.");
				
						while ($row=mysqli_fetch_assoc($result))
						{
						extract($row);
						?>
				
						<tr>
						<td align="center">
						<A HREF="<?php echo "$url"; ?>" target="_blank"><IMG SRC="<?php echo "$url"; ?>" border="0" alt="<?php echo $description"; ?></a>
</td>
	</tr>
		 <?php
						}
						?>
						</table>

Open in new window

Avatar of psimation
psimation
Flag of South Africa image

<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
                               
                                                <?php
 
                                                include ("carter.inc");
 
                                                $cxn = mysqli_connect($host,$user,$password,$database)
                                                or die ("couldn't connect to server");
       
                                                $query = "select * from news order by article_date";
                                                $result = mysqli_query($cxn, $query)
                                                or die ("Couldn't execute query.");
                               
                                                while ($row=mysqli_fetch_assoc($result))
                                                {
                                                extract($row);
                                                ?>
                               
                                                <tr>
                                                <td align="center">
                                                <A HREF="<?php echo "$url"; ?>" target="_blank"><IMG SRC="<?php echo "$url"; ?>" border="0" alt="<?php echo $description?>"; ?></a>
</td>
        </tr>
                 <?php
                                                }
                                                ?>
                                                </table>
Sorry missed that ?> ...
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
                               
                                                <?php
 
                                                include ("carter.inc");
 
                                                $cxn = mysqli_connect($host,$user,$password,$database)
                                                or die ("couldn't connect to server");
       
                                                $query = "select * from news order by article_date";
                                                $result = mysqli_query($cxn, $query)
                                                or die ("Couldn't execute query.");
                               
                                                while ($row=mysqli_fetch_assoc($result))
                                                {
                                                extract($row);
                                                ?>
                               
                                                <tr>
                                                <td align="center">
                                                <A HREF="<?php echo "$url"; ?>" target="_blank"><IMG SRC="<?php echo "$url"; ?>" border="0" alt="<?php echo $description?>"</a>
</td>
        </tr>
                 <?php
                                                }
                                                ?>
                                                </table>

Open in new window

OK, wait - eveidently it's been a long day - I see now what you want to do - give me a couple of minutes...
OK, can you try this:

<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
                                
                                                <?php
 
                                                include ("carter.inc");
 
                                                $cxn = mysqli_connect($host,$user,$password,$database)
                                                or die ("couldn't connect to server");
        
                                                $query = "select * from news order by article_date";
                                                $result = mysqli_query($cxn, $query)
                                                or die ("Couldn't execute query.");
                                                $count = "1";
                                                ?>
                                                <tr>
                                                <?php
                                                while ($row=mysqli_fetch_assoc($result))
                                                {
                                                extract($row);
                                                ?>                                                                            
                                                
                                                if (bcmod($count,4)) {
                                                echo "</tr><tr>";
                                                } else {
                                                ?>
                                                
                                                <td align="center">
                                                <A HREF="<?php echo "$url"; ?>" target="_blank"><IMG SRC="<?php echo "$url"; ?>" border="0" alt="<?php echo $description?>"; ?></a>
                                                </td>
                                                ?>
                                                
                 <?php
                                                $count++;
                                                }
                                                ?>
                                                </table>

Open in new window

Avatar of Bruce Gust

ASKER

I think we're close...

Here's what I've got based on your input. As of now, I'm still getting only one picture per row, so I'm obviously missing something but I don't know what.

<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?php

include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
      
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
                        
while ($row=mysqli_fetch_assoc($result))
{extract($row);
 if (bcmod($count,4)) {
 echo "</tr><tr>";
 } else {
 ?>
<td align="center">
<A HREF="<?php echo "$url"; ?>" target="_blank"><IMG SRC="<?php echo "$url"; ?>" border="0" alt="<?php echo "$description"; ?>" width="150"></a>
</td>
</tr>
 <?php
}
 $count++;
}
?>
</table>
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?php
 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
	
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
				
while ($row=mysqli_fetch_assoc($result))
{extract($row);
 if (bcmod($count,4)) {
 echo "</tr><tr>";
 } else {
 ?>
<td align="center">
<A HREF="<?php echo "$url"; ?>" target="_blank"><IMG SRC="<?php echo "$url"; ?>" border="0" alt="<?php echo "$description"; ?>" width="150"></a>
</td>
</tr>
 <?php
}
 $count++;
}
?>
</table>

Open in new window

I think my if statement might be broken, try
if (bcmod($count,4) == "0") {

instead of

if (bcmod($count,4) ) {

Sorry for all the errors - I don't have test server to run the script now myself, so I'm "compiling" in my head...

The basic idea is to check for each 4th row, then make a new <tr> , else you make <td>'s

The bcmod basically checks for multiples of 4 to determine if it should be a td or a tr in the loop.

I'm just not sure if this will now skip a row in your db - something tells me it may just since each time the while loops, it steps one row ahead, and if we then echo tr's in that loop, it will "miss" the details in that loop.

if it does skip a row, then we may need to add a mysql_data_seek() in the if statement when it picks up a multiple of 4 to one row back ...
OK, also, from that last code you posted...

the <tr> should start outside the loop as per my example, and there should be no </tr> inside the loop as you have now on line 21 - the "if" statement should take care of that...

Let me know if the modified If statement helps, and if it skips rows...
Avatar of NerdsOfTech
Tip: Once you state your first <?php ?>
all you have to do for subsequent php code is <? ?>

I fixed the code big time and took extra time to post comments

=NerdsOfTech
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?php
 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=1; // initialize count
echo '<tr>'; // first row start
 
while ($row=mysqli_fetch_assoc($result))
{extract($row);
 
?>
 
<td align="center">
<A HREF="<? echo "$url"; ?>" target="_blank"><IMG SRC="<? echo "$url"; ?>" border="0" alt="<? echo "$description"; ?>" width="150"></a>
</td>
 
<?
}
 
 if (bcmod($count,4)) { 
  echo "</tr><tr>"; // if modulus of count is 0 then end count=result of mod: row 1=1, 2=2, 3=3, 4=0 end row, 5=1, 6=2, 7=3, 8=0 end row
 }
$count++;
}
?>
</table>

Open in new window

PERFECTED...
Fixed 2 logic errors. USE THIS COPY
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?php
 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=1; // initialize count
echo '<tr>'; // first row start
 
while ($row=mysqli_fetch_assoc($result))
{
extract($row);
?>
 
<td align="center">
<A HREF="<? echo "$url"; ?>" target="_blank"><IMG SRC="<? echo "$url"; ?>" border="0" alt="<? echo "$description"; ?>" width="150"></a>
</td>
 
<?
 if (bcmod($count,4)) { 
  echo "</tr><tr>"; // if modulus of count is 0 then end count=result of mod: row 1=1, 2=2, 3=3, 4=0 end row, 5=1, 6=2, 7=3, 8=0 end row
 }
$count++;
}
?>
</table>

Open in new window

On the safe side don't use extract function
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?php
 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=1; // initialize count
echo '<tr>'; // first row start
 
while ($row=mysqli_fetch_assoc($result))
{
extract($row);
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (bcmod($count,4)) { 
  echo "</tr><tr>"; // if modulus of count is 0 then end row
 }
$count++; // increment count
}
?>
</table>

Open in new window

Yea, that last code snippet looks good - also won;t cause skipped rows...
Guys...

Head out to http://hihatwebdesign.com/Showdown/Gallery.php and see what the last suggestion produces. I think we're close,but I'm still missing something...
OK, For starters,

line 26 of NerdofTechs version is a potential culprit:

  echo "</tr><tr>"; // if modulus of count is 0 then end row

That is OK *if* there is data for a new row.

If however we have gone through the last loop, then it should only be </tr>...

I'm going out now so I won;t be able to work on this now - but maybe you can play around with it so long...
1 logic error Fixed:

Change:
 if (bcmod($count,4)) {  // executes on mod = 1,2,3 not 0

To:
 if (!bcmod($count,4)) { // executes on mod IS 0 :)

Use this code instead
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?php
 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=1; // initialize count
echo '<tr>'; // first row start
 
while ($row=mysqli_fetch_assoc($result))
{
extract($row);
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (!bcmod($count,4)) 
 { 
  echo "</tr><tr>"; // if modulus of count IS = 0 then end row
 }
$count++; // increment count
}
?>
</table>

Open in new window

In other words
If (!0) { // execute on 0 beacuse NOT 0 = True
That is why you had two images on row 4

Here is the fixed code repeated from ID # 22908101 Author: NerdsOfTech =
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?php
 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=1; // initialize count
echo '<tr>'; // first row start
 
while ($row=mysqli_fetch_assoc($result))
{
extract($row);
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (!bcmod($count,4)) 
 { 
  echo '</tr><tr>'; // if modulus of count IS = 0 then end row
 }
$count++; // increment count
}
?>
</table>

Open in new window

MASTERED and PERFECTED

=NerdsOfTech
<?php
?>
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<?
 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=1; // initialize count
echo '<tr>'; // first row start
 
while ($row=mysqli_fetch_assoc($result))
{
extract($row);
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (!bcmod($count,4)) 
 { 
  echo '</tr><tr>'; // if modulus of count IS = 0 then end row
 }
$count++; // increment count
}
?>
</table>

Open in new window

The re are still 2 problems that could occur:

1) if there isn't an exact multiple of 4 results from the query, ie, let's say there are 26 photos in the table.
This will "break" the table cause you will short 2x td in the last row.
2) The last row is still "broken" because you open a new<tr> on line 29 - that *might* not be critical, but I think the problem as per point 1 would definately cause the resulting table to display wrong?
*ENHANCED*

Fix VARIABLE for $maxcols to specify max columns
Fix NO TABLE: on NO DATA
Fix FILL: UNPOPULATED ROWS OF $maxcols

Let me know
<?php
 
$maxcols = 4; 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=0; // initialize count
 
while ($row=mysqli_fetch_assoc($result))
{
$count++; // increment count
?>
 
 if ($count == 1)
 { // initalize table
?>
 
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<tr>
 
<?
 }
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (!bcmod($count,$maxcols)) 
 { // if modulus of count is = 0 then end row
  echo '</tr><tr>'; 
 }
 
}
 
if ($count)
{ // data exists
 $fill = bcmod($count,$maxcols) // current column
 if (!$fill){ // if not last column already fill in columns
  for ($i = $fill; $i <= $maxcols; $i++){
   echo '<td>&nbsp;</td>';
  }
  echo '</tr>';
 }
 echo '</table>';
}
?>

Open in new window

1 Syntax Error fixed:
DONT USE ABOVE CODE

USE THIS INSTEAD

Test and let me know to results
<?php
 
$maxcols = 4; 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=0; // initialize count
 
while ($row=mysqli_fetch_assoc($result))
{
$count++; // increment count
 
 if ($count == 1)
 { // initalize table
?>
 
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<tr>
 
<?
 }
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (!bcmod($count,$maxcols)) 
 { // if modulus of count is = 0 then end row
  echo '</tr><tr>'; 
 }
 
}
 
if ($count)
{ // data exists
 $fill = bcmod($count,$maxcols) // current column
 if (!$fill){ // if not last column already fill in columns
  for ($i = $fill; $i <= $maxcols; $i++){
   echo '<td>&nbsp;</td>';
  }
  echo '</tr>';
 }
 echo '</table>';
}
?>

Open in new window

1 Logic error fixed:
USE THIS CODE INSTEAD
<?php
 
$maxcols = 4; 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=0; // initialize count
 
while ($row=mysqli_fetch_assoc($result))
{
$count++; // increment count
 
 if ($count == 1)
 { // initalize table
?>
 
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<tr>
 
<?
 }
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (!bcmod($count,$maxcols)) 
 { // if modulus of count is = 0 then end row
  echo '</tr><tr>'; 
 }
 
}
 
if ($count)
{ // data exists
 $fill = bcmod($count,$maxcols) // current column
 if ($fill){ // if not last column already fill in blank columns
  for ($i = $fill; $i <= $maxcols; $i++){
   echo '<td>&nbsp;</td>';
  }
  echo '</tr>';
 }
 echo '</table>';
}
?>

Open in new window

1 Logic error fixed for loop: MAX from $maxcols to ($maxcols - 1 =3)
data in col 1= 3 blank columns... $fill = 1 to ($maxcols - 1 =3) run 3 steps
data in col 2= 2 blank columns... $fill = 2 to ($maxcols - 1 =3) run 2 steps
data in col 3= 1 blank columns... $fill = 3 to ($maxcols - 1 =3) run 1 steps
<?php
 
$maxcols = 4; 
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");
        
$query = "select * from photos order by photo_date";
$result = mysqli_query($cxn, $query)
or die ("Couldn't execute query.");
 
$count=0; // initialize count
 
while ($row=mysqli_fetch_assoc($result))
{
$count++; // increment count
 
 if ($count == 1)
 { // initalize table
?>
 
<table width="600" border="0" align="center" cellspacing="0" cellpadding="0">
<tr>
 
<?
 }
?>
 
<td align="center">
<A HREF="<? echo $row['url']; ?>" target="_blank"><IMG SRC="<? echo $row['url']; ?>" border="0" alt="<? echo $row['description']; ?>" width="150"></a>
</td>
 
<?
 if (!bcmod($count,$maxcols)) 
 { // if modulus of count is = 0 then end row
  echo '</tr><tr>'; 
 }
 
}
 
if ($count)
{ // data exists
 $fill = bcmod($count,$maxcols) // current column
 if ($fill){ // if not last column already fill in blank columns
  for ($i = $fill; $i <= ($maxcols -1); $i++){
   echo '<td>&nbsp;</td>';
  }
  echo '</tr>';
 }
 echo '</table>';
}
?>

Open in new window

I'm getting this error:

Parse error: syntax error, unexpected T_IF in /home/hihatweb/public_html/Showdown/Gallery.php on line 95

Line 95 is the line where it says "if ($fill){ // if not last column already fill in blank columns"
ASKER CERTIFIED SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try above code this should be a winner