简体   繁体   English

无法通过php数组显示数据库中的图像

[英]unable to display images from database via php array

Learning php! 学习php!

I have a sql database which contains a table called 'images' which stores image paths that users would upload. 我有一个sql数据库,其中包含一个名为'images'的表,用于存储用户上传的图像路径。 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. 我遇到的问题是将图像路径从数据库拉到PHP数组,然后使用路径在屏幕上显示图像作为列表项。 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. 我使用phpmyadmin执行了SQL查询,查询正确。 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. 所以我知道foreach循环和查询工作。 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? 我之前使用的'create_connection'函数,连接到同一个数据库没有任何问题,我有点难过,但我认为它可能连接到mysql_fetch_array部分?

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

Everything looks ok except you are populating $this->images[] with 'category', but 'category' wasn't being selected from mysql, 'image_path' was, try this: 一切看起来不错,除了你用'category'填充$ this-> images [],但是没有从mysql中选择'category','image_path'是,试试这个:

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

change $imageArray[] = $images->getItems(); 更改$imageArray[] = $images->getItems(); to $imageArray = $images->getItems(); to $imageArray = $images->getItems(); on top of changes suggested by Digital Precision 除了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. 我希望它与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 ); var_dump总是显示一些内容...检查“空白页面”的来源以查看显示内容以及转储变量是否为空尝试使用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 ); 应该告诉你什么是坐在对象中,如果仍然属性_images是空数组,那么构造函数有问题,也许可以尝试while ($paths = mysql_fetch_array($result, MYSQL_ASSOC))var_dump( $result ); before WHILE LOOP - you loosing data somewhere... 在WHILE LOOP之前 - 你在某处丢失了数据......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM