【问题标题】:Display only specific data from array containing database values仅显示包含数据库值的数组中的特定数据
【发布时间】:2021-08-10 11:17:54
【问题描述】:

我有一个查询,它按月选择最后输入的数据到数据库中,我已将其分配到一个数组中并尝试将其显示在一个工作正常但数据组合在一起的表中,如下所示

示例 - 您在表格 1245871 上看到的第一个数字数据应显示如下

月 - 12 结束里程 - 45871

但是正如您在图像上看到的那样,这两个数据被组合在一起,我如何分离这两个值并相应地显示它们。代码如下,

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$querymain = "SELECT * FROM users";
$resultsmain = mysqli_query($connect,$querymain);
if ($resultsmain->num_rows>0) {
    while ($userid = mysqli_fetch_assoc($resultsmain)) {
        ?>
        <table class="table" class="mt-3 mb-3">
        <thead class="bg-dark text-light">
        <tr>
        <th colspan="3"><?php echo $userid['id']; ?></th>
        <th colspan="3"><?php echo $userid['name']; ?></th>
        <th colspan="3"><?php echo $userid['approved_kmpl'];?> KM</th>
        </tr>
        </thead>
        <thead>
        <tr>
        <th>Month</th>
        <th>Closing Mileage</th>
        <th>Usage For Month</th>
        <th>Required Per Month</th>
        <th>Excess Used</th>
        <th>(%)</th>
        <th>KM/L</th>
        <th>Consumed Liters</th>
        <th>Cost</th>
        </tr>
        </thead>
        
        <tbody>
        
<?php         
    $closingmileage = "SELECT extract(MONTH from date) as Month,
                              MAX(mileage) FROM mileagesnew 
                        WHERE user_id='".$userid['id']."' 
                        AND extract(YEAR FROM date) ='2020' 
                        group by Month 
                        ORDER BY month desc";
        
    $closingres = mysqli_query($connect,$closingmileage);
     if ($closingres->num_rows>0) {
        while ($closingrow = mysqli_fetch_assoc($closingres)) {
                
            $data =array($closingrow);
            foreach($data as $value){
                print_r($value);
                ?>
                <tr>
                <td> <?php echo implode($value); ?> </td>
                </tr>
                
                <tr>
                <td> <?php echo implode($value); ?> </td>
                </tr>
                <?php
            }
        }
    }
    else{
        echo "No Data Found";
    }
    ?>
        
        </tbody>
        
        </table>
        <?php
    }
}

【问题讨论】:

  • 一个做MOD,另一个做DIVIDE

标签: php mysql arrays


【解决方案1】:

您在查询Monthmileagesnew 的结果集中返回了2 列。所以$closingrow 将是一个包含 2 个成员的数组,命名为表中的列名,或者您在查询中为列名提供的别名。因此,您需要做的就是在 2 个表格单元格中分别输出 $closingrow['Month']$closingrow['mileagesnew']

我还假设您希望表格同一行上的 2 个值输出同一 &lt;tr&gt;...&lt;/tr&gt; 内的两个单元格以及尚未填充的空单元格

<?php         
    $closingmileage = "SELECT extract(MONTH from date) as Month,
                              MAX(mileage) FROM mileagesnew 
                        WHERE user_id='".$userid['id']."' 
                        AND extract(YEAR FROM date) ='2020' 
                        group by Month 
                        ORDER BY month desc";
    
    $closingres = mysqli_query($connect,$closingmileage);
    if ($closingres->num_rows>0) {        
        while ($closingrow = mysqli_fetch_assoc($closingres)) {
            echo "<tr>
                    <td>$closingrow[Month]}</td>
                    <td>$closingrow[mileagesnew ]</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                  </tr>";
    
        }
    } else{
        echo "No Data Found";
    }
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    相关资源
    最近更新 更多