Page 1 of 2

my class sux!!!!

Posted: Mon Feb 27, 2006 11:36 am
by modplod
Hi all, I need some help with one of the classes im building for my site, it a simple gallery script that reads the contents of a dir and displays them as thumbmails ith links to view the images full size, (at this time it just displays simple linmks and images)

The problem is that at the end of the __construct function, a function call is not working for some reason. I have the correct code and it has worked before, but I'm just unable to fie the problem, mybe its my approch to the problem, anyhow, It would help loads if someone coud take a look at the class in the fille attached and reveal the correct code to me.

I have doc'ed the code as much as will be helpfull, this is a work in progerss I know the code is scruffy. :oops:

Code: Select all

<?

session_start();
// get gallery mnam from session var galID
// test session var
$_SESSION['galID'] = "maya";


class gallery{
	# Gallery class 0.9/2.2006

	# DEBUG VERSION, NOT FOR RELEASE
	# PLEASE DO NOT USE THIS CODE IN ANY PRODUNCTION SCRIPTS.
	# THIS CODE HAS NOT BEEN TESTED AND MY BE UNSTABLE

	#THE PROBLEM IS THAT THE FUNCTION CALL TO showDIR AT THE END
	#OF THE __CONSTRUCT FUNCTION

	# THE GOAL IS TO DISPLAY THE CONTENTS OF THE A DIRECTORY NAMED IN THE $galName VAR PASSED TO
	# THE CLASS WHEN ITS USED TO CREATE AN OBJECT, THE CODE SHOULD THEN DISPLAY A SET OF THUMBNAILS
	# OF THE IMAGES IN THE $location/$galName FOLDER

	
	
	# gallery class for php 5+ by leigh edwards Feb 25 2006
	# Please send any information on any problems you encounter to
	# leighjedwards@msn.com
	
	# Note Some of this code was taken from open scorce files and is not the work of the auther,
	# if you see any of your code here, please let me know and i'll add the propper credits where needed

	private $maxDirSize;
	private $window;
	private $maxcols;
	private $location;
	private $imagemaxwidth;
	private $extensions;
	private $browsedir;
	private $galleryName;
	private $adirectory;

	function __construct($galName){


		// set gallery defaults here
		$this->maxDirSize = 200;

		// set open option 0 = same window, 1 = new window
		$this->window = 1;

		// change layout options here
		$this->maxcols = 3;
		$this->imagemaxwidth = 100;

		// set to the dir where you will store your galleries, remember the ending slash
		$this->location = "galleries/";

		# list allowed extensions here
		$this->extensions[1] = "jpeg";
		$this->extensions[2] = "jpg";
		$this->extensions[3] = "gif";
		$this->extensions[4] = "txt";


		// the problem is with this line here or the function it links to, I think.
		// All other parts of this code are working fine when tested alone
		$this->showDir( $this->dirToArray($this->galName) ); // display the gallery
	}

	# dirToArray
	#
	# directory to array extractor
	# $browsedir (string - path name)
	# $extentions (array -  extensions to be listed)

	function dirToArray ($galleryName)
	{
		$maxDirSize = 200;
		$nextensions = sizeof($this->extensions);
		$idirectory = 0;
		$this->browsedir = $this->location . $galName; // set the gallery to view
		$directory = dir ($browsedir);
		while ($entry = $directory->read())
		{
			for ($i=1; $i<=$nextensions; $i++)
			{
				$compare = stristr ($entry, $this->extensions[$i]);
				if (strlen($compare) == strlen($this->extensions[$i]))
				{
					$adirectory[++$idirectory] = $entry;
					break;
				}
			}
		}
		$directory->close();
		return $adirectory;
	}


	# showDir
	#
	# show table with images and links
	# $browsedir (string - path name)
	# $adirectory (array -  filenames w/o path)

	function showDir ($adirectory)

