Page 1 of 1

Array oh yay

Posted: Wed Jun 16, 2004 8:31 pm
by harkeyahh
For the purpose of letting everyone get what i'm trying to say i'm going to post my entire lines of script.

Code: Select all

$url = "http://www.harkeyahh.com/gallery/images/" ;

$dirname = "images/";
$dir = opendir($dirname);

while(false !=($file = readdir($dir) ) )
{if ( ("$file" != ".") AND ("$file" != "..") )

{
$harkeyfile[] = $file;
$file_list .="<li>$file</li>";

}
}
closedir($dir);
//Files in images are now in an array 

sort($harkeyfile);

$mode = current($harkeyfile); // $mode = 0

list($width, $height, $type, $attr) = getimagesize("$url$mode");
echo"<img src="$url$mode" $attr />";

while( list( $element ) = each( $harkeyfile ) )
echo "<a href="$PHP_SELF?">$element<br /></a>";

$mode = next($harkeyfile);    // $mode = 'bike';

list($width, $height, $type, $atrib) = getimagesize("$url$harkeyfile");
echo"<img src="$url$harkeyfile" $atrib />";

echo $mode . "\n";
echo "<a href="$PHP_SELF?$mode"> Next maybe?</a>";


$result = count($harkeyfile);
echo "Currently there are: $result images in the gallery";
?>
Now All I want to do is move forward and backward in the array. next() and prev() work i'm sure, but I don't know how to go about making a loop or using the functions in a link that would work for the task i'm trying to pull off. I'd like to keep the array on the same page since I plan on have a lot of images.

Posted: Wed Jun 16, 2004 8:51 pm
by feyd
from what I remember:

while( list( $element ) = each( $harkeyfile ) )
echo "<a href=\"$PHP_SELF?\">$element<br /></a>";

after that, next() will return null/false.

Posted: Wed Jun 16, 2004 9:13 pm
by harkeyahh
Soo your saying i need next();

before...
while( list( $element ) = each( $harkeyfile ) )
echo "<a href=\"$PHP_SELF?\">$element<br /></a>";
?

Hmm but what about the next prev link? how can that be accomplished?

Posted: Wed Jun 16, 2004 9:27 pm
by dull1554
maybe a little class or something if you have not had any experience with them search this forum, there are many valid topics about classes
but here we go

Code: Select all

<?php
class array_handler {
	var $array;
	var $place = 0;
	
	function define_array($array1) {
		$this->array = $array1;
	}
	function forward($num = 1) {
		$this->place = $this->place + $num;
		return $this->array[$this->place];
	}
	function backward($num = 1) {
		$this->place = $this->place - $num;
		return $this->array[$this->place];
	}
	function print_array_location() {
		print $this->array[$this->place];
	}
}

$array_handler = new array_handler;
$array_handler->define_array(array(1,2,3,4,5,6,7,8,9,10));
$array_handler->forward();
$array_handler->print_array_location();
$array_handler->backward();
$array_handler->print_array_location();
?>