Jump to content

Recommended Posts

I am trying to build my own gallery that reads the files from a folder.

 

What I Need it to do is

Choose only jpgs from the folder.

Display 4 jpgs in a row the insert a line break to display the next for images

 

Here is the code I have so far and it only displays one image over and over again and will not stop looping.

 

 

<?php
$count = 0;
$counter = 0;
$dir = @ opendir("images");
while (($file = readdir($dir)) !== false)
  {
  if(strpos($file, ".jpg"))
{ 
$name[${$counter}] = $file;
$counter++;
}
}

while($count <= $counter)
{
echo "<a href='img.php?file=". $name[${$count}]" /><img src='images/" . $name[${$count}] . "' width='100' style='border:1px solid #e96302;' /></a>";
$count++;
if($count == 4) {
  	echo '<br />';
  	$count = 1;
}
}
  closedir($dir);
?> 

 

im pretty sure the problem lies within the array. $name[${$counter}] = $file;

 

Or has something to do with the $counter variable inside the name array.

 

Please help!

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/
Share on other sites

Ok I included that in some changes I made to the script and that fixed the looping problem all images are displaying but now I am getting an error.

 

Here is the code

 

<?php
$count = 0;
$counter = 0;
$break = 0;
$dir = @ opendir("images");
while (($file = readdir($dir)) !== false)
  {
  if(stristr($file, ".jpg"))
{ 
$name[${"counter"}] = $file;
$counter++;
}
}

while($count <= $counter)
{
echo "<a href=\"img.php?file=$name[$count]\" /><img src=\"images/$name[$count]\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>";
$count++;
$break++;
if($break == 4) {
  	echo '<br />';
  	$break = 0;
}
}
  closedir($dir);
?> 

 

here is the error

 

PHP Notice: Undefined offset: 15 in PATH\TO\FILE\view.php on line 26

PHP Notice: Undefined offset: 15 in PATH\TO\FILE\view.php on line 26

 

it prints out twice.

 

 

Also I noticed that at the end something is coming up blank

 

here is what the script is printing out

 

<a href="img.php?file=Picture 108.jpg" /><img src="images/Picture 108.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 119.jpg" /><img src="images/Picture 119.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 120.jpg" /><img src="images/Picture 120.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 136.jpg" /><img src="images/Picture 136.jpg" width="100" style="border:1px solid #e96302;" /></a><br /><a href="img.php?file=Picture 138.jpg" /><img src="images/Picture 138.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 139.jpg" /><img src="images/Picture 139.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 140.jpg" /><img src="images/Picture 140.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 142.jpg" /><img src="images/Picture 142.jpg" width="100" style="border:1px solid #e96302;" /></a><br /><a href="img.php?file=Picture 143.jpg" /><img src="images/Picture 143.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 144.jpg" /><img src="images/Picture 144.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 145.jpg" /><img src="images/Picture 145.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 146.jpg" /><img src="images/Picture 146.jpg" width="100" style="border:1px solid #e96302;" /></a><br /><a href="img.php?file=Picture 147.jpg" /><img src="images/Picture 147.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 148.jpg" /><img src="images/Picture 148.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=Picture 149.jpg" /><img src="images/Picture 149.jpg" width="100" style="border:1px solid #e96302;" /></a><a href="img.php?file=" /><img src="images/" width="100" style="border:1px solid #e96302;" /></a><br /> 

Any help is appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818475
Share on other sites

Why ya using ${$counter}

if counter = 0

than it becomes $0

 

so fix that

 

why did u escape the first set of quotes, but not the rest in the display loop?

 

<?php
$count = 0;
$counter = 0;
$break = 0;
$dir = @ opendir("images");
while (($file = readdir($dir)) !== false)
{
  if(stristr($file, ".jpg"))
  { 
	$name[$counter] = $file;
	$counter++;
  }
}


while($count <= $counter)
{
  echo "<a href=\"img.php?file={$name[$count]}\" ><img src=\"images\/P\" width=\"100\" style=\"border:1px solid #e96302;\"  alt=\"\"></a>";
$count++;
$break++;
if($break == 4) {
     echo '<br>';
     $break = 0;
  }
}
  closedir($dir);