	{

		//global $browsedir;

		# change layout options here
		$maxcols = 3;
		$imagemaxwidth = 100;
		$imagemaxheight = 80;

		$imagemaxratio =  $imagemaxwidth / $imagemaxheight;

		$ndirectory = sizeof ($adirectory);
		echo ("<table width=100%>");
		for ($i=0; $i<=$ndirectory;)
		{
			echo ("  <tr>");
			for ($icols=1; $icols<=$maxcols; $icols++)

			{
				echo ("   <td align=center>");
				$imagefilename = $this->adirectory[++$i];
				if (strlen($imagefilename)>0)
				{
					$this->imagepath = $browsedir."/".$imagefilename;
					$imagesize = GetImageSize ($this->imagepath);
					if ($imagesize)
					{
						$imagewidth = $imagesize[0];
						$imageheight = $imagesize[1];
						$imageratio = $imagewidth / $imageheight;
						if ($imageratio > $imagemaxratio)
						{
							$imageoutputwidth = $imagemaxwidth;
							$imageoutputheight = ceil ($imagemaxwidth/$imagewidth*$imageheight);
						}
						else if ($imageratio < $imagemaxratio)
						{
							$imageoutputheight = $imagemaxheight;
							$imageoutputwidth = ceil ($imagemaxheight/$imageheight*$imagewidth);
						} else
						{
							$imageoutputwidth = $imagemaxwidth;
							$imageoutputheight = $imagemaxheight;
						}

						$out = "<a href=\"".$this->imagepath."\"";
						// check to see if link to open in a window or not
						// default 0
						if ($this->window ==1){ $out .= "target=\"_blank\">";}

						$out .= "<img src=\"".$this->imagepath;
						$out .= "\" width=\"".$imageoutputwidth;
						$out .= "\" height=\"".$imageoutputheight;
						$out .= "\" border=\"0\"><br>";
						$out .= $adirectory[$i]."</a>";
						echo $out;
					}
					echo "</td>";
				}
			}
			echo "</tr>";
		}
		echo "</table>";
	}


}
$galName = $_SESSION['galID'];
$gal = new gallery($galName);

?>

