Function Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Function Error

Post by John Cartwright »

Okay I have a bunch of functions located in a file called

inc/classes

These functions are being called in from a file called inc/design1.. which is being called from every page .. ie cart.php..index.php in the root directory and everything works fine.

But now I have a gallery/index.php and I try to include the inc/design1.php but I get this error message.

Fatal error: Cannot instantiate non-existent class: cart in /home/edenis/public_html/inc/design1.php on line 1

How come I am getting this error message when I am looking in the gallery/index.php but not from index.php

this is my gallery.php

Code: Select all

<?php

include("../inc/design1.php");

//Title for your gallery that will be used with the default header.
$title="";

//Thumbnail size. The minimum width and height in pixels, that you would like the thumbnails to be. Photos are scaled.
$twidth=200;
$theight=200;

//Photo size. The minimum width and height in pixels, that you would like you photos scaled to when displayed.
$pwidth = 640;
$pheight = 640;

//The thumbnail gallery is displayed in a table. Please choose the number of rows and columns you would like.
$rows=3;
$cols=3;

//If you would like the filename of the photo to be displayed under the thumbnail change this setting.
//0=off 1=on
$showfilename=0;

//If you would like photo to up a new window when clicked change this setting.
//The defualt is to open the image up in the same browser window.
//0=off 1=on
$window=0;

//If you would like to use a custom header or footer please add the file names here and they will be included in the script.
//Otherwise the plain jane default will be used. The files should be in the same folder as the script, but if you provide an
//alternate path it will work as well. Example: header.html, header.php. 
$header="NULL";
$footer="NULL";

//No need to mess with anything below here.
//Part of this function comes from http://www.php.net/manual/en/function.getimagesize.php
function thumb_maker($filename, $minwidth, $minheight) {
	if (file_exists($filename."thmb.jpg")) {
		$photosize = getimagesize($filename."thmb.jpg");
		if (max($minwidth,$minheight)!=max($photosize[0],$photosize[1])) {
		unlink($filename."thmb.jpg");
		}		
	}
	if (!file_exists($filename."thmb.jpg") && file_exists($filename)) {
		echo "<br/>One moment please....creating a thumbnail.";
		set_time_limit(60);
		$photosize = getimagesize($filename);
	    // Get image size and scale ratio
	    $scale = min($minwidth/$photosize[0], $minheight/$photosize[1]);
		if ($scale < 1) {
		   $width = floor($scale*$photosize[0]);
		   $height = floor($scale*$photosize[1]);
		}
		else {
		   $width = $minwidth;
		   $height = $minheight;
		}
		if ($photosize[mime]=="image/jpeg") {
			$resizedimage = imagecreatetruecolor($width, $height);
			$thumbimage = imagecreatefromjpeg($filename);
			imagecopyresampled($resizedimage, $thumbimage, 0, 0, 0, 0, $width, $height, $photosize[0], $photosize[1]);
			imagejpeg($resizedimage,$filename."thmb.jpg",50);
			imageDestroy($resizedimage); 
			imageDestroy($thumbimage); 
		}
	}
}

//Part of this function comes from http://www.php.net/manual/en/function.getimagesize.php
function gallery_sizer($photo,$minwidth,$minheight) {
	if (file_exists($photo)) {
		$photosize = getimagesize("$photo");
	    # Get image size and scale ratio
	    $scale = min($minwidth/$photosize[0], $minheight/$photosize[1]);
		if ($scale <= 1) {
		   $width = floor($scale*$photosize[0]);
		   $height = floor($scale*$photosize[1]);
		}
		return array($width,$height,$photosize[0],$photosize[1]);
	}
}

function getdirs($dir) {
	$dirs=array();
	chdir($dir);
	if ($handle = opendir($dir)) {
	    while (false !== ($file = readdir($handle))) {
	        if ($file != "." && $file != ".."){
				if (is_dir($file)) {
	        		$dirs[] = $file;
				}
	        }
	    }
	    closedir($handle);
	}
	sort($dirs);
	return $dirs;
}


function getphotos($photodir) {
	$photos=array();
	if ($handle = opendir($photodir)) {
	    while (false !== ($file = readdir($handle))) {
	        if ($file != "." && $file != ".." && !eregi(".jpgthmb.jpg$",$file) && eregi(".jpg$",$file)){
	                $photos[] = $file;
	        }
	    }
	    closedir($handle);
	}
	sort($photos);
	return $photos;
}
		
