Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.