Can Any body help with this script. I am not an expert in PHP

I am displaying images 4 each in a page and the last image is repeating. unset is not working.

<?php
$ar=scandir("hairupdo");
$n=0;
$h="hairupdo";
$cnt=0;
$numofimages=count($ar)-3;
$imagesperpage=4;
$numberofimages=0;
$pages=ceil ($numofimages/$imagesperpage );
$gid=$_GET["id"];
	
 echo "<table width=100 border=0 cellspacing=5 cellpadding=5>";
   echo "<tr>";
	
		  
for($i=1; $i<=$pages ;$i++)
{
 
	echo "<a href=viewhairupdo.php?id=$i a>$i   ";

}

while(list($key,$ars)=each($ar))

{


	echo "dollar n : $n";		
	
	if (file_extension($ar[$key])=="jpg"||file_extension($ar[$key])=="jpeg") 
	{
		
		if ($cnt==2 )
		{
			echo "</tr><tr>";
			echo "<td>";
		}
		else
		
		{
			echo "<td>";
		}

		echo "<img src=hairupdo/$ar[$n]>";
		echo "</td>";
		$cnt=$cnt+1;
				
	}
	

	if ($n<count($ar)-1)
	{
		$n=$n+1;
	}
	if ($cnt==4 )
		{
			break;
			
		}
		
}

function file_extension($filename)
{
    return end(explode(".", $filename));
}
?>
Member Avatar for diafol

TRY THIS:

<?php
//DEFAULT VARS AND IMAGE ARRAY
$images_page = 4; 
$start_directory = "";
$files = glob("$start_directory*.{jpg,jpeg,JPG,JPEG}",GLOB_BRACE);
$no_images = count($files);
$no_pages = ceil($no_images/$images_page);

//GET ID FOR PAGE NUM
if(isset($_GET['id']) && intval($_GET['id']) > 0 && intval($_GET['id'] <= $no_pages)){ 
	$id =  intval($_GET['id']);
}else{
	$id = '1';
} 

//STARTS AND ENDS
$start_num = ($id - 1) * $images_page;
$end_num = $start_num + $images_page - 1;
if($end_num > $no_images - 1)$end_num = $no_images - 1;

//TEST OUTPUT - DELETE THIS BIT
echo "No images: " . $no_images . "<br />" . "Page number: " . $id . "<br />" . "No pages: " . $no_pages . "<br />" . "Images per Page: " . $images_page . "<br />" ."Start image: " . $start_num . "<br />" ."End image: " . $end_num;

//OUTPUT
$output = "\n<table>\n\t<tr>\n\t\t<td>";
for($i = 1;$i <= $no_pages; $i++){
	$output .= "<a href=\"?id=$i\">$i</a> ";
}
$output .= "\n\t\t</td>\n\t</tr>";
for($i=$start_num;$i<=$end_num;$i++){
	$output .="\n\t<tr>\n\t\t<td><img src=\"" . $files[$i] . "\" />\n\t\t</td>\n\t</tr>";
}
$output .= "\n</table>";
echo $output;
?>

Thank you for your reply. But its not working for me. Can you pls check it again

Test Out put is as shows
No images: 0
Page number: 1
No pages: 0
Images per Page: 4
Start image: 0
End image: -1

Actually it is
No images: 9
Page number: 1
No pages: 3
Images per Page: 4
Start image: 0

End image: -1 -- Is this correct ?

TRY THIS:

<?php
//DEFAULT VARS AND IMAGE ARRAY
$images_page = 4; 
$start_directory = "";
$files = glob("$start_directory*.{jpg,jpeg,JPG,JPEG}",GLOB_BRACE);
$no_images = count($files);
$no_pages = ceil($no_images/$images_page);

//GET ID FOR PAGE NUM
if(isset($_GET['id']) && intval($_GET['id']) > 0 && intval($_GET['id'] <= $no_pages)){ 
	$id =  intval($_GET['id']);
}else{
	$id = '1';
} 

//STARTS AND ENDS
$start_num = ($id - 1) * $images_page;
$end_num = $start_num + $images_page - 1;
if($end_num > $no_images - 1)$end_num = $no_images - 1;

//TEST OUTPUT - DELETE THIS BIT
echo "No images: " . $no_images . "<br />" . "Page number: " . $id . "<br />" . "No pages: " . $no_pages . "<br />" . "Images per Page: " . $images_page . "<br />" ."Start image: " . $start_num . "<br />" ."End image: " . $end_num;

//OUTPUT
$output = "\n<table>\n\t<tr>\n\t\t<td>";
for($i = 1;$i <= $no_pages; $i++){
	$output .= "<a href=\"?id=$i\">$i</a> ";
}
$output .= "\n\t\t</td>\n\t</tr>";
for($i=$start_num;$i<=$end_num;$i++){
	$output .="\n\t<tr>\n\t\t<td><img src=\"" . $files[$i] . "\" />\n\t\t</td>\n\t</tr>";
}
$output .= "\n</table>";
echo $output;
?>
Member Avatar for diafol

It looks like your start directory is not the one you're looking for. Try this:

$start_directory = "hairupdo/";

If it's not that make sure it's a relative reference from your file, e.g. "../images/hairupdo/".
the glob() function is beautiful, but takes a little experimentation to get the path. If you go to root "/", you'll go to webroot - which is not what you want, so using relative refenceing should be easier.

Thank you. It worked

Hi,

Sorry to disturb you again. That script is not working for IE. The link to the pics is not coming. Pls refer to artofmehendi.com/mehandigallery.php. It works with other browsers.

That portion is like this