?> 

 

This code can be optimized a lot. Other from that didnt see anything else

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818506
Share on other sites

I found the problem

 

Then problem was with this

 

while($count <= $counter)
{

 

I switched it to

 

while($count < $counter)
{

 

All errors are gone now. =)

 

Thanks everyone for the help.

 

Here is the final code for anyone reading along.

 

<?php
$count = 0;
$counter = 0;
$break = 0;
$dir = opendir("images");
while (($file = readdir($dir)) !== false)
  {
  if(stristr($file, ".jpg"))
{ 
$name[$counter] = $file;
$counter++;
}
}

while($count < $counter)
{
echo "<a href=\"img.php?file=$name[$count]\" /><img src=\"images/$name[$count]\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>";
$count++;
$break++;
if($break == 4) {
  	echo '<br />';
  	$break = 0;
}
}
  closedir($dir);
?> 

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818514
Share on other sites

Wow!

 

Thanks crayon violet that didn't work at first but I made a few adjustments and it works perfect!

 

Here is what I did

 

$files = glob('images/*.jpg');
$count = 0;
foreach ($files as $file) {
   echo ($count % 4 == 0)? "<br/>" : "";
   $count++;
   echo "<a href=\"img.php?file=$file\" /><img src=\"$file\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>";
}

 

Basically just made $count = 0;

 

and changed the path to the file because the way you had it printed out this way "/image/images/image.jpg"

 

THANKS ALOT!

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818529
Share on other sites

ah yeah, the undefined var notice.  It's not strictly necessary to pre-assign a var like that with php.  It just "let's you know" that that's what you're doing. I always forget about that because I have it set to not report notices.

 

and yeah, sorry, forgot about removing the image/ in the echo since it would already be there from the glob. and on that note, you're probably going to want to change the href to use basename($file) instead of just file.

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818547
Share on other sites

Here's a quick question..

 

Where can I read about how to condense my scripts?

 

I got this entire thing working.

 

Basically Page 1 displays all the images in the folder

 

<?php
$files = glob('images/*.jpg');
$count = 0;
$counter = 0;
foreach ($files as $file) {
   echo ($count % 4 == 0)? "<br/>" : "";
   echo "<a href=\"img.php?file=$file&count=$counter\" /><img src=\"$file\" width=\"100\" style=\"border:1px solid #e96302;\" /></a>";
   $count++;
   $counter++;
}
?> 

 

Once you click on the image it will take you to a page to view the images.

 

<div align="center">
<table width="425" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
<?php 
if(empty($_GET['file'])) {
header('Location:view.php');
} else {
$ifile = $_GET['file'];
$files = glob('images/*.jpg');
$count = $_GET['count'];
$next = $count + 1;
$prev = $count - 1;
$taly = 0;
foreach ($files as $file) {
$taly++;
}
?>
<?php if($_GET['count'] == 0) {
} else {?>
<td width="22"><a href="<?php echo("img.php?file=$files[$prev]&count=$prev"); ?>"><img src='../../../img/prev.png' width='22' height='24' border="0" /></a></td>
<?php }?>
<td width="381"><div align="center"><a href="view.php"><img src='<?php echo $ifile; ?>' border="0" /></a></div></td>
<td width="22"><?php if($_GET['count'] != $taly -1) { ?>
  <a href="<?php echo("img.php?file=$files[$next]&count=$next"); ?>"><img src='../../../img/next.png' width='22' height='24' border="0" /></a>
<?php 

}
}
?></td>
  </tr>
</table>
</div>

 

Thanks to Crayon Violet the first code is nice and tight. The second code is sloppy i think.

 

So are there any good articles on how to make scripts smaller?

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818568
Share on other sites

There are no good articles on that.  That's the art of coding.  It comes with experience.  The more you code, the more you look at it and say hey, there's no reason these two things can't be combined, or maybe you come across in the manual some function or something that happens to already do what those 5 lines of code you wrote out the hard way does.  It's pattern recognition.  There's nothing to memorize about it.  It's a skill that comes with experience.

 

 

Link to comment
https://forums.phpfreaks.com/topic/155440-help-with-arrays/#findComment-818577
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.