Link to home
Start Free TrialLog in
Avatar of FairyBusiness
FairyBusinessFlag for United States of America

asked on

How to get my function to display images from my database??

Hi, I am trying to get images from my database table and display them on my page.  Its displaying something. . .

http://auroriella.com/image_tests.php

Can anyone see what I am missing in order to make this happen??

I call it like this on my page:

$images = new Image();
$images->display_images();

function image_path() {
	return $this->upload_dir.DS.$this->filename;
}
function display_images() {
	global $db_object;
	$images = $db_object->find_all();
	foreach($images as $image):
		echo "<a href=\"photo.php?id=" . $this->id . "\">";
		echo "<img src=\"" . $this->image_path() . "\" /></a>";
		echo $this->caption;
	endforeach;
}
function find_all() { // for arrays
	return $this->find_by_sql("SELECT * FROM " . $this->table);
}
function find_by_sql($sql="") { // for arrays
	global $database;
	$result = $database->query($sql);
	$object_array = array();
	while ($row = $database->fetch_array($result)) {
		$object_array[] = $this->instantiate($row);
	}
	return $object_array; // Must return an array!
}
private function instantiate($record) {
	$class_name = __CLASS__;
	$object = new $class_name; // Here is where it starts a new class for itself
	foreach($record as $attribute => $value) {
		if($object->has_attribute($attribute)) {
			$object->$attribute = $value;
		}
	}
	return $object;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some resources to help you learn the basics of PHP programming:

http://us2.php.net/tut.php
http://www.sitepoint.com/books/phpmysql4/
http://us2.php.net/manual/en/funcref.php

The script fragments you posted above tell me that you need to step back from this project and get some kind of foundation in the language basics.  Posting a fragment of a class without the class definition is the kind of thing that reveals a fundamental lack of understanding about the basics of programming.  Confusing the terminology about displaying images vice displaying image links is another clue.  There is nothing wrong with being new to PHP programming -- we all were at one time or another.  But it is not reasonable to believe that you will learn PHP programming by posting questions in a forum, any more than you can learn to play the piano by asking pianists how they play.  Try to find a course in PHP, maybe at a local junior college, where you can have some structured lessons.  If you can't  get that, use the resources posted above so you can get some foundation in the language and the craft of programming.  The SitePoint book is especially good.

And not to be facetious, but Google is your friend when you have very basic questions like this:
http://lmgtfy.com?q=Display+an+image+from+a+MySQL+database+in+a+PHP+web+page

Good luck with it, ~Ray
Avatar of FairyBusiness

ASKER

Yes, you have already pointed out in other questions that I am a beginner.  I already know this, and I have books, tutorials, and websites for help as well but sometimes I don't get it still so I come here.  Please post new or relevant stuff to my questions. . .
Thanks
Glad to help :)