方法不会返回数组,只返回一个元素


Method won't return array, just one element

嗨,它

只是返回我以前遇到过这个问题的所有图片,每当我将代码放入类文件中时,我真的不知道是什么导致了它

<?php
class get_pic{
function thumb($id){
$piclist = '';
     if (isset($_GET['id'])) {
    $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
    // Use this var to check to see if this ID exists, if yes then get the product 
    // details, if no then exit this script and give message why
    $sql = mysql_query("SELECT * FROM productpic WHERE id='$id' ");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
        // get all the product details
        while($row = mysql_fetch_array($sql)){
                 $path1 = $row["path1"];
                 $piclist ='<li id="thumb"><img id="thumb" style=" position:relative; border:#666 1px solid;  "  src="'.$path1.'" alt="' . $id . '" width="120" height="160" border="1" /></li>';
              } 
          }     
         }
          return $piclist;
       }
    }   
?>

您的$piclist只有最后一个值。您应该连接字符串。

进行以下更改:

...
if ($productCount > 0) {
  while($row = mysql_fetch_array($sql)) {
    $piclist .= '<li>...';

更改此设置:

$piclist = '';
...
$piclist ='<li id="thumb"><img id="thumb" style=" position:relative; border:#666 1px solid;  "  src="'.$path1.'" alt="' . $id . '" width="120" height="160" border="1" /></li>';

。这可能会解决它:

$piclist = array();
...
$piclist[] ='<li id="thumb"><img id="thumb" style=" position:relative; border:#666 1px solid;  "  src="'.$path1.'" alt="' . $id . '" width="120" height="160" border="1" /></li>';