Jump to content

struggling with for loops....


plugnz

Recommended Posts

Ok, This is probably php 101 but I just cannot get the for loop to do what I want it to do. I've been trying to display images from the database horizontally on the page. I keep losing the image so I've decided to go back to basics to try it with some text.

What I want is this....

 

1  2  3  4  5  6 7

8 9 10 11 12 13

14 15 16 17 18

 

but i get this...

 

1 1 1 1 1 1 1

2 2 2 2 2 2 2

3 3 3 3 3 3 3

 

the code looks like this...

$y=0;
$x=1;
$i=2;
for ($y=1;$y<=1;$y++) {
  echo "<tr>";
  for ($x=1;$x<=5;$x++) {
    echo "<td>$image_id </td>";
}
echo "</tr>";
}

 

Ant ideas how to make this work???

 

 

Link to comment
https://forums.phpfreaks.com/topic/202101-struggling-with-for-loops/
Share on other sites

Hi BillyBoB,

 

$image_id is set in the database

echo "<table>";
echo "<tr ><td >";
echo "<h1>Category: " . $category_name . " </h1>\n";
echo "</td></tr>";
$ImageDir = "img/category/";
$ImageThumb = $ImageDir . "thumbnails/";
  
  $getpic = "SELECT image_id, image_group, image_name, location_no " .
//$getpic = "SELECT image_id " .
               "FROM cms_images_category " .
		   "WHERE category_name = '" . $_GET['category'] . "'";
               "ORDER BY image_group ";
		   
      $results = mysql_query($getpic,$conn)
        or die(mysql_error());

    while ($row = mysql_fetch_array($results)) {
  extract($row);
$images = $ImageThumb . $image_id . ".jpg";
$y=0;
$x=1;
$i=2;
for ($y=1;$y<=1;$y++) {
  echo "<tr>";
  for ($x=1;$x<=5;$x++) {
    echo "<td>$image_id </td>";
}
echo "</tr>";
}

 

Thanks

Hey thanks, tried it and works great for numbers but when I apply it to the images i get 100 times each image unless I change $ct to 1 and then the images don't wrap they just keep going to the right.

 

$ct=100;
for($i=1;$i<=$ct;$i++)
{
echo "<td><a href =\"".$ImageDir . $image_id . ".jpg\" target='top'>"; 
echo "<img src=\"".$images . "\"></a></td>";
if($i%10==0)echo "</tr>\n";
}

if I try to attach [$i] to end of $images i lose the image path and still get the repeats.

// database query stuff here

echo "<tr>";
while ($row = mysql_fetch_array($results)) {
  extract($row);
  $images = $ImageThumb . $image_id . ".jpg";
  echo "<td><a href =\"".$ImageDir . $image_id . ".jpg\" target='top'>"; 
  echo "<img src=\"".$images . "\"></a></td>";
  echo ($x % 7 == 0)? "</tr><tr>" : "";
  $x++;
}
echo "</tr>";

Cor thats the closest so far... :D

just a few tweaks needed. for some reason the first image seems to want to occupy the whole row by itself, with the rest doing as they're told and then how do I make it so the images only fill the available screen width rather than a select number wide? I want it to fill the screen as much as possible.

 

But thanks every one

 

If you want to fill the whole screen, you have to stop using tables and learn how to use floating divs in css. Here's one tutorial: http://www.w3schools.com/css/css_image_gallery.asp

Another tutorial can be found at http://css.maxdesign.com.au/floatutorial/

 

Ken

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.