PHP MySQL查询未在div中显示所有结果

问题描述 投票:0回答:1

我的代码有问题,我从数据库中选择了所有数据,但它仅返回2行。它是这样的:

result1https://i.stack.imgur.com/x5EFu.png

这是我的代码:

<?php $sql = "select document_type, document_title , issued_year from documents WHERE document_type=document_type GROUP BY document_type";
                                $query = $dbh -> prepare($sql);
                                $query->execute();
                                $results=$query->fetchAll(PDO::FETCH_OBJ);
                                if($query->rowCount() > 0)
                                {
                                foreach($results as $result)
                                {         
                                ?>
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" href="#<?php echo htmlentities($result->document_type);?>"><?php echo htmlentities($result->document_type);?></a>
                        </h4>
                                </div>
                    <div id="<?php echo htmlentities($result->document_type);?>" class="panel-collapse collapse">
                        <ul class="list-group">
                            <li class="list-group-item"><?php echo htmlentities($result->issued_year);?>
                                <ul>
                                    <li><a href="../documents/<?php echo htmlentities($result->filename);?>"><?php echo htmlentities($result->document_title);?></a></li>   
                                </ul>
                            </li>

请帮助我。谢谢!

编辑:我在GROUP BY子句中添加了发行年份。现在,它显示所有但返回2个策略的div。这可能是什么解决方案?谢谢

Result

这是我想要的结果。我希望2019年政策与1990年政策相同。

Result I wanted

php mysql pdo
1个回答
0
投票

按如下所示通过PHP中的类型删除GROUP BY组数据:

<?php $sql = '
SELECT
  document_type,
  document_title,
  issued_year
FROM
  documents';

$query = $dbh->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_ASSOC);
if($query->rowCount() > 0) {

// Converts result to ['type1'=>[...rows], 'type2'=>[...rows]]
$results = array_reduce($results, function($acc, $row) {
    if(!isset($acc[$row['document_type']])) {
        $acc[$row['document_type']] = [];
    }
    $acc[$row['document_type']][] = $row;
    return $acc;
}, []);

foreach($results as $type=>$result) {
foreach($result as $row) {
?>
<div class="panel panel-default">
    <div class="panel-heading">
        <h4 class="panel-title">
            <a data-toggle="collapse" href="#<? echo htmlentities($type); ?>">
                <? echo htmlentities($row['document_title']); ?>
            </a>
        </h4>
    </div>
    <div id="<?php echo htmlentities($type); ?>" class="panel-collapse collapse">
        <ul class="list-group">
            <li class="list-group-item">
                <?php echo htmlentities($row['issued_year']); ?>
                <ul>
                    <li>
                        <a href="../documents/<? echo htmlentities($row['filename']); ?>">
                            <? echo htmlentities($row['document_title']); ?>
                        </a>
                    </li>
                </ul>
            </li>
        </ul>
    </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.