$output .="\n\t\n\t\t<td><div align=center <a href=viewsubmehandi.php?id1=".$start_directory.$new1."/"."&id=1 a><img src=\"" . $files[$i] . "\" />\n\t\t<br />$new</div></td>\n\t";
}

Member Avatar for diafol
$output .="\n\t\n\t\t<td><div align=center <a href=viewsubmehandi.php?id1=".$start_directory.$new1."/"."&id=1 a><img src=\"" . $files[$i] . "\" />\n\t\t<br />$new</div></td>\n\t";

Good grief, I didn't write that! It's horrible. It's all garbled to hell. Your link tag is like this:

<a .... a>

Gawd knows what you're trying to do with this. If other browsers work with this format, their developers should be shot. 1-0 to IE in that case.

why not use forearch() instead of your while(list($key,$ars)=each($ar))??? you like complicated stuff? it think your image folder contains 4 images, coz the end of your loop depends on your $ar=scandir("hairupdo"); which is the location of the images.

Sorry i cant understand what is the problem. The link is in <a..a>

for($i=$start_num;$i<=$end_num;$i++){
$new=str_replace($start_directory,"",$files[$i]);

$output .="\n\t\n\t\t<td><div align=center [B]<a [/B]href=viewsubmehandi.php?id1=".$start_directory.$new1."/"."&id=1 [B]a>[/B]<img src=\"" . $files[$i] . "\" />\n\t\t<br />$new</div></td>\n\t";
}

So can you please correct me.

Member Avatar for diafol
$output .="\n\t\n\t\t<td><div align=center <a href=viewsubmehandi.php?id1=".$start_directory.$new1."/"."&id=1 a><img src=\"" . $files[$i] . "\" />\n\t\t<br />$new</div></td>\n\t";

Will give the html output:

<td><div align=center <a href=viewsubmehandi.php?id1=whateverfolder/&id=1 a><img src="myimage.jpg" />
        <br />whateverstring</div></td>

As you can see this is just plain wrong. You probably want something like this:

<td>
            <div align="center">
                <a href="viewsubmehandi.php?id1=whateverfolderandfile/&id=1"><img src="myimage.jpg" /></a><br />whateverstring
            </div>
        </td>

OR

<td>
            <div align="center">
                <a href="viewsubmehandi.php?id1=whateverfolderandfile/&id=1"><img src="myimage.jpg" /><br />whateverstring</a>
            </div>
        </td>

I don't like the folder structure within the querystring. You'd be better off urlencoding the folder/file name before writing to the querystring. You then urldecode at the other end: urldecode($_GET).
In addition, you should be looking to use img alt and a title attributes for accessibility.

Sorry to disturb you again.


Please go through this code

if ($new1=="bridal")
{
	$output .="\n\t\n\t\t<td><div align=center <a href=viewsubbridal.php?id=".$start_directory.$new1." ><img src=" . $files[$i] . " /> </a>\n\t\t<br />$new</div></td>\n\t";
}
else
{
	$output .="\n\t\n\t\t<td><div align=center <a href=viewsubmehandi.php?id1=".$start_directory.$new1."> <img src=" . $files[$i] . " /></a>\n\t\t<br />$new</div></td>\n\t";
}

THE VIEW SOURCE IN MOZILLA WHICH WORKS PERFECT

<td><div align=center <a href=viewsubbridal.php?id=mehandi/bridal ><img src=mehandi/bridal.jpg /> </a>
		<br />bridal.jpg</div></td>
	
	
		<td><div align=center <a href=viewsubmehandi.php?id1=mehandi/tattoo/> <img src=mehandi/tattoo.jpg /></a>

		<br />tattoo.jpg</div></td>
	</tr><tr>
	
		<td><div align=center <a href=viewsubmehandi.php?id1=mehandi/arabic/> <img src=mehandi/arabic.jpeg /></a>
		<br />arabic.jpeg</div></td>
	
	
		<td><div align=center <a href=viewsubmehandi.php?id1=mehandi/fusion/> <img src=mehandi/fusion.jpeg /></a>
		<br />fusion.jpeg</div></td>

The view source in I.E - Link not shown

<td><div align=center <a href=viewsubbridal.php?id=mehandi/bridal ><img src=mehandi/bridal.jpg /> </a>
		<br />bridal.jpg</div></td>
	
	
		<td><div align=center <a href=viewsubmehandi.php?id1=mehandi/tattoo/> <img src=mehandi/tattoo.jpg /></a>
		<br />tattoo.jpg</div></td>
	</tr><tr>
	
		<td><div align=center <a href=viewsubmehandi.php?id1=mehandi/arabic/> <img src=mehandi/arabic.jpeg /></a>
		<br />arabic.jpeg</div></td>
	
	
		<td><div align=center <a href=viewsubmehandi.php?id1=mehandi/fusion/> <img src=mehandi/fusion.jpeg /></a>
		<br />fusion.jpeg</div></td>

Now can you please explain this

Member Avatar for diafol

Yes - it's just extremely badly formed html - in fact, sorry to be rude, but it's horrible.

1) You don't have quotes around tag attributes
2) You have a link (<a...>...</a>) tag inside the open tag of the div.
3) The open div tag doesn't have a '>' at the end of it.

Try running your code in the xhtml validator: http://validator.w3.org/#validate_by_input

You can use the "paste your code" tab (link above) or, on another tab, enter the url of your page if it's hosted remotely. You've got at least 8 validation errors.

Concentrate on getting valid html output - learn the basics. The fact that Firefox and a few other browsers would actually display your page at all is worrying. I suppose they're built to be lenient, but that's ridiculous.

Ya thanks. Div was the problem. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.