将mysql结果更改为产品表

时间:2015-12-01 18:43:26

标签: php mysql

我有这段代码

SELECT t.typeName, m.quantity, m.typeID
FROM invTypeMaterials AS m
LEFT JOIN invTypes AS t
ON m.materialTypeID = t.typeID
WHERE m.typeID = 222'

它产生了这个:

Array ( [0] => Array ( [typeName] => Tritanium [quantity] => 204 [typeID] => 222 ) [1] => Array ( [typeName] => Pyerite [quantity] => 17 [typeID] => 222 ) [2] => Array ( [typeName] => Nocxium [quantity] => 1 [typeID] => 222 ) ) 

如何在甜美的上帝中把它变成这个。

Tritanium   Pyerite     Nocxium 
204          17          1  

这应该非常简单,我确信它是,但无论我尝试什么,我都无法让它显示出来。

试过这个以及其他很多:

 $i=0;
// output data of each row
   foreach ($users as $user) {

    //echo count($user['typeID']);
    echo '<td>' . $user["quantity"] . '</td>';
    //echo '<td>' . $user["typeID"] . '</td>';
    $i++ ;


 if($i % 8 == 0){
   echo '<td></td>';

}

elseif($i % 1 == 0) {
    echo '</tr>';
}

    if($i<5) { echo '<td></td>';    

}

选择答案

Tritanium   Pyerite Mexallon    Tritanium   Pyerite Mexallon    Isogen  Tritanium   Mexallon    Isogen
107 213 107 56000   12050   2100    450 134 267 134

3 个答案:

答案 0 :(得分:1)

//execute query
//fetch data into an array like Array(Array(name, quantity, id), Array(name, quantity, id)...)
//Loop through the array:
echo '<table>';
foreach ($results as $val) {
     echo '<tr><td>'.$val["typeName"].'</td>';
     echo '<td>'.$val["quantity"].'</td>':
     echo '<td>'.$val["typeID"].'</td></tr>';
}
echo '</table>';

i%1 == 0?真? :d

修改 啊,我现在得到了问题。您希望列表是水平的,而不是垂直的。嗯,这有点棘手,我能想到的唯一解决方案是使用三个循环:

//same as above to load the array
echo '<table>';
  echo '<tr>';
    foreach ($results as $val)
      echo '<td>'.$val["typeName"].'</td>';
  echo '</tr><tr>';
    foreach ($results as $val)
      echo '<td>'.$val["quantity"].'</td>';
  echo '</tr><tr>';
    foreach ($results as $val)
      echo '<td>'.$val["typeID"].'</td>';
  echo '</tr>';
echo '</table>';

答案 1 :(得分:0)

当然这会对你有帮助

<?php
$key = array();
$values = array();
foreach ($users as $user => $value) {
    $key[] = $user;
    $values[] = $value;
}
echo "<table>";
echo "<tr>";
foreach ($key as $single) {
    echo '<td>' . $single . '</td>';
}
echo "<tr>";

echo "<tr>";
foreach ($values as $single) {
    echo '<td>' . $single . '</td>';
}
echo "<tr>";

echo "</table>";
?>

答案 2 :(得分:-1)

如果你想要一个标题,其他标题就像下面那样,那么试试这个

Tritanium   Pyerite     Nocxium 
204          17          1 
205          24          3

=============================

<?php
$keys = array_keys($users[0]);

echo "<table>";
echo "<tr>";
foreach ($keys as $single) {
    echo '<th>' . $single . '</th>';
}
echo "<tr>";

echo "<tr>";
foreach ($users as $key=>$value) {
    echo '<td>' . $value. '</td>';
}
echo "<tr>";

echo "</table>";
?>