显示来自DB php / MySQL的项目数组

时间:2013-10-30 05:50:50

标签: php mysql

我不确定如何显示items字段。我想显示两个数据表;一个包含来自用户的所有项目,一个包含所有项目以供用户使用。我能够输出的只是item_id's(我粘贴了下面的html)。如何从item表格中的这些ID获取所有项目信息,并填充HTML?

trans table

TRANS TABLES

item

enter image description here

$from = 1;
$sql = $db->prepare("SELECT * FROM test WHERE from_id = :id");
$sql->bindValue(':id', $from);

$sql->execute();

while($row = $sql->fetch())
{
    $t =$row['items'];
    $u =$row['to_id'];
    $trans .= "<tr><th>Items</th><th>To</th><th>Status</th></tr><tr><td>$t</td>
            <td>$u</td></tr>";
} 

HTML DISPLAY

enter image description here

4 个答案:

答案 0 :(得分:2)

试试这个!

<?php
    $from = 1;
   $sql = $db->prepare("SELECT * FROM test WHERE from_id = :id");
   $sql->bindValue(':id', $from);

   $sql->execute();

  while($row = $sql->fetch())
    {
     $t =$row['items'];
     $u =$row['to_id'];
             $itemIDs = @explode(",", $t);
         $items = array();
            foreach($itemIDs as $ID){
            $sqlItem = $db->prepare("SELECT itemname FROM itemtable WHERE itemid = :itemid");
            $sqlItem->bindValue(':itemid', $ID);
            $sqlItem->execute();
            $itemname ='';
            while($rowItems = $sqlItem->fetch())
            {
                $itemname .=$rowItems['itemname'];
            }

            $items[$t] = $itemname;

     }   

        $trans .= "<tr><th>Items</th><th>To</th><th>Status</th></tr><tr><td>$items[$t]</td>  <td>$u</td></tr>";
   }  

下面是我的测试代码,

<?php

  $from = 1;
 $sql = mysqli_query($db,"SELECT * FROM test WHERE from_id = '$from'");  
 while($row = mysqli_fetch_array($sql))
 {

     $t =$row['items'];
     $u =$row['to_id'];
     $itemIDs = @explode(",", $t);
     $itemname ='';
        foreach($itemIDs as $ID){
            $sqlItem = mysqli_query($db, "SELECT itemname FROM itemtable WHERE item_id = '$ID'");           

            while($rowItems = mysqli_fetch_array($sqlItem))
            {
                 $itemname .= $rowItems['itemname'].', ';
            }       


            $items[$u] = $itemname;

     }      
    $trans .= "<tr><th>Items</th><th>To</th><th>Status</th></tr><tr><td>$items[$u]</td>  <td>$u</td></tr>";
  } 

  echo "<table>".$trans."</table>";
?>

答案 1 :(得分:2)

注意:根据需要更改我的查询

在你的循环中

while($row = $sql->fetch())
{
$items_array = array();
$items_array = explode(",",$row["items"]);

    foreach($items_array as $key => $value)
    {                                       
        //modify ur query according to ur need
        $query3 = "SELECT  item_name
            FROM item_table 
            WHERE item_id =".$value." ";
        $result3 = mysql_query($query3);
        $row3 = mysql_fetch_assoc($result3);                                            
        $item_name .= $row3['subcategory_name'].", ";

    }
}

现在你的数组将包含item_id, 在ur while循环中使用foreach循环,并从expolode函数

中的item_id获取项目表中Item的信息

答案 2 :(得分:1)

在此期间,您必须触发将获取项目信息的新查询。 例如:

"SELECT * FROM item_info_table WHERE id IN (id1,id2, id3)"

它会返回与id相对应的项目信息。

答案 3 :(得分:0)

数据未标准化。让它正常化,你将有一个更好,更清洁的解决方案。