function thumb_gallery($photonum) {
	global $photos, $photourl, $photodir, $twidth, $theight, $rows, $cols, $showfilename, $album, $window;
		if (($photonum)>(count($photos)-($rows*$cols))) {
			$photonum=(count($photos)-($rows*$cols));
		}
		if (($photonum)<=0) {
			$photonum=0;
		}

		if(isset($album)) { $query="&album=".urlencode($album); }
		for ($tr = 1; $tr <= $rows; $tr++) {
			$photobody[]="<tr>";
			for ($td = 1; $td <= $cols; $td++) {
				$photobody[]="<td align="center">";
				$size=gallery_sizer($photodir."/".$photos[$photonum]."thmb.jpg",$twidth,$theight);
				$link = $photourl."/".$photos[$photonum]."thmb.jpg";
				if ($window==1) { $target="_blank"; } else { $target="_self"; }
				if ($photonum<=count($photos)-1) { $photobody[]="<a href="$_SERVER[PHP_SELF]?p=$photos[$photonum]$query" target="$target"><img src="$link" width="$size[2]" height="$size[3]" alt="$photos[$photonum]"></a><br />"; }
				if ($showfilename==1) { $photobody[]=$photos[$photonum]; }
				$photobody[]="</td>";
				$photonum++;
			}
			$photobody[]="</tr>";
		}
		unset($tr, $td);
	
		#this id down here for a reason don't move it and use array_unshift instead. Trust me.
		if (($photonum-($rows*$cols))>0) { $prev="<a href="$_SERVER[PHP_SELF]?pn=".($photonum-(($rows*$cols)*2)).$query.""><< Previous</a>"; }
		if (($photonum)<(count($photos))) { $next="<a href="$_SERVER[PHP_SELF]?pn=".$photonum.$query."">Next>></a>"; }
		
		$photopage=array();
		$photopage[]="Pages: ";
		for ($pagelink=0, $pagenum=1; $pagelink<=count($photos); $pagelink+=($rows*$cols), $pagenum++) {
			$photopage[]="<a href="$_SERVER[PHP_SELF]?pn=".($pagelink).$query."">$pagenum</a> | ";
		}
		unset($pagelink, $pagenum);
		$photopage=implode("", $photopage);	

		$photobody[]="<tr><td colspan=".$cols."><table width="100%" border="0"><tr><td width="33%"><div align="LEFT">".$prev."</div></td><td width="33%"><div align="CENTER" class="caption">Photos <strong>".($photonum-(($rows*$cols)-1))."</strong> to <strong>".$photonum."</strong> of <strong>".count($photos)."</strong><br />".$photopage."</div></td><td width="33%"><div align="RIGHT">".$next."</div></td></tr></table></td></tr>";
		array_unshift($photobody, "<tr><td colspan=".$cols."><table width="100%" border="0"><tr><td width="33%"><div align="LEFT">".$prev."</div></td><td width="33%"><div align="CENTER">Photos <strong>".($photonum-(($rows*$cols)-1))."</strong> to <strong>".$photonum."</strong> of <strong>".count($photos)."</strong><br />".$photopage."</div></td><td width="33%"><div align="RIGHT">".$next."</div></td></tr></table></td></tr>");

		#put the table tag at the top of the array.
		array_unshift($photobody, '<table width="100%" border="0" cellspacing="0" cellpadding="3">');
		$photobody[]='</table>';

	return $photobody;
}

function photo_gallery($photo, $pwidth, $pheight) {
	global $photodir, $photourl, $thumbsurl, $window;
	$photobody=array();
	
	if (isset($photo)) {
		$size=gallery_sizer("$photodir/$photo",640,640);
		$photobody[]='<table width="100%" border="0" cellspacing="0" cellpadding="3">';
		$photobody[]="<img src="$photourl/$photo">";
		$photobody[]='<tr><td colspan="3" align="center">To save this image to your computer right click on the picture and choose<br /> <strong>''Save Picture As...'' or ''Save Image As...''</strong>.</td></tr>';
		if ($window==1) { $target="<a href="javascript:window.close()">Close Window</a>"; } else { $target="<a href="".$_SERVER[HTTP_REFERER]."">Back</a>"; }
		$photobody[]="<tr class="imageborder"><td>".$target."</td><td align="center"><div align="CENTER" class="caption">Photo <strong>".($photo)."</strong></div></td><td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>";
		$photobody[]='<tr><td colspan="3" align="center">';
		$photobody[]="<img src="$photourl/$photo" width="$size[0]" height="$size[1]" border=0 alt="" class="imageborder">";
		$photobody[]='</td></tr>';
		$photobody[]="<tr class="imageborder"><td align="center" colspan="3"><div align="CENTER" class="caption">Full size photo size ".$size[2]." pixels wide by ".$size[3]." pixels high.</div></td></tr>";
		$photobody[]="<tr class="imageborder"><td>".$target."</td><td align="center"><div align="CENTER" class="caption">Photo <strong>".($photo)."</strong></div></td><td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>";
		$photobody[]='</table>';
	}
	return $photobody;
}

