Page 1 of 1

image limiting!

Posted: Sat Jul 03, 2004 10:37 am
by Joe
I was curious to know if anyone knew any techiques to read records from the database and making a row of 6 images with the number of columns depending on how many images existed. A perfect example is:

http://racresearch.com/

Regards


Joe 8)

Posted: Sat Jul 03, 2004 10:50 am
by feyd
something like the following..

Code: Select all

<?php

for($x = 0, $y = count($images); $x < $y; $x++)
{
  if($x % 6 == 0)
    echo '<tr>';
  echo '<td><img src="'.$images[$x].'" /></td>';
  if($x % 6 == 5)
    echo '</tr>';
}

$y = ($y + 5) % 6;
if($y != 5)
  echo '<td colspan="'.($y+1).'">&'.'nbsp;</td></tr>';

?>
[edit]tweak to get the nbsp to show..

Posted: Sat Jul 03, 2004 10:55 am
by Joe
OK Thanks alot feyd I will give that a shot!

Posted: Sat Jul 03, 2004 11:16 am
by redmonkey

Code: Select all

<table width="342" border="0" cellspacing="0" cellpadding="0">
  <tr>
<?php
$image[] = 'image1.jpg';
$image[] = 'image2.jpg';
$image[] = 'image3.jpg';
$image[] = 'image4.jpg';
$image[] = 'image5.jpg';
$image[] = 'image6.jpg';
$image[] = 'image7.jpg';
$image[] = 'image8.jpg';
$image[] = 'image9.jpg';
$image[] = 'image10.jpg';
$image[] = 'image11.jpg';
$image[] = 'image12.jpg';
$image[] = 'image13.jpg';
$image[] = 'image14.jpg';
$image[] = 'image15.jpg';

$cols = 6;
$num_pics = count($image) -1;
$i = 0;

while ( $i <= $num_pics )
{
  $newrow = $i > 0 && (!($i % $cols)) ? "  </tr>\n  <tr>\n" : '';
  echo $newrow;
  echo "    <td><img src="{$image[$i]}"></td>\n";
  $i++;
}

while ($i % $cols)
{
  echo "    <td>&", "nbsp;</td>\n"; // done like that to stop the forum pasring the HTML entity
  $i++;
}

?>
  </tr>
</table>
Outputs...

Code: Select all

&lt;table width="342" border="0" cellspacing="0" cellpadding="0"&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;img src="image1.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image2.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image3.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image4.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image5.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image6.jpg"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;img src="image7.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image8.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image9.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image10.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image11.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image12.jpg"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;img src="image13.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image14.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&lt;img src="image15.jpg"&gt;&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

Posted: Sat Jul 03, 2004 11:20 am
by Joe
I tried feyd's method first and I wasn't sure if I done it right. (infact I think I am way off). The code go's like:

Code: Select all

<?php
$link = mysql_connect("???", "????", "???");
mysql_select_db("???") or die("Could not connect!" . mysql_error());
$sql = "SELECT * FROM images";
$result = mysql_query($sql) or die("Error!");

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $images = $row['image'];
}

for($x = 0, $y = count($images); $x < $y; $x++)
{
  if($x % 6 == 0)
  echo '<tr>';

  echo '<td><img src="'.$images[$x].'" /></td>';

  if($x % 6 == 5)
  echo '</tr>';
}

$y = ($y + 5) % 6;

if($y != 5)
echo '<td colspan=".($y+1)."> </td></tr>';
?>
It only displays 1 image thats not even an image, lol. I will also try your method redmonkey. Thanks guys!

Posted: Sat Jul 03, 2004 11:22 am
by redmonkey

Code: Select all

$images = $row['image'];
Should be...

Code: Select all

$images[] = $row['image'];
EDIT: Note that feyd's code uses $images where mine use $image

Posted: Sat Jul 03, 2004 11:23 am
by feyd
$images[] = $row['image'];

Posted: Sat Jul 03, 2004 11:24 am
by Joe
Thanks again. Worked perfect. (I learned something new!), hehe...