Jump to content

Distribute results evenly between columns


GalaxyTramp

Recommended Posts

Hi

 

I have a page which returns 2 rows of images max 5 in each.

Code is:

 

<?php
$query = mysql_query("SELECT COUNT(id) FROM " . $prev . "pics where id=" . $id . "");
list($number_records) = mysql_fetch_row($query);

if ($number_records >= 10 )
{
$number =10; //LIMIT IMAGES TO 10
}
else
{
$number =$number_records;
}

echo "<br />";

echo "Displaying $number of $number_records images";

?>



<div class="thumb-right">


        <dl>
   
            <?php for ($i = 0; $i < 5; $i++): ?>
               <dt>
                 <img src="../pics/<?php echo $images[$i]; ?>" alt="" width="120" height="120" class="border1"  /><br /><br />
               </dt>
            <?php endfor; ?>
        
        </dl>



</div>    


<div class="thumb-left">

<dl>
          
            <?php for ($i = 5; $i < $number; $i++): ?>
            <dt>
                  <img src="../pics/<?php echo $images[$i]; ?>" alt="" width="120" height="120" border="0" class="border1" /><br /><br />
            </dt>
            <?php endfor; ?>
         
        </dl>

</div>

 

This works fine if there are 10 images to display, but on occasions there may only be 7 for instance. This then puts 5 in the right column and only 2 in the left column which looks ugly.

 

Can someone suggest a way of calculating the total number of images and then displaying them say 4 in the left column and 3 in the right column for this example?

 

Thanks for your time

 

GT

Dude,

 

Its a small logic as follows:

 

<?php

$query = mysql_query("SELECT COUNT(id) FROM " . $prev . "pics where id=" . $id . "");

list($number_records) = mysql_fetch_row($query);

 

if ($number_records >= 10 )

{

$number =10; //LIMIT IMAGES TO 10

}

else

{

$number =$number_records;

}

 

echo "<br />";

 

echo "Displaying $number of $number_records images";

 

$left_side = '';

$right_side = '';

for($i=0;$i<$number;$i++){

if($i%2 == 0){

$left_side = '<dt>

                <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" class="border1"  /><br /><br />

              </dt>';

}

else{

$right_side = '<dt>

                  <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" border="0" class="border1" /><br /><br />

            </dt>';

}

}

 

 

?>

 

 

 

<div class="thumb-right">

 

 

        <dl>

 

            <?php print $right_side; ?>

       

        </dl>

 

 

 

</div>   

 

 

<div class="thumb-left">

 

<dl>

         

            <?php print $left_side; ?>

       

        </dl>

 

</div>

 

 

Dude,

 

Just replace the for loop with this

 

for($i=0;$i<$number;$i++){

  if($i%2 == 0){

      $left_side .= '<dt>

                <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" class="border1"  /><br /><br />

              </dt>';

  }

  else{

      $right_side .= '<dt>

                  <img src="../pics/'.$images[$i].'" alt="" width="120" height="120" border="0" class="border1" /><br /><br />

            </dt>';

  }

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.