Jump to content

Recommended Posts

Hello,

 

I have a picture gallery that will automatically add pictures if they have the right name and all i have to do is add rows to my loop. Right now i have it so there are four thumbnails per row.  The problem is that when the number is not divisible by 4 evenly it will still add pictures in a number that is evenly divisible by four but they will just be dead images.  How would i go about making if there is not a number of pictures that is even divisible by four to make it not add the extra dead images. here is the code.

<?php
$num = 1;
print "<table border=\"0\">\n";
for ($y = 1; $y <= 15; $y++)  {
    print "<tr>\n";
    for ($x=1; $x<=4; $x++)  {
       print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
       ++$num;
     }
     print "</tr>\n";
}
print "</table>";
?>

 

I know what i have said might be a little confusing, and i am sorry for that.

 

thanks in advance,

Craig

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/
Share on other sites

Im not quite getting it but im trying to guess. With divisible by 4 u mean having lets say 16 images so it shows them in 4 rows with 4 images in a row? So if u have 15 u want 3 rows with 4 images and one with 3 images? If so which varialbe should be divisible by 4? Anyway u can try this:

 

if(file_exists('pages/gallery/$num.jpg')){
   //print the image
}

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-339039
Share on other sites

Yea i think you understand it, but when i tried using that it wont display anything now.  It is very likely i placed it in the wrong place. Any advice?

 

<?php
$num = 1;
print "<table border=\"0\">\n";
for ($y = 1; $y <= 15; $y++)  {
    print "<tr>\n";
    for ($x=1; $x<=4; $x++)  {
      if(file_exists('pages/gallery/$num.jpg')){
   //print the image
       print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" 

src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" 

height=\"90\"></a>";
  }
       ++$num;
       
     }
     print "</tr>\n";
}
print "</table>";
?>

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-339043
Share on other sites

Alright i looked at that and it seems pretty similar to something i've done before.  This is what i have now.

 

while ($newarray = 55) {


  if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row
   echo "<TD>";
   print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
echo "</TD>\n";
   $count++;
   if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}
if ($count % NUMCOLS != 0) {
  # end row if not already ended
  while ($count++ % NUMCOLS) echo "<td> </td>";
  echo "</TR>\n";
}
echo "</TABLE>";

 

But whenever i run it, it freezes up firefox, which makes me belive that it actually has no limits and jsut continues through the loop forever.  Also what does show up is that its just one column.  Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340603
Share on other sites

whoops i meant to have

define ("NUMCOLS",2);
$count = 0;
echo "<TABLE border=1>";

while ($newarray = 55) {


  if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row
   echo "<TD>";
   print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
echo "</TD>\n";
   $count++;
   if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}
if ($count % NUMCOLS != 0) {
  # end row if not already ended
  while ($count++ % NUMCOLS) echo "<td> </td>";
  echo "</TR>\n";
}
echo "</TABLE>";

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340623
Share on other sites

should this work a little better?

define ("NUMCOLS",4);
$count = 0;
echo "<TABLE border=1>";

while ($newarray >= 55) {


  if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row
   echo "<TD>";
   print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" 

src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" 

height=\"90\"></a>";
echo "</TD>\n";
   $count++;
   if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}
if ($count % NUMCOLS != 0) {
  # end row if not already ended
  while ($count++ % NUMCOLS) echo "<td> </td>";
  echo "</TR>\n";
  $newarray++;
}
echo "</TABLE>";

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340741
Share on other sites

Ok scratch that last post, i found lots of errors.  Now it is working but instead of cycling through all the pictures it just continually shows image 1 for every place. here is the code now

define ("NUMCOLS",4);
$count = 0;
$num = 1;
echo "<TABLE border=1>";
$newarray = 1;
while ($count <= 55) {


  if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row
   echo "<TD>";
   print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" 

src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" 

height=\"90\"></a>";
echo "</TD>\n";
   $count++;
   if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}
if ($count % NUMCOLS != 0) {
  # end row if not already ended
  while ($count++ % NUMCOLS) echo "<td> </td>";
  echo "</TR>\n";
  $newarray++;
  ++$num;
}
echo "</TABLE>";[\code]

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340751
Share on other sites

I am totally trying to rewrite this and am getting some errors that i can not figure out. it is saying Parse error: syntax error, unexpected $end on like 37, which is the last line of the file. here is the complete file.

 

<html>
<head>
<title></title>
</head>
<body>
<font color="FFFFFF">
<center>
<?php

$numcols = 4;
$count = 0;
$num = 1;
echo "<table><tr>";
while ($count <= 55) {

   if ($count % $numcols == 0) {
   echo "</tr>";
   echo "<tr>\n";
   echo "<td>";
   echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
   echo "</td>";
   $count++;
   $num++;
   
   } elseif ($count % $numcols != 0) {
   echo "<td>";
   echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
   echo "</td>;
   $count++;
   $num++;
   }
}


?></tr></table></center></font>
</body>
</html>

 

any help would be great, thanks.

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342549
Share on other sites

Try this.  if ... else is adequate, you don't need if elseif:

 

<html>
<head>
<title></title>
</head>
<body>
<font color="FFFFFF">
<center>
<?php
$numcols = 4;
$count = 0;
$num = 1;
echo "<table><tr>";
while ($count <= 55) {
if ($count % $numcols == 0) {
	echo "</tr>";
	echo "<tr>\n";
	echo "<td>";
	echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
	echo "</td>";
} else {
	echo "<td>";
	echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
	echo "</td>;
}
$count++;
$num++;
}
?>
</tr></table></center></font>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342563
Share on other sites

Ok that got it working but its still adding the extra dead image. So what i am trying is to say is that if it exists put the picture but if it doesnt put  . But i think somehtign is wrong with my file_exists() because its saying none of them exist. Here is my code.

<html>
<head>
<title></title>
</head>
<body>
<font color="FFFFFF">
<center>
<?php

$numcols = 4;
$count = 0;
$num = 1;
echo "<table><tr>";
while ($count <= 55) {

   if ($count % $numcols == 0) {
   echo "</tr>";
   echo "<tr>\n";
   echo "<td>";
    if (file_exists('pages/gallery$num.jpg')) {
   echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
    } else {
     echo " ";
    }
   echo "</td>";
   $count++;
   $num++;
   
   } else {
   echo "<td>";
    if (file_exists('pages/gallery$num.jpg')) {
   echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>";
    } else {
     echo " ";
    }
   echo "</td>";
   $count++;
   $num++;
   }
}


?></tr></table></center></font>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342976
Share on other sites

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.