[SOLVED] image limiting!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

image limiting!

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Last edited by feyd on Sat Jul 03, 2004 10:59 am, edited 2 times in total.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

OK Thanks alot feyd I will give that a shot!
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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;
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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!
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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
Last edited by redmonkey on Sat Jul 03, 2004 11:24 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$images[] = $row['image'];
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Thanks again. Worked perfect. (I learned something new!), hehe...
Post Reply