简体   繁体   中英

Alter mysql result to product table

I have this code

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'

it produces this:

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 ) ) 

How in sweet good god do I turn it into this.

Tritanium   Pyerite     Nocxium 
204          17          1  

This should be really simple and I'm sure it is, but no matter what I try I can't get it to show up like that.

Tried this and so much else:

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

}

For chosen answer

Tritanium   Pyerite Mexallon    Tritanium   Pyerite Mexallon    Isogen  Tritanium   Mexallon    Isogen
107 213 107 56000   12050   2100    450 134 267 134
//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>';

Also i%1 == 0 ? Really? :D

EDIT: Aaah, I get the problem now. You want the list to be horizontally, not vertically. Well that is a little more tricky and the only solution that I could think of is using three loops:

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

Surely this will help you

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

if you want one header and others as its value just like below, then try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM