• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

Display limited to 4 in one row, PHP

This is the code, but I kept looping 1 image for 4 times before I switch to another image. Help?


<table>
<?php while($result1 = mysql_fetch_array($myrql )) { ?>

<?php
$desired_nr_of_images = 4;
for ($i = 0; $i < $desired_nr_of_images; $i++) {
    if ($i % 4 == 0) {
        if ($i > 0) {
            echo '</td></tr>';
        }

        echo '<tr><td>';
    }
?>
     
<img src="<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>" width="200" height="97" onclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=400;this.width=359" ondblclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=97;this.width=200" /><br />
<input type="radio" name="image" id="image" value="<?php echo $result1['templateno'];?>"> Template <?php echo $result1['templateno'];?><br /><span id="errorsDiv_image"></span>


<?php
}
?>

<?php } ?>

</table>
0
rolandmy
Asked:
rolandmy
  • 4
  • 3
  • 2
1 Solution
 
Pratima PharandeCommented:
<table>
<?php
$rowsfound = mysql_num_rows($myrql);
$count=0
while($result1 = mysql_fetch_array($myrql )) { ?>

<?php
$desired_nr_of_images = 4;
for ($i = 0; $i < $desired_nr_of_images; $i++) {
$count = $count +1
    if ($i % 4 == 0) {
        if ($i > 0) {
            echo '</td></tr>';
        }

        echo '<tr><td>';
    }
?>
     
<img src="<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>" width="200" height="97" onclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=400;this.width=359" ondblclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=97;this.width=200" /><br />
<input type="radio" name="image" id="image" value="<?php echo $result1['templateno'];?>"> Template <?php echo $result1['templateno'];?><br /><span id="errorsDiv_image"></span>


<?php
if ( $count < $rowsfound)
$result1 = mysql_fetch_array($myrql )
}
?>

<?php } ?>

</table>
0
 
rolandmyAuthor Commented:
That just gives me row by row with 1 result in each row. So, I updated the code once again. This is what I want (the code below) BUT result no.4 is not showing...it is showing:

row 1: result1 result2 result3
row 2: result 5

Code as below:

<table>
<?php while($result1 = mysql_fetch_array($myrql )) { ?>

<?php
                 echo "<tr>";
                         //echo "<td>";
                 for( $i=0; $i<$desired_nr_of_images; $i++ )
                   {
                                 echo "<td>";
                                 if (!empty($result1["templateno"]) ) {
                                 ?>
                       <img src="<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>" width="200" height="97" onclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=400;this.width=359" ondblclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=97;this.width=200" /><br />
<input type="radio" name="image" id="image" value="<?php echo $result1['templateno'];?>"> Template <?php echo $result1['templateno'];?><span id="errorsDiv_image"></span></td>
                       <?php
                                 }
                     $result1 = mysql_fetch_array($myrql );
                   }          // ends for loop               // inserted line
                 //echo "</td>";
                         echo "</tr>";
                         
              }              // ends while loop
              echo "</table>";
?>
0
 
Pratima PharandeCommented:
<?php while($result1 = mysql_fetch_array($myrql )) { ?>

<?php
                 echo "<tr>";
                         //echo "<td>";
                 for( $i=0; $i<$desired_nr_of_images; $i++ )
                   {
                                 echo "<td>";
                                 if (!empty($result1["templateno"]) ) {
                                 ?>
                       <img src="<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>" width="200" height="97" onclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=400;this.width=359" ondblclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=97;this.width=200" /><br />
<input type="radio" name="image" id="image" value="<?php echo $result1['templateno'];?>"> Template <?php echo $result1['templateno'];?><span id="errorsDiv_image"></span></td>
                       <?php
                                 }
if ($desired_nr_of_images < 4)
                     $result1 = mysql_fetch_array($myrql );
                   }          // ends for loop               // inserted line
                 //echo "</td>";
                         echo "</tr>";
                         
              }              // ends while loop
              echo "</table>";
?>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rolandmyAuthor Commented:
Same result as what I got from my 2nd posting. Result is:

My problem:

row 1: result1 result2 result3
row 2: result5

Why?

I have a total of 5 results in the database. My problem = result4 is missing.

I want:

row 1: result1 result2 result3
row 2: result4 result 5
0
 
Pratima PharandeCommented:
<?php while($result1 = mysql_fetch_array($myrql )) { ?>

<?php
                 echo "<tr>";
                         //echo "<td>";
                 for( $i=0; $i<$desired_nr_of_images; $i++ )
                   {
                                 echo "<td>";
                                 if (!empty($result1["templateno"]) ) {
                                 ?>
                       <img src="<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>" width="200" height="97" onclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=400;this.width=359" ondblclick="this.src='<?php echo $result1['url'];?><?php echo $result1['templateno']."b.jpg";?>';this.height=97;this.width=200" /><br />
<input type="radio" name="image" id="image" value="<?php echo $result1['templateno'];?>"> Template <?php echo $result1['templateno'];?><span id="errorsDiv_image"></span></td>
                       <?php
                                 }
if ($i < 3)
                     $result1 = mysql_fetch_array($myrql );
                   }          // ends for loop               // inserted line
                 //echo "</td>";
                         echo "</tr>";
                         
              }              // ends while loop
              echo "</table>";
?>
0
 
Pratima PharandeCommented:
try this

if ($i < 3)
                     $result1 = mysql_fetch_array($myrql );

OR

if ($i < 2)
                     $result1 = mysql_fetch_array($myrql );
0
 
rolandmyAuthor Commented:
if ($i < 2) works like a charm. Thanks a lot!
0
 
darren-w-Commented:
<?php

$a = array("one", "two", "three", "four", "five");
for ($b = 0; $b < @(3 % count($a)+1); $b++) {
echo "<pre>";
    print_r(array_splice($a, 0, 3));
    echo "</pre>";
}
?>

Open in new window


Array
(
    [0] => one
    [1] => two
    [2] => three
)
Array
(
    [0] => four
    [1] => five
)

0
 
darren-w-Commented:
A refinement
<?php

$split = 4;

$a = array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten");
$count = ceil(count($a) / $split);
for ($b = 0; $b < $count; $b++) {
    echo "<pre>";
    print_r(array_splice($a, 0, $split));
    echo "</pre>";
}
?>

Open in new window


Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)
Array
(
    [0] => five
    [1] => six
    [2] => seven
    [3] => eight
)
Array
(
    [0] => nine
    [1] => ten
)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now