我很难得到正确的输出。我有一张这样的桌子:
我使用以下PHP代码来创建表:
<?php
include_once("includes/dbh.inc.php");
if(isset($_POST["r_id"])){
$output = '';
$query = "SELECT * FROM relatorioc WHERE datas = '".$_POST["r_id"]."'";
$res = mysqli_query($conn, $query);
$output .='
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_array($res)){
$output .= '
`<tr>
<td>'.$row['linha'].'</td>
<td>'.$row['user'].'</td>
<td>'.$row['prob'].'</td>
<td>'.$row['res'].'</td>
<td>'.$row['timep'].'</td>
<td>'.$row['timei'].'</td>
</tr>';
}
$output .= "</table></div>";
echo $output;
}
发布于 2018-06-10 18:15:11
在我回答你的问题之前,有一点很重要,那就是你的代码中有一个明显的安全漏洞,那就是SQL Injection:在进入SQL查询之前,你不会转义输入。($_POST["r_id"]
)
现在,对于您的问题,我所做的就是创建一个数组,并在while循环中输入按"linha“分组的行,然后使用两个foreach
输出。我还在SQL查询中为您转义了输入。(mysqli_real_escape_string
)
if (isset($_POST["r_id"])) {
$output = '';
$query = "SELECT * FROM relatorioc WHERE datas = '" . mysqli_real_escape_string($conn, $_POST["r_id"]) . "'";
$res = mysqli_query($conn, $query);
$data = [];
while ($row = mysqli_fetch_array($res)) {
$data[$row['linha']][] = $row;
}
$output .= '';
foreach ($data as $linha => $items) {
$output .= '<p>' . $linha . '</p>';
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
foreach ($items as $row) {
$output .= '
<tr>
<td>' . $row['linha'] . '</td>
<td>' . $row['user'] . '</td>
<td>' . $row['prob'] . '</td>
<td>' . $row['res'] . '</td>
<td>' . $row['timep'] . '</td>
<td>' . $row['timei'] . '</td>
</tr>';
}
$output .= '</table></div>';
}
echo $output;
}
发布于 2018-06-10 01:13:33
你需要先进行“group by”查询,然后再进行其他的分组,如果你没有很多Mysql的经验,也可以进行其他更复杂的查询。
(在第一个查询中按'linha‘分组,在带有where的其他查询之前(first resul are ='linha’值)
select * FROM relatorioc datas = '".$_POST"r_id"."'";group by 'linha‘before:$query = "SELECT * from relatorioc WHERE datas = '".$_POST"r_id"."'“linha=’上一个结果‘;
这对你来说是一个简单的解决方案。
发布于 2018-06-10 17:52:07
首先执行一个查询,根据您的需要对结果进行分组,以执行此->:
select * from relatorioc datas = '". $ _ POST [" r_id "]."' "; group by 'linha' with it will provide the necessary data to the following query ->: $ query = "SELECT * FROM relatoryc WHERE datas = '". $ _ POST ["r_id"]. "'" and linha = 'previous result';
https://stackoverflow.com/questions/50776342
复制相似问题