搜索后未显示数据库中的php数据 [英] php data from database is not shown after search

查看:101
本文介绍了搜索后未显示数据库中的php数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手,正在尝试对名称和日期进行搜索,但是我遇到了问题.

I am new to php and trying to make a search function on name and date but I have a problem.

我的问题是搜索功能会从搜索中看到所有受影响的行,但不会显示它们.我将数据库中的所有数据作为默认值加载到我的页面上,并且我希望如果将搜索中的$_GET['go']设置为不再显示所有默认数据,并且仅显示搜索结果.现在,他总是显示默认数据.除了$_GET['by']之外,没有显示任何数据.

My problem is that the search function sees all the effected rows from the search but does not show them. I load all the data from the database as default on my page and I want that if the $_GET['go'] from search isset that all the default data isn't shown anymore and only the results from the search are shown. Now he always shows that default data. Except from the $_GET['by'] there is no data shown.

我也尝试使用PDO,但是对此我不太了解....所以我也可能对此做错了.

I also try to use PDO but I don't know much about that.... so there's a possibility I am doing something wrong with that too.

我的PHP代码:

<?php


            ini_set('display_errors',1);
            ini_set('display_startup_errors',1);
            error_reporting(-1);

            //connect  to the database
            $db_host = "localhost";
            $db_username = "**";
            $db_password = "**";
            $db_name = "**";

            $db = new PDO('mysql:host=' .$db_host . ';dbname='. $db_name . '',$db_username,$db_password);


            if(isset($_POST['submit'])){
                if(isset($_GET['go'])){
                    if(preg_match("/^[a-zA-Z]+/", $_POST['search'])){
                        $zoek=$_POST['search'];

                        var_dump($zoek); //this will display on the screen the content of the variable 

                        // query  the database table
                        $sql="SELECT v.vacatureID, v.werkgeverID, v.functie, v.omschrijvingKort, v.datum, w.werkgeverID, w.naamBedrijf, w.plaats, w.image FROM vacature AS v JOIN werkgever AS w ON v.werkgeverID = w.werkgeverID WHERE w.naamBedrijf LIKE  '%" . $zoek .  "%' ORDER BY datum DESC";
                        // run  the query against the mysql query function
                        $result = $db->prepare($sql); 
                        $result->execute(); 

                        // count number of rows that are effected
                        $rows = $result->fetchAll();
                        $numrows = count($rows);
                        echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

                        // create  while loop and loop through result set
                        while($row = $result->fetch()){
                                $Functie=$row['functie'];
                                $OmschrijvingKort=$row['omschrijvingKort'];
                                $Datum=$row['datum'];
                                $Bedrijf=$row['naamBedrijf'];
                                $Plaats=$row['plaats'];
                                $Image=$row['image'];
                                $ID=$row['vacatureID'];

                            // truncate
                            if (strlen($Functie) > 20) {
                                // truncate string
                                $stringCut = substr($Functie, 0, 20);
                                // make sure it ends in a word
                                $Functie = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
                            }
                            if (strlen($Bedrijf) > 25) {
                                // truncate string
                                $stringCut2 = substr($Bedrijf, 0, 14);
                                // make sure it ends in a word
                                $Bedrijf = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            if (strlen($OmschrijvingKort) > 63) {
                                // truncate string
                                $stringCut2 = substr($OmschrijvingKort, 0, 63);
                                // make sure it ends in a word
                                $OmschrijvingKort = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            // display the result of the array
                            echo 
                            "
                                <div class='vacatureinfo2'>
                                    <img class='userimg2' src='../../uploads/userimage/". $Image ."' id='imge'/>
                                    <p class='func-br-pl pull-left'>". $Functie ." - ". $Bedrijf ." - ". $Plaats ."</p><p class='dtm pull-right'>". $Datum ."</p>
                                    <div class='omschrijvingkort2'>
                                        ". $OmschrijvingKort ."
                                    </div>
                                    <a href='#' class='pull-right'>Meer informatie</a>
                                </div>
                                <hr>
                            ";
                        }
                    }
                    elseif(preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}/", $_POST['searchdate'])){
                        $zoek=$_POST['searchdate'];

                        var_dump($zoek); //this will display on the screen the content of the variable 

                        //-query  the database table
                        $sql="SELECT v.vacatureID, v.werkgeverID, v.functie, v.omschrijvingKort, v.datum, w.werkgeverID, w.naamBedrijf, w.plaats, w.image FROM vacature AS v JOIN werkgever AS w ON v.werkgeverID = w.werkgeverID WHERE v.datum LIKE '%" . $zoek ."%' ORDER BY datum DESC";
                        //-run  the query against the mysql query function
                        $result = $db->prepare($sql); 
                        $result->execute(); 
                        $rows = $result->fetchAll();
                        $numrows = count($rows);
                        echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

                        //-create  while loop and loop through result set
                        while($row = $result->fetch()){
                                $Functie=$row['functie'];
                                $OmschrijvingKort=$row['omschrijvingKort'];
                                $Datum=$row['datum'];
                                $Bedrijf=$row['naamBedrijf'];
                                $Plaats=$row['plaats'];
                                $Image=$row['image'];
                                $ID=$row['vacatureID'];

                            //-truncate
                            if (strlen($Functie) > 20) {
                                // truncate string
                                $stringCut = substr($Functie, 0, 20);
                                // make sure it ends in a word
                                $Functie = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
                            }
                            if (strlen($Bedrijf) > 25) {
                                // truncate string
                                $stringCut2 = substr($Bedrijf, 0, 14);
                                // make sure it ends in a word
                                $Bedrijf = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            if (strlen($OmschrijvingKort) > 63) {
                                // truncate string
                                $stringCut2 = substr($OmschrijvingKort, 0, 63);
                                // make sure it ends in a word
                                $OmschrijvingKort = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            //-display the result of the array
                            echo 
                            "
                                <div class='vacatureinfo2'>
                                    <img class='userimg2' src='../../uploads/userimage/". $Image ."' id='imge'/>
                                    <p class='func-br-pl pull-left'>". $Functie ." - ". $Bedrijf ." - ". $Plaats ."</p><p class='dtm pull-right'>". $Datum ."</p>
                                    <div class='omschrijvingkort2'>
                                        ". $OmschrijvingKort ."
                                    </div>
                                    <a href='#' class='pull-right'>Meer informatie</a>
                                </div>
                                <hr>
                            ";
                        }
                    }
                    else{
                        echo  "<p>Please enter a search query</p>";
                    }
                }
            }//end of search form script                
            if(isset($_GET['by'])){
                $letter=$_GET['by'];

                $sql="SELECT v.vacatureID, v.werkgeverID, v.functie, v.omschrijvingKort, v.datum, w.werkgeverID, w.naamBedrijf, w.plaats, w.image FROM vacature AS v JOIN werkgever AS w ON v.werkgeverID = w.werkgeverID WHERE SUBSTRING(w.naamBedrijf,1,1) LIKE  '%" . $letter .  "%' ORDER BY datum DESC";

                $result = $db->prepare($sql); 
                $result->execute(); 

                $rows = $result->fetchAll();
                $numrows = count($rows);
                echo  "<p>" .$numrows . " results found for '" . $letter . "'</p>"; 

                //-create  while loop and loop through result set
                while($row = $result->fetch()){
                    $Functie=$row['functie'];
                    $OmschrijvingKort=$row['omschrijvingKort'];
                    $Datum=$row['datum'];
                    $Bedrijf=$row['naamBedrijf'];
                    $Plaats=$row['plaats'];
                    $Image=$row['image'];
                    $ID=$row['vacatureID'];

                    //-truncate
                    if (strlen($Functie) > 20) {
                        // truncate string
                        $stringCut = substr($Functie, 0, 20);
                        // make sure it ends in a word
                        $Functie = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
                    }
                    if (strlen($Bedrijf) > 25) {
                        // truncate string
                        $stringCut2 = substr($Bedrijf, 0, 14);
                        // make sure it ends in a word
                        $Bedrijf = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                    }
                    if (strlen($OmschrijvingKort) > 63) {
                        // truncate string
                        $stringCut2 = substr($OmschrijvingKort, 0, 63);
                        // make sure it ends in a word
                        $OmschrijvingKort = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                    }

                    //-display  the result of the array
                    echo 
                    "
                        <div class='vacatureinfo2'>
                            <img class='userimg2' src='../../uploads/userimage/". $Image ."' id='imge'/>
                            <p class='func-br-pl pull-left'>". $Functie ." - ". $Bedrijf ." - ". $Plaats ."</p><p class='dtm pull-right'>". $Datum ."</p>
                            <div class='omschrijvingkort2'>
                                ". $OmschrijvingKort ."
                            </div>
                            <a href='#' class='pull-right'>Meer informatie</a>
                        </div>
                        <hr>
                    ";
                }
            }//end of our letter search script
            else{
                include('php/vacatureoverzichtphp.php');
            }
        ?>

include('php/vacatureoverzichtphp.php');中,我具有页面上显示的默认数据的代码.

In the include('php/vacatureoverzichtphp.php'); I have the code for the default data that is shown on the page.

HTML代码:

<form class="form-inline pull-right" method="post" action="vacatureoverzicht.php?go"  id="searchform">
            <div class="form-group">
                <label>Zoeken op: </label>
            </div>
            <div class="form-group">
                <label class="sr-only" for="zoekenBedrijf">Bedrijfsnaam of Datum</label>
                <input type="text" class="form-control" name="search" id="zoekenBedrijf" placeholder="Bedrijfsnaam">
                <label> of </label>
                <input type="text" class="form-control" name="searchdate" id="zoekenDatum" placeholder="Datum">
            </div>
            <button type="submit" name="submit" value="Search" class="btn btn-default">zoeken</button>
        </form>
        <br><br><br>
        <p><a  href="vacatureoverzicht.php">Alles</a> | <a  href="?by=A">A</a> | <a  href="?by=B">B</a> | <a  href="?by=C">C</a> | <a  href="?by=D">D</a> | <a  href="?by=E">E</a> | <a  href="?by=F">F</a>
        | <a  href="?by=G">G</a> | <a  href="?by=H">H</a> | <a  href="?by=I">I</a> | <a  href="?by=J">J</a> | <a  href="?by=K">K</a> | <a  href="?by=L">L</a> 
        | <a  href="?by=M">M</a> | <a  href="?by=N">N</a> | <a  href="?by=O">O</a> | <a  href="?by=P">P</a> | <a  href="?by=Q">Q</a> | <a  href="?by=R">R</a>
        | <a  href="?by=S">S</a> | <a  href="?by=T">T</a> | <a  href="?by=U">U</a> | <a  href="?by=V">V</a> | <a  href="?by=W">W</a> | <a  href="?by=X">X</a> 
        | <a  href="?by=Y">Y</a> | <a  href="?by=Z">Z</a></p>

推荐答案

如果我正确获取了您的代码,则问题出在这里:

if I got your code correctly the issue is here:

$rows = $result->fetchAll();
$numrows = count($rows);
echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

// create  while loop and loop through result set
while($row = $result->fetch()){

因此,您首先做了fetchAll(),然后又尝试了while($row = $result->fetch()){.但是您无法从相同的结果中再次获取.

So you did fetchAll() first and then you are trying to while($row = $result->fetch()){. but you can't fetch again from the same result.

所以您应该将循环头更改为:

so you should change your loop header to :

 foreach($rows as $row){

因此完整片段如下:

$rows = $result->fetchAll();
$numrows = count($rows);
echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

// create  while loop and loop through result set
foreach ($rows as $row ){

希望会有所帮助:-)

这篇关于搜索后未显示数据库中的php数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