简体   繁体   中英

Transpose rows in columns with data from MySQL to PHP

I need your help, I have this table (image) and I want to put that data in columns per row like this fiddle , but I don't know how, I only know display in the same way like db table (rows). You know how can I do that?

在此处输入图片说明

This is the way for show data from MySQL that I use:

<?php 
    $sqlStr = "SELECT items.CA_id, items.item_id, items.item_Cant, items.CECO_cod,      items.item_desc, items.item_enduser
FROM items where CA_id = ".$CA_id;
    $sqlStrAux = "SELECT count(*) as total FROM items";

$aux = Mysql_Fetch_Assoc(mysql_query($sqlStrAux));
$query = mysql_query($sqlStr);  

    if($aux['total']>0){

        echo "</br></br>";
        echo "<div class='datagrid'>";
        echo "\t<table class=\"tablesorter\">\n";
        echo "<thead>";
        echo "<tr>
                <th>Item</th>
                <th>Cantidad</th>
                <th>CECO</th>
                <th>Descripción de solicitud</th>
                <th>Usuario final</th>              
              </tr>\n";
        echo "</thead>";      
        echo "<tbody>";
        while($row = mysql_fetch_assoc($query)){
      echo "\t\t<tr>
                    <td>".htmlentities($row['item_id'])."</td>
                    <td>".htmlentities($row['item_Cant'])."</td>
                    <td>".htmlentities($row['CECO_cod'])."</td>
                    <td>".$row['item_desc']."</td>
                    <td>".$row['item_enduser']."</td>
                </tr>\n";
    }
        echo "</tbody>";                  
        echo "\t</table>\n";
    }
        echo "</div>";
?>

I hope you understand, thanks.

Assuming you are asking how to flip the table sideways: Store the values in an array, and then echo the contents of each array per line, like so:

<?
while($row = mysql_fetch_assoc($query)){
    $itemID[] = htmlentities($row['item_id']);
    $item_Cant[] = htmlentities($row['item_Cant']);
    $CECO_cod[] = htmlentities($row['CECO_cod']);
    $item_desc[] = $row['item_desc'];
    $item_enduser[] = $row['item_enduser'];
}
echo "<table>";

echo "<tr><td>Item ID</td>";
for ($i=0; $i<count($itemID);$i++) {
    echo "<td>".$itemID[$i]."</td>";
}
echo "</tr><tr><td>Item Can't</td>";
for ($i=0; $i<count($item_Cant);$i++) {
    echo "<td>".$item_Cant[$i]."</td>";
}
echo "</tr><tr><td>Next row title...</td>";
...
...
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