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

时间:2013-10-08 19:27:35

标签: php class oop

嗨它只是不会返回我以前遇到过这个问题的所有图片而且我不知道每当我把我的代码放在类文件中时它会导致什么?

<?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;
       }
    }   
?>

2 个答案:

答案 0 :(得分:0)

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

进行以下更改:

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

答案 1 :(得分:0)

更改此内容:

$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>';