Alan
Alan

Reputation: 279

unable to display images from database via php array

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

Upvotes: 0

Views: 2030

Answers (2)

bensiu
bensiu

Reputation: 25564

change $imageArray[] = $images->getItems(); to $imageArray = $images->getItems(); on top of changes suggested by Digital Precision

EDIT:

$imageArray = $images->getItems();
var_dump( $imageArray );
foreach($imageArray as $value) {
        var_dump( $value );
        echo '<li><img src="'.$value.'"></li>';
}

would find where you loose data. I hope it is not related to wrong image path in DB.

EDIT:

var_dump always display something... check source of your 'blank page' to see what is displaying and if dumped variables are empty try with var_dump( $images ); should show you what is sitting in object, if still property _images is empty array there is problem with constructor, maybe try while ($paths = mysql_fetch_array($result, MYSQL_ASSOC)) or var_dump( $result ); before WHILE LOOP - you loosing data somewhere...

Upvotes: 1

Mike Purcell
Mike Purcell

Reputation: 19979

Everything looks ok except you are populating $this->images[] with 'category', but 'category' wasn't being selected from mysql, 'image_path' was, try this:

while($paths = mysql_fetch_array($result)) {        
    //$this->_images[] = $paths['catagory'];
    $this->_images[] = $paths['image_path'];      
}  

Upvotes: 1

Related Questions