Posted: Mon Feb 27, 2006 11:47 am
by John Cartwright
Sorry I'm in a lecture ATM and don't have time to go through your code but I did notice you have some extra code that is not really required, although it is not incorrect (I'm a nit pick on these kinds of things)

Code: Select all

private $maxDirSize = 200;
    private $window = 1;
    private $maxcols = 3;
    private $location = 'galleries/';
    private $imagemaxwidth = 100;
    private $extensions = array('jpeg', 'jpg', 'gif', 'txt');
    private $browsedir;
    private $galleryName;
    private $adirectory;

    function __construct($galName){
        // the problem is with this line here or the function it links to, I think.
        // All other parts of this code are working fine when tested alone
        $this->showDir( $this->dirToArray($this->galName) ); // display the gallery
    }
Back to school now.. :(

Posted: Mon Feb 27, 2006 11:53 am
by modplod
yer, I know, I'm in the process of moving things around a bit to streem line the code,

Thanks anyhow.

Posted: Mon Feb 27, 2006 12:01 pm
by John Cartwright
One more note, you could reduce your dirToArray method to a couple lines, and avoid loops

Code: Select all

$pattern = '{*.'. implode(',*.', $this->extensions).'}'
$file = glob($this->browsedir.$pattern, GLOB_BRACE);

Posted: Mon Feb 27, 2006 12:52 pm
by modplod
cool I'll tyr that latter thanks :D

Posted: Mon Feb 27, 2006 3:12 pm
by Christopher
I didn't wade through the code, but usually you don't have a class execute competely in the constructor. I would suggest removing the following from the constructor:

Code: Select all

$this->showDir( $this->dirToArray($this->galName) ); // display the gallery
and moving the:

Code: Select all

$this->dirToArray($this->galName);
into the showDir() method.

Then you can test to see if the object is initialized properly before executing the show method.

Posted: Mon Feb 27, 2006 3:38 pm
by modplod
cool, I think I tryed something simler to that just before I when with what I got.

but I need all the code to execute when the class is called with the command new galler($galName) If I can get this I dont realy care where everything inside the class is or how it runs

I'll try that again
thanks

Posted: Mon Feb 27, 2006 4:53 pm
by Christopher
modplod wrote:but I need all the code to execute when the class is called with the command new galler($galName) If I can get this I dont realy care where everything inside the class is or how it runs
I think the following would make is clearer what is going on:

Code: Select all

$gal = new gallery();
$gal->showDir($_SESSION['galID']);

Posted: Mon Feb 27, 2006 6:23 pm
by modplod
same thing just a bit longer,
thanks anyhow

Posted: Mon Feb 27, 2006 6:34 pm
by modplod
I think I have found the problem

Code: Select all

$directory->close();
for some reason the line above is entering into a open loop and bogging the server down.

The only other thing that even remotly works in its place(that I know about) is

Code: Select all

$directory->close();
but this just hangs the script.

All the vars are being passed right that I can see and all other function calls before this seem to be working to


below is the current version of the code I'm using just incase I missed something else someone else may spot.

Code: Select all

<?
class gallery{

	# Gallery class 0.9/2.2006

	# DEBUG VERSION, NOT FOR RELEASE!!!
	# PLEASE DO NOT USE THIS CODE IN ANY PRODUCTION SCRIPTS.!!!
	# THIS CODE HAS NOT BEEN TESTED AND MAY BE UNSTABLE!!!

	# THE GOAL IS TO DISPLAY THE CONTENTS OF THE A DIRECTORY NAMED IN THE $galName VAR PASSED TO
	# THE CLASS WHEN ITS USED TO CREATE AN OBJECT, THE CODE SHOULD THEN DISPLAY A SET OF THUMBNAILS
	# OF THE IMAGES IN THE $location/$galName FOLDER

	# gallery class for php 5+ by leigh edwards Feb 25 2006
	# Please send any information on any problems you encounter to
	# leighjedwards@msn.com

	# Note Some of this code was taken from open scorce files and is not the work of the auther,
	# if you see any of your code here, please let me know and i'll add credits where needed



	// set gallery defaults here

	private $window =1; // display in window: 1 = true, 0 = faulse
	private $maxcols =3; // maximum number of colums per page
	private $location = "galleries/"; // root dir of galleries
	private $imagemaxwidth = 100; // maximum width of thumbnails
	private $extensions = Array('jpeg', 'jpg', 'gif'); // array of listed files

	// do not change below here
	private $browsedir;
	private $galleryName;

	// construct method
	function __construct($galName){
                // set the gallery dir
		$this->galleryName = $this->galName;
		echo "construct executed";
		$this->display();
	}

	# dirToArray
	#
	# directory to array extractor
	# $browsedir (string - path name)
	# $extentions (array -  extensions to be listed)

	function dirToArray ()
	{
		$nextensions = sizeof($this->extensions);
		$i = 0;
		$this->browsedir = $this->location . $this->galleryName; // set the gallery to view
		$directory = dir ($this->browsedir);

		while ($entry = $directory->read())
		{
			for ($i=1; $i<=$nextensions; $i++)
			{
				$compare = stristr ($entry, $this->extensions[$i]);
				if (strlen($compare) == strlen($this->extensions[$i]))
				{
					$adirectory[++$i] = $entry;
					break;
				}
			}
		}

		$this->directory->close();
		echo"<br>ok2";
		return $adirectory;
	}


	# showDir
	#
	# show table with images and links
	# $browsedir (string - path name)
	# $adirectory (array -  filenames w/o path)

	function showDir (){
		echo"ok";
		$adirectory = $this->dirToArray();
		echo"ok 111";
		$imagemaxratio =  $imagemaxwidth / $imagemaxheight;
		$ndirectory = sizeof ($adirectory);
		echo ("<table width=100%>");
		for ($i=0; $i<=$ndirectory;)
		{
			echo ("  <tr>");
			for ($icols=1; $icols<=$maxcols; $icols++)

			{
				echo ("   <td align=center>");
				$imagefilename = $this->adirectory[++$i];
				if (strlen($imagefilename)>0)
				{
					$this->imagepath = $browsedir."/".$imagefilename;
					$imagesize = GetImageSize ($this->imagepath);
					if ($imagesize)
					{
						$imagewidth = $imagesize[0];
						$imageheight = $imagesize[1];
						$imageratio = $imagewidth / $imageheight;
						if ($imageratio > $imagemaxratio)
						{
							$imageoutputwidth = $imagemaxwidth;
							$imageoutputheight = ceil ($imagemaxwidth/$imagewidth*$imageheight);
						}
						else if ($imageratio < $imagemaxratio)
						{
							$imageoutputheight = $imagemaxheight;
							$imageoutputwidth = ceil ($imagemaxheight/$imageheight*$imagewidth);
						} else
						{
							$imageoutputwidth = $imagemaxwidth;
							$imageoutputheight = $imagemaxheight;
						}

						$out = "<a href=\"".$this->imagepath."\"";
						// check to see if link to open in a window or not
						// default 0
						if ($this->window ==1){ $out .= "target=\"_blank\">";}

						$out .= "<img src=\"".$this->imagepath;
						$out .= "\" width=\"".$imageoutputwidth;
						$out .= "\" height=\"".$imageoutputheight;
						$out .= "\" border=\"0\"><br>";
						$out .= $adirectory[$i]."</a>";
						echo $out;
					}
					echo "</td>";
				}
			}
			echo "</tr>";
		}
		echo "</table>";
	}
	/**
 * Function to retrive the gallery list and create a menu from that list
 * The menu is output via an echo statment at the end of the function
 *
 * This function requiers the mysql.class.php file
 */
	function GalMenu(){
		// at this time, this function uses the mysql.class.php file
		// to retrive the list of galleries from a my sqo database.
		// this should be improved to dynamicaly read all subdir's in the gallery root
		// and use that to build the list
		// Load in data for the gallery list.

		// make connection to the mysql database
		// load sql class

		require("functions_and_classes/mysql.class.php");

		// create a new mysql object
		$mysqldb = new mysql();

		// connect to the database
		$mysqldb->connect();

		// select the database
		$mysqldb->select();

		// execute a query to retrive gallery menu list from the database
		$mysqldb->query("SELECT * FROM gallery_list");
		echo "<br>";
		// start building the menu
		while ($row = $mysqldb->fetchObject()){
			$gName = $row->gal_name;
			$gDir = $row->gal_dir;


			echo "<a href=\"  \">". $gName."</a><br>";
		}
	}


	// method to 'display' the gallery
	// can be called direct or places in the construct to
	// run when the class is used to create a new object


	function display(){
		// call the menu function
		$this->GalMenu();
		// out put gallery
		$this->showDir();
		echo "ok, script compleate";

	}
}



?>

Posted: Mon Feb 27, 2006 6:49 pm
by Christopher
modplod wrote:same thing just a bit longer,
thanks anyhow
Conceptually in OOP they are different.

In you code you do the following:

Code: Select all

$directory = dir ($this->browsedir);
..
        $this->directory->close();

Posted: Mon Feb 27, 2006 7:10 pm
by modplod
arborint wrote:
modplod wrote:same thing just a bit longer,
thanks anyhow
Conceptually in OOP they are different.
I've not read much about OOP concepts in php or other lang', other than a few intros in text books. However after starting to write this, and a few other scripts. I think I should get right on that. As you can see from my code i'm a noob to this, out side of phpNuke 'haking', dont realy know squat about programing. But for some reason I find it fun. :D

Any hooooo. going to try your example..

Posted: Mon Feb 27, 2006 7:15 pm
by modplod
arborint wrote:Conceptually in OOP they are different.

In you code you do the following:

Code: Select all

$directory = dir ($this->browsedir);
..
        $this->directory->close();

yes I do....why??


I also tryed

Code: Select all

$directory->close();

Posted: Mon Feb 27, 2006 9:44 pm
by modplod
Thanks for all your help guys, I solved the problem, with a compleat rewrite of my original code, works almost as I I had hoped.
thanks again.

Posted: Tue Feb 28, 2006 7:00 pm
by a94060
when i read the title,i thought you meant one of your real school/college classes :)