Learning php!
I have a sql database which contains a table called ‘images’ which stores image paths that users would upload. The problem I am having is pulling the image paths from the database to a PHP array, then using the paths to display the images on the screen as a , list item. However when I run the page nothing is displayed.
code for database connection:
include('Database.php');
class Images extends Database{
private $_images = array();
public function __construct(){
$conn = $this->create_connection('read');
$sql = "SELECT image_path FROM 'items' WHERE catagory='tshirt'";
$result = $conn->query($sql)or die(mysql_error());
while($paths = mysql_fetch_array($result)) {
$this->_images[] = $paths['catagory'];
}
}
public function getItems() {
return $this->_images;
}
code for the view:
<ul>
<?php
require ('../model/Images.php');
$imageArray = array();
$images = new Images();
$imageArray[] = $images->getItems();
foreach($ImageArray as $value){
echo '<li><img src="'.$value.'"></li>';
}
?>
</ul>
I executed the SQL query using phpmyadmin, which query’s correctly. Also I have simulated the database data by adding the image paths manually to test looping through array.
private $_images = array('./images/tshirt1.jpg', etc, etc);
so I know the foreach loop and query work. The ‘create_connection’ function I have used before, connecting to the same database without any issues, I am a bit
stumped, but I think it may be connected to the mysql_fetch_array section?
Am I using the correct approach? or is there a better way to solve this issue?
Hope someone can point me in the right direction!
Thanks
change
$imageArray[] = $images->getItems();to$imageArray = $images->getItems();on top of changes suggested by Digital PrecisionEDIT:
would find where you loose data. I hope it is not related to wrong image path in DB.
EDIT:
var_dumpalways display something… check source of your ‘blank page’ to see what is displaying and if dumped variables are empty try withvar_dump( $images );should show you what is sitting in object, if still property _images is empty array there is problem with constructor, maybe trywhile ($paths = mysql_fetch_array($result, MYSQL_ASSOC))orvar_dump( $result );before WHILE LOOP – you loosing data somewhere…