function display($photobody) {
	global $photodir;
	getheader();
	albums($photodir);
	foreach ($photobody as $value) {
	   echo $value."\n";
	}
	
	getfooter();
	exit;
}

function albums($dir) {
//for now only one level of albums works.
	global $album, $title;
	$dirs=getdirs($dir);
	if (isset($album)) { echo "<h1 align="center">".$album."</h1>"; } else { echo "<h1 align="center">".$title."</h1>"; }
	echo "<div align="center">";
	if (isset($album)) { echo "<a href="".$_SERVER[PHP_SELF]."">$title</a>\n"; } else {  }
	if (count($dirs)>0) {
		foreach ($dirs as $value) {
		   echo "<a href="".$_SERVER[PHP_SELF]."?album=".urlencode($value)."">".$value."</a>\n";
		}
	}
	echo "</div>";
}

//returns the header for the page. Checks for a user defined header.
function getheader() {
	global $header, $title;
	if (isset($header) && $header!="NULL" && is_file ($header)) {
		require("$header");
	}
	else {
		echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
		echo '<html>';
		echo '<head>';
		echo "<title>$title</title>";
		echo '</head>';
		echo '<body>';
	}
}

//returns the footer for the page. Checks for a user defined footer.
function getfooter() {
	global $footer;
	if (isset($footer) && $footer!="NULL" && is_file ($footer)) {
		require("$footer");
	}
	else {
		echo "</body>\n</html>";
	}
}

$photodir= dirname($_SERVER['PATH_TRANSLATED']);
$photourl=dirname($_SERVER[PHP_SELF]);
$thumbsurl=dirname($_SERVER[PHP_SELF]);
$photobody=array();

if (isset($_GET[album])) {
	$album = urldecode($_GET[album]);
	$photodir=dirname(realpath($_SERVER['PATH_TRANSLATED']))."/".$album;
	$photourl=dirname($_SERVER[PHP_SELF])."/".$album;
	$thumbsurl=dirname($_SERVER[PHP_SELF])."/".$album;
}

//To make sure this works both on Linux and Win
$photodir = str_replace("", "/", $photodir);

//Get the jpegs from the folder.
$photos=getphotos($photodir);

//check to see if thumbnails are made if not it will make. Adds time to the processing of the script.
for ($i=0; $i<=count($photos)-1; $i++) {
	thumb_maker($photodir."/".$photos[$i], $twidth, $theight);
}
if (count($photos)<=0) {
	echo "No JPEG photos in the folder to display";
	exit;
}
if (isset($_GET[pn])) {
	$photonum = $_GET[pn];
	$photobody=thumb_gallery($photonum);
}
elseif (isset($_GET[p])) {
	$photo=$_GET[p];
	$photobody=photo_gallery($photo,$pwidth, $pheight);
}
else {
	$photobody=thumb_gallery(0);
}
//output it all to the browser.
display($photobody);
//include("http://www.mobiledreamz.com/inc/design2.php");
exit;




?>
This is sort of boggling my mind ......... :S because the function is working properly.. its just when i call it from that directory it is not working properly.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

Have you tried...

Code: Select all

<?php
include("$DOCUMENT_ROOT/inc/design1.php"); 

?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Same error:

Fatal error: Cannot instantiate non-existent class: cart in /home/edenis/public_html/inc/design1.php on line 1

BTW cart is 1 of the functions located in inc/classes.php
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post by EvilWalrus »

add

Code: Select all

<?php
error_reporting(E_ALL);
?>
to the top of your class... that will help you identify where the error is.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nothing new :S

already tried that
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

From doing a little "googling" ..
POSSIBLE CAUSES ..
you have forgotten to define the class

you have given the class a wrong name (for instance it is named uppon some other page whose code you have just copied over)

You have misspelled the class

you have a syntax error somewhere in your logic file, so php compiler can not find the definition.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

The thing is that everything works fine until I add that include file

inc/design1.php

I use this throughout my whole site and the function works fine so i know i have no mispelled anything or have a syntax erorr which is screwing up the complilation............ :S :S :S
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

ANy other suggestions....? I can provide you with code of other pages if needed be...

EDIT.. it was actually an error on a different page causing the problems... something completely unrelated.. TY

Problem solved.
Post Reply