Log in

View Full Version : can i restrict the code to show one image?



wmguk
03-17-2008, 01:56 AM
Hey guys

I am trying to get this script just to show one image, basically at the moment it reads the folder specified as $dir and displays every image in there... how can i restrict the amount of images shown?

this is above the page


<?php
$login = $_GET['login'];
$NBFile=0;
$dir ="clients/$login";

if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$FileArray[] = $dir."/".$file;
$NBFile=$NBFile+1;
}}}
closedir($handle);
?>

On the page



<?php
$NBPicswidth=1;
$NBPics=0;
for ($i=0; $i<$NBFile; $i++
)
{
$image=$FileArray[$i];

echo "<img src='$image'>";

$NBPics=$NBPics+1;
if ( $NBPics==$NBPicswidth )
{
$NBPics=0;
}
}
?>

wmguk
03-17-2008, 02:00 AM
ok, scrub that, ive got it....


$login = $_GET['login'];
$NBFile=1; //CHANGED THIS FROM =0
$dir ="clients/$login";

if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$FileArray[] = $dir."/".$file;
$NBFile=$NBFile; //CHANGED THIS FROM $NBFile=$NBFile+1;

Nile
03-17-2008, 02:01 AM
Try this:


<?php
$NBPicswidth=1;
$NBPics=0;
for ($i=0; $i<2; $i++
)
{ // replace 2 with the ammount of img
$image=$FileArray[$i];

echo "<img src='$image'>";

$NBPics=$NBPics+1;
if ( $NBPics==$NBPicswidth )
{
$NBPics=0;
}
}
?>

wmguk
03-17-2008, 05:55 AM
Try this:


<?php
$NBPicswidth=1;
$NBPics=0;
for ($i=0; $i<2; $i++
)
{ // replace 2 with the ammount of img
$image=$FileArray[$i];

echo "<img src='$image'>";

$NBPics=$NBPics+1;
if ( $NBPics==$NBPicswidth )
{
$NBPics=0;
}
}
?>


Hey,

I'm trying to get this to display 10 images at a time, I have assigned a total image variable, and I am using pagination scripting that will allow me next and previous...

any ideas how i can set this to display 10 images, and assign the start image and end image number?

basically if i have 500 images I dont want to show all 500 images on the same page.

Nile
03-17-2008, 10:35 PM
But did this script show 2 images? If it did, and you want it to display 10 images, try this:


<?php
$NBPicswidth=1;
$NBPics=0;
for ($i=0; $i<10; $i++
)
{ // replace 10 with the ammount of img
$image=$FileArray[$i];

echo "<img src='$image'>";

$NBPics=$NBPics+1;
if ( $NBPics==$NBPicswidth )
{
$NBPics=0;
}
}
?>

Jas
03-17-2008, 11:19 PM
I think you're talking about pagination (http://www.allsyntax.com/tutorials/PHP/23/Pagination-with-PHP/1.php). You use the $_GET super global to retrieve the current "page" the user is on (which is there you start-- (($number_per_page * $current_page) -$number_per_page)), and then advance so many in the array from there. See the tutorial, because I don't think I explained that very well :p

An example of how it *might* work in your page:


<?php

$page = (isset($_GET['page'])) ? $_GET['page']: 1;

$limit = 10; //How many per page?
$limter= ($limit*page) - $limit;

$NBPicswidth=1;
$NBPics=0;

for ($i=$limiter; $i<$limit; $i++) {
$image=$FileArray[$i];
echo "<img src='$image'>";
$NBPics=$NBPics+1;

if ( $NBPics==$NBPicswidth ) {
$NBPics=0;
}
}
?>