【问题标题】:PHP / MySQL - how to transpose data (columns into rows) with dynamic column names into a tablePHP / MySQL - 如何将具有动态列名的数据(列到行)转换为表
【发布时间】:2019-03-06 18:33:11
【问题描述】:

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

我已经将如下代码放在一起,效果很好,但如果您需要添加一列,则意味着添加名称 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>";
?>

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

【问题讨论】:

    标签: php mysql transpose


    【解决方案1】:

    经过一段时间的研究,我想出了这个:

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

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

    如果觉得有帮助,请点赞!

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 1970-01-01
      • 2020-09-30
      • 2022-01-21
      相关资源
      最近更新 更多