Page 1 of 1

moving through Arrays.

Posted: Mon Apr 07, 2003 12:31 pm
by thadeak
I have this code which reads from a directory all the thumbnail jpg images and puts them into an array then displays them. the problem is at the moment I have 25 plus images in the folder but only want to display 5 at a time.
Can anyone tell me how to display them in groups of five and always create a next and back button.

I am totally stuck.

here is my code.

Code: Select all

<?php
$path = 'images/fish/';
$dir = opendir($path); 
if ($dir = opendir($path)) &#123;
	while (false !== ($file = readdir($dir))) &#123; 
		if (preg_match ('/tmb/i', $file)) &#123;
			if ($file != '.' && $file != '..') &#123; 
				if (preg_match('/.jpg$/i', $file)) &#123;
				$image_info&#1111;] = $file; 
				&#125;
		  	&#125; 
		&#125;
	&#125;
	closedir($dir); 
&#125;

foreach ($image_info as $element) &#123;
			echo $element.'<br />';
&#125;
?>
Thanks in advance.

Tha Deak

using javascript

Posted: Mon Apr 07, 2003 1:40 pm
by phpScott
my suggestion would to create a javascript array that takes care of an array of images and using a little more javascript to take care of the forward and back buttons.
I have done this with single images but it shouldn't be to much trouble to modify it to use five images at a time..

Code: Select all

$sql="SELECT picName, picId FROM picture WHERE clientNbr=$clientId AND picId='pg'";
  $db->Exec_Sql($sql);
  $n=$db->Row_Count();
  $myPix=" myPix=new Array(";
  for($y=0; $y<$n; $y++)
  &#123;
     $row=$db->Next_Record();
     $src=$row&#1111;'picName'];
     $myPix.=""$src"";
     if($y<$n-1)
       $myPix.=",";
  &#125;
  $myPix.=");\n";
I have my image names stored in db but you should get the idea of what I am doing.
the $myPix variable is being concatinated together to create a javascript array that I echo out inbetween my <script> tags and use the following js goop to process the picture

Code: Select all

function processPic(n)
&#123;
   oldPic=document.gallery.src
   thisPic+=n;
   //alert("thisPic is "+thisPic+" myPixLength is "+myPix.length);
   if(thisPic >= 0 && thisPic < myPix.length)
   &#123;

      document.gallery.src=path+myPix&#1111;thisPic];
      //alert(document.gallery.src);
   &#125;
   else
   &#123;
      document.gallery.src=oldPic;
      thisPic-=n;
   &#125;
   if(thisPic >= myPix.length-1)
   &#123;
      document.next1.src="images/backgroung.gif";
      document.next2.src="images/backgroung.gif";
   &#125;
   else
   &#123;
      document.next1.src="images/gallery_next_g.gif";
      document.next2.src="images/gallery_next_g.gif";
   &#125;
   if(thisPic <= 0)
   &#123;
      document.prev1.src="images/backgroung.gif";
      document.prev2.src="images/backgroung.gif";
   &#125;
   else
   &#123;
     document.prev1.src="images/gallery_prev_g.gif"
     document.prev2.src="images/gallery_prev_g.gif";;
   &#125;
&#125;
Im sure it could be more elegant but it works.
It takes care of diplaying previous and next buttons and displays a default pic if there is no prev or next.

here is an example of the href around the image.

Code: Select all

<A HREF="javascript:processPic(1)">
              <img src='images/gallery_next_g.gif' border='0'  hspace='0' vspace='0' name="next2"></A>
I just pass in wether to 1 or -1 as the parameter. I don't see why it won't work for doing five pics at a time. You will have to modify the code so thaty you are changing all five pics but that shouldn't be to much effor either.

need more help just post agian

Posted: Mon Apr 07, 2003 5:32 pm
by bionicdonkey
put them into a multi dimensional array
and just get one row at a time

Posted: Tue Apr 08, 2003 12:50 am
by Tubbietoeter
yepp thats easier than that javascript stuff

with the form (forward/next button) submit the starting point (the index to start from) and from there display the next 5 images

Posted: Tue Apr 08, 2003 4:42 am
by twigletmac
Try this:

Code: Select all

<?php
// How many images you want to display at a time
$increment = 5;
// Pass a start variable through the URL so that you know
// which image is the starting one.
$start = (!empty($_GET['start']) && is_numeric($_GET['start']) && $_GET['start'] > 0) ? $_GET['start'] : 1;
// Now determine the ending image
$end = $start + $increment;

$path = 'images/fish'; 

// use this to count all the files
$count = 0;

// Take the line below out as it is uneccessary
/*
$dir = opendir($path); 
*/
// Use @ to suppress errors if the directory cannot be opened
// should probably do a file_exists() before to check that the
// folder exists
if (@$dir = opendir($path)) { 
	// initialise a counter variable
	$i = 1;
	while (false !== ($file = readdir($dir))) { 
		// Put all the conditions in one if statement since they all have to be met
		if (preg_match('/^tmb/i', $file) && $file != '.' && $file != '..' && preg_match('/.jpg$/i', $file)) { 
			// make sure the image is between (or equal to) $start and (or) $end
			if ($i >= $start && $i <= $end) {
				$image_info[] = $file; 
			}
			// increment the counters
			$i++;
			$count++;
		}
	} 
	closedir($dir); 
} 

// Don't show anything if there aren't any files
if ($count > 0) {
	foreach ($image_info as $element) { 
			 echo $element.'<br />'; 
	} 
	// display previous and next links
	if ($start != 1) {
		echo '<a href="test.php?start='.($start-$increment).'"><<< Previous</a>&nbsp;';
	}
	if ($end <= $count) {
		echo '&nbsp;<a href="test.php?start='.($start+$increment).'">Next >>></a>';
	}
} else {
	echo 'no files matching criteria';
}
?>
Needs some refining but could give you a start.

Mac