PHP / MySQL-如何将具有动态列名的数据(列成行)转置为表

时间:2019-03-06 18:33:11

标签: php mysql transpose

我知道关于这种事情还有其他问题/答案,但是花了我一段时间才专门弄清楚这一点,所以我认为我会与社区分享,因为可能还有其他人可以从中受益。

我已经将如下所示的代码组合在一起,效果很好,但是如果您需要添加一列,则意味着在甚至需要将其添加到表中所需的位置之前,将名称添加4次:

<?php
// Prepare statement & retreive data from database
$sql_retreive = $con->prepare("
SELECT widget_id
     , widget_name
     , widget_price
     , widget_upc
     , widget_color
     FROM widgets
 WHERE widget_price > ?
");
$bind_process = $sql_retreive->bind_param('d',$price); 
$sql_retreive->execute();
$result = $sql_retreive->get_result(); 
// Initiate arrays to place variables from query in order to transpose data from database
$widget_id = [];
$widget_name = [];
$widget_price = [];
$widget_upc = [];
$widget_color = [];
// If there are results, fetch values for each row and add values to each corresponding array
if($result->num_rows > 0 ){
    while($row=$result->fetch_assoc()){
        $widget_id[] = $row['widget_id'];
        $widget_name[] = $row['widget_name'];
        $widget_price[] = $row['widget_price'];
        $widget_upc[] = $row['widget_upc'];
        $widget_color[] = $row['widget_color'];
    } // end of while
} // end of if num_rows > 0
// Build dynamic table with results transposed 
echo "<table class='table'><thead>";
echo "<tr><th>Widgets</th>"; for ($i=0; $i<count($crop_name);$i++) {echo "<th>".$widget_name[$i]." (".$widget_id[$i].")</th>";}
echo "</tr></thead><tbody>";
echo "<tr><td>widget_price</td>"; for ($i=0; $i<count($widget_price);$i++) {echo "<td>".$widget_price[$i]."</td>";} echo "</tr>";
echo "<tr><td>widget_upc</td>"; for ($i=0; $i<count($widget_upc);$i++) {echo "<td>".$widget_upc[$i]."</td>";} echo "</tr>";
echo "<tr><td>widget_color</td>"; for ($i=0; $i<count($widget_color);$i++) {echo "<td>".$widget_color[$i]."</td>";} echo "</tr>";
echo "</tbody></table>";
?>

所以我想找出一种更好的方法...请参阅下面的答案...

1 个答案:

答案 0 :(得分:0)

花了一段时间后,我想到了:

<?php
// Prepare statement & retreive data from database
$sql_retreive = $con->prepare("SELECT widget_id, widget_name, widget_price, widget_upc, widget_color FROM widgets WHERE widget_price > ?");
$bind_process = $sql_retreive->bind_param('d',$price); 
$sql_retreive->execute();
$result = $sql_retreive->get_result(); 
if($result->num_rows > 0 ){ // If there are results, fetch values for each row and add values to each corresponding array
    // Initiate an array for each field specified, in which to place variables from query, in order to transpose data from database
    for($i = 0; $i < mysqli_num_fields($result); $i++) { // loop through fields
        $field_info = mysqli_fetch_field($result); // for each, retreive the field info
        $column = $field_info->name; // retreive the field name from the field info
        $$column = []; // note double $$, create a blank array using the field name, will loop through for each
    } // end of for loop
    while($row=$result->fetch_assoc()){ // for each row of responses, place the data into the above created arrays
        $field_info = mysqli_fetch_fields($result); // retreive the field info
        foreach ($field_info as $field_value) { // for each, retreive the field info
            $column = $field_value->name; // retreive the field name from the field info
            $$column[] = $row[$column]; // note double $$, using the array (which uses the field name), place the row data in, and loop through for each
        } // end of foreach loop
    } // end of while
} // end of if num_rows > 0
// Build dynamic table with results transposed 
echo "<table class='table'><thead>";
echo "<tr><th>Widgets</th>"; for ($i=0; $i<count($crop_name);$i++) {echo "<th>".$widget_name[$i]." (".$widget_id[$i].")</th>";}
echo "</tr></thead><tbody>";
echo "<tr><td>widget_price</td>"; for ($i=0; $i<count($widget_price);$i++) {echo "<td>".$widget_price[$i]."</td>";} echo "</tr>";
echo "<tr><td>widget_upc</td>"; for ($i=0; $i<count($widget_upc);$i++) {echo "<td>".$widget_upc[$i]."</td>";} echo "</tr>";
echo "<tr><td>widget_color</td>"; for ($i=0; $i<count($widget_color);$i++) {echo "<td>".$widget_color[$i]."</td>";} echo "</tr>";
echo "</tbody></table>";
?>

这使您可以仅将列/字段名称添加到查询中,然后在表中所需的位置使用值。

如果您有帮助,请投票!