如何查看从单行表中爆炸检索的多个图像

时间:2014-12-18 06:48:32

标签: php mysql

我使用下面的代码来检索图像名称并爆炸并显示。但问题是输出。在输出视图中,一个额外的空白图像显示在下面的75以下,它仅由2个图像名称组成,但它显示2个图像和一个空白图像。任何帮助解决这个问题的人都将不胜感激。

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div>
<?php
$sql1=mysql_query("Select * from multiimg WHERE id=75");

$result=mysql_fetch_array($sql1);
/*$images = explode(",",$result['image']);

if(sizeof($images) > 0 ){
  foreach($images as $image){
   echo '<img src="photo/'.$image.'" height="50" width="50">' ;
   echo "<br />"; 
  }
}*/

$temp = explode(',',$result['image'] );

foreach($temp as $image){
    $images[]="photo/".trim( str_replace( array('[',']') ,"" ,$image ) );

}

//now your array of images cotaines to full source to each image

foreach($images as $image){
  echo "<img src='{$image}' height='100' width='200' />";
}
?>


</div>
</body>
</html>  

enter image description here

4 个答案:

答案 0 :(得分:2)

你可以做三件事来解决这个问题。一种是修剪/删除表格中图像输入末尾的逗号,其他是:

编辑首先添加trim()

// (I don't think you have to escape the comma, but I do anyway)
$result['image'] = trim($result['image'],'\,');

// Explode array
$temp = explode(',',$result['image'] );

// use array_filter() to remove empty key/values
// I had this filtering $images, but probably best to
// filter the original exploded array $temp
$temp = array_filter($temp);

foreach($temp as $image){
    $images[]="photo/".trim( str_replace( array('[',']') ,"" ,$image ) );
}    

foreach($images as $image){
  // Use an if here...you could use a root directory if defined previously.
  // Use whatever you like to check if the file exists
  if(is_file(ROOTDIR.$image))
      echo "<img src='{$image}' height='100' width='200' />";
}

答案 1 :(得分:0)

这是一个猜测,但尝试在爆炸前添加一个trim语句 像

 $images = trim($result['image'], "'");
 $temp = explode(',', $images);

答案 2 :(得分:0)

首先打印并查看从数据库中获取的图像数量。如果它们是正确的,那么在此之前执行此操作或使用修剪删除空白:

$temp = explode(',',$result['image'] );

foreach($temp as $image)
{
$images="photo/".$image;


 echo "<img src='$images' height='100' width='200' />";

}

答案 3 :(得分:0)

试试这个,我确定你想从下面的代码中得到什么。还有一件事,如果你得到空白的图像,请检查你的文件夹中是否存在图像,无论你想要到哪里..

<?php
$sql1=mysql_query("Select * from multiimg WHERE id=75");
$result=mysql_fetch_array($sql1);
$images=$result['image'];
$remove_last_comma=substr($images,0,-1);
$temp = explode(',',$remove_last_comma);
for($i=0;$i<count($temp);$i++)
{
	echo '<img src="photo/'.trim($temp[$i]).'" height="50" width="50">';
	echo "<br />";
    echo "<br />";  
}
?>
enter image description here enter image description here