?
Solved

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

Posted on 2011-04-23
5
Medium Priority
?
206 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:FairyBusiness
  • 2
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 2000 total points
ID: 35455175
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35455995
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
0
 

Author Comment

by:FairyBusiness
ID: 35457148
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. . .
0
 

Author Closing Comment

by:FairyBusiness
ID: 35457149
Thanks
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35457308
Glad to help :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question