0

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

3
  • 'catagory' is misspelled as well, should be 'category'. Commented Nov 5, 2011 at 20:05
  • Also, just a friendly heads-up, you shouldn't put that query in the __construct() method, because every time you instantiate that object it will run that query. Instead you could add it to a new method such as loadImages(). Commented Nov 5, 2011 at 21:28
  • Oh and another note, the practice of prefixing private member variables and methods with an underscore is an old PHP 4.x throwback, you should try to train yourself not to do that in future projects. Commented Nov 5, 2011 at 21:29

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

5 Comments

Ahhh yes good catch, would result in a multi-dim array. @Alan, try Bensiu's suggestion as well.
Thanks guys, but still not displaying anything, would be nice to have an error displayed to work on :) !! I have also noticed a typo in my code sorry! ImageArray should be imageArray within the foreach loop, I changed this, but still having the same problem
I have added var_dump's as suggested by bensiu , thanks. I am still getting a blank page. So what i decided to to was check my location which is correct as displays 'hello' which I just out in to tesy , I then commented out the while loop and put random strings into the $_images array, just to get something on the screen however the screen still displays blank for some reason?
@bensiu, thanks i've commented out everything under $images = new Images(); and var_dumped images object this is the following output: object(Images)#1 (3) { ["_images":"Images":private]=> array(2) { [0]=> string(5) "wffef" [1]=> string(7) "dsfwvew" } ["_error_messages":protected]=> array(0) { } ["_user_messages":protected]=> array(0) { } }
thanks for your efforts guys, it has given me something to work on :)
1

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'];      
}  

2 Comments

Thanks, I have tried your suggestion that makes complete sense however still nothing is displayed
If I were having this issue I would first run the sql statement from the mysql command line and ensure results were available, if so then I would ad a var_dump after you populate the $this->_images array right after the while loop, if data exists, then var_dump($images->getItems()) from your calling script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.