PHP Searchbar仅显示1个结果

时间:2019-02-12 11:00:03

标签: php searchbar

我正在尝试在php中创建一个搜索栏。 我想从数据库中检索数据。 问题是,如果我搜索,只会得到1个结果。 我该如何解决? 还是有其他方法可以使我工作? 我的代码:

<?php
   include("config.php");
   session_start();


   $zoekopdracht = $_GET['search'];
   $categorie = $_GET['categorie'];
   $zoekopdracht = mysqli_real_escape_string($db,$zoekopdracht);
   $sql = ("SELECT * FROM activiteiten WHERE `inhoud` LIKE '%$zoekopdracht%'");
   $result = mysqli_query($db,$sql);
   $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
   $activiteit_id = $row['activiteit_id'];

   $naamsql = ("SELECT * FROM `activiteiten` WHERE activiteit_id = {$activiteit_id} AND optie = 'naam'");
   $naamresult = mysqli_query($db,$naamsql);
   $naamrow = mysqli_fetch_array($naamresult,MYSQLI_ASSOC);

   $beschrijvingsql = ("SELECT * FROM `activiteiten` WHERE activiteit_id = {$activiteit_id} AND optie = 'beschrijving'");
   $beschrijvingresult = mysqli_query($db,$beschrijvingsql);
   $beschrijvingrow = mysqli_fetch_array($beschrijvingresult,MYSQLI_ASSOC);

结果栏:

            <?php
              if(mysqli_num_rows($result) > 0){
                ?>
                <div class="Row" id="main" style="border: 2px solid #51BAA4;border-radius: 4px; height: 0px; width: 100%;padding: 0em 0 0em 0; margin: 0 auto; display: table;">
                  <div class="Column" id="plaatje" style="padding: 0em 0 0em 0;">
                    <left><img src="images/noimage.png" height="100px" width="100px" alt=""></left>
                  </div>
                  <div class="Column" id="titel">

                    <div style="text-align: left;position:relative;margin:0px;width:10%;height:40px;padding-top: 3px; font-size:125%; display: -webkit-box;-webkit-line-clamp: 1;">
                      <left><h3><?php echo $naamrow['inhoud'];?></h3>
                    </div>

                    <div style="text-align: left;  height: 0px;width: 450px; word-wrap: break-word;white-space: initial;position:absolute;">
                      <p style="  overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical"><?php echo "<b>"; echo $naamrow['inhoud']; echo "</b>,&nbsp;"; echo $beschrijvingrow['inhoud']; ?></p>
                    </div>


                  </div>
                </div>

                <?php

              }

            ?>

1 个答案:

答案 0 :(得分:1)

您仅在此处处理第一条记录。您需要为所有返回值重复操作。

//$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$naamrows = [];
$beschrijvingrow = [];
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {       
   $activiteit_id = $row['activiteit_id'];

   $naamsql = ("SELECT * FROM `activiteiten` WHERE activiteit_id = {$activiteit_id} AND optie = 'naam'");
   $naamresult = mysqli_query($db,$naamsql);
   $naamrow = mysqli_fetch_array($naamresult,MYSQLI_ASSOC);

   $beschrijvingsql = ("SELECT * FROM `activiteiten` WHERE activiteit_id = {$activiteit_id} AND optie = 'beschrijving'");
   $beschrijvingresult = mysqli_query($db,$beschrijvingsql);
   $beschrijvingrow = mysqli_fetch_array($beschrijvingresult,MYSQLI_ASSOC);
   // Store you data in array to be use later
   array_push($naamrows,$naamrow);
   array_push($beschrijvingrows,$beschrijvingrow);
}

现在更改模板以迭代数组以呈现所有内容。

<?php foreach ($naamrows as $key => $naamrow) { ?>
    <div class="Row" id="main"
        style="border: 2px solid #51BAA4;border-radius: 4px; height: 0px; width: 100%;padding: 0em 0 0em 0; margin: 0 auto; display: table;">
        <div class="Column" id="plaatje" style="padding: 0em 0 0em 0;">
            <left><img src="images/noimage.png" height="100px" width="100px" alt=""></left>
        </div>
        <div class="Column" id="titel">
            <div
                style="text-align: left;position:relative;margin:0px;width:10%;height:40px;padding-top: 3px; font-size:125%; display: -webkit-box;-webkit-line-clamp: 1;">
                <left>
                    <h3><?php echo $naamrow['inhoud'];?></h3>
            </div>
            <div
                style="text-align: left;  height: 0px;width: 450px; word-wrap: break-word;white-space: initial;position:absolute;">
                <p style="  overflow: hidden;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical">
                    <?php echo "<b>"; echo $naamrow['inhoud']; echo "</b>,&nbsp;"; echo $beschrijvingrows[$key]['inhoud']; ?></p>
            </div>
        </div>
    </div>
    <?php } ?>