$ where_stmt语法和}展示位置问题

时间:2017-12-20 14:58:04

标签: php mysqli where-clause

我是PHP和MySQLI的新手,但是,我已经和他们讨论了几年。

我正在根据3个标准​​为我网站上的一些照片添加搜索功能。

我已成功在不同的页面上实现脚本,但不包括页面限制的语法。但是,现在,当我添加$where_stmt代码时,我最终会遇到一些错误,而且我认为这与所需的展示位置有关。

有问题的剧本是:

<?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);

    if (!defined('DB_SERVER')) define('DB_SERVER', 'xxx');
    if (!defined('DB_USER')) define('DB_USER', 'xxx');
    if (!defined('DB_PASSWORD')) define('DB_PASSWORD', 'xxx');
    if (!defined('DB_TABLE')) define('DB_TABLE', 'xxx');

    // The procedural way
    $mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_TABLE);
    $mysqli->set_charset("utf8");
    $mysqli->query("SET NAMES 'utf8'");
    if (mysqli_connect_errno($mysqli)) {
        trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);
    }

    // This first query is just to get the total count of rows
    $sql = "SELECT COUNT(*) FROM tbl_photos";
    $query = mysqli_query($mysqli, $sql);
    $row = mysqli_fetch_row($query);
    // Here we have the total row count
    $rows = $row[0];
    // This is the number of results we want displayed per page
    $page_rows = 16;
    // This tells us the page number of our last page
    $last = ceil($rows/$page_rows);
    // This makes sure $last cannot be less than 1
    if($last < 1){
        $last = 1;
    }
    // Establish the $pagenum variable
    $pagenum = 1;
    // Get pagenum from URL vars if it is present, else it is = 1
    if(isset($_GET['pn'])){
        $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }
    // This makes sure the page number isn't below 1, or more than our $last page
    if ($pagenum < 1) { 
        $pagenum = 1; 
    } else if ($pagenum > $last) { 
        $pagenum = $last; 
    }

    // This sets the range of rows to query for the chosen $pagenum
    $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

        if(isset($_POST['submit']))
        if(isset($_GET['go']))
        if(isset($_REQUEST['submited'])) {

        $tid = $_POST['tID'];
        $lvmid = $_POST['lvmID'];
        $lid = $_POST['lID'];

        $where_stmt="";

        // query only for LuchtvaartmaatschappijID (LVMID)
        if((isset($lvmid) && !empty($lvmid)) && (!isset($tid) || empty($tid)) && (!isset($lid) || empty($lid)))
                {
            $where_stmt="WHERE lvm.luchtvaartmaatschappijID='".$lvmid."'";
                }
        // query only for ToestelID 
            elseif((isset($tid) && !empty($tid)) && (!isset($lvmid) || empty($lvmid)) && (!isset($lid) || empty($lid)))
                {
            $where_stmt="WHERE t.toestelID = '".$tid."'";
                }
        // query only for luchthaven    
            elseif((isset($lid) && !empty($lid)) && (!isset($lvmid) || empty($lvmid)) && (!isset($tid) || empty($tid)))
                {
                $where_stmt="WHERE img_location = '".$lid."'";  
                }
        // query only for Luchtvaartmaatschappij and Toestel    
            elseif((isset($lvmid) && !empty($lvmid)) && (isset($tid) || !empty($tid)) && (!isset($lid) || empty($lid)))
                {
                $where_stmt="WHERE lvm.luchtvaartmaatschappijID='".$lvmid."' and t.toestelID='".$tid."'";   
                }
        // query only for Luchtvaartmaatschappij and luchthaven 
            elseif((isset($lvmid) && !empty($lvmid)) && (isset($lid) || !empty($lid)) && (!isset($tid) || empty($tid)))
                {
                $where_stmt="WHERE lvm.luchtvaartmaatschappijID='".$lvmid."' and img_location = '".$lid."'";    
                }
        // query for luchtvaartmaatschappij, toestel and luchthaven
            elseif((isset($lvmid) && !empty($lvmid)) && (isset($tid) && !empty($tid)) && (isset($lid) && !empty($lid)))
            {
                $where_stmt="WHERE lvm.luchtvaartmaatschappijID='".$lvmid."' and t.toestelID='".$tid."' and img_location = '".$lid."'";
            }
            else
            {
            //where statement should be excluded as no filters are available
            }

    // This is your query again, it is for grabbing just one page worth of rows by applying $limit
    $sql = "
        SELECT randimgID, img_location, img_lvm, img_file, img_nmr, img_t, t.toestel, l.luchthavennaam, lvm.luchtvaartmaatschappij, lvm.luchtvaartmaatschappijID, t.toestelID

        FROM tbl_photos p

        LEFT JOIN tbl_luchthaven l
        ON p.img_location = l.luchthavenID

        LEFT JOIN tbl_luchtvaartmaatschappij lvm
        ON p.img_lvm = lvm.IATAcode

        LEFT JOIN tbl_toestel t
        ON p.img_t = t.toestelID

        $where_stmt

        ORDER BY lvm.luchtvaartmaatschappij, t.toestel ASC, p.img_nmr ASC $limit";

    $query = mysqli_query($mysqli, $sql);
    /* determine number of rows result set */

    $row_cnt = mysqli_num_rows($query);
    // This shows the user what page they are on, and the total number of pages
    $textline1 = "beelden (<b>$rows</b>)";
    $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
    // Establish the $paginationCtrls variable
    $paginationCtrls = '';
    // If there is more than 1 page worth of results
    if($last != 1){
        /* First we check if we are on page one. If we are then we don't need a link to 
           the previous page or the first page so we do nothing. If we aren't then we
           generate links to the first page, and to the previous page. */
        if ($pagenum > 1) {
            $previous = $pagenum - 1;
            $paginationCtrls .= '<li class="page-item"><a class="page-link"  href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">vorige</a> &nbsp; &nbsp; </li>';
            // Render clickable number links that should appear on the left of the target page number
            for($i = $pagenum-4; $i < $pagenum; $i++){
                if($i > 0){
                    $paginationCtrls .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; </li>';
                }
            }
        }
        // Render the target page number, but without it being a link
        $paginationCtrls .= '<li class="page-item active">
          <a class="page-link" href="#">'.$pagenum.' <span class="sr-only">(current)</span></a>
        </li>';


        // Render clickable number links that should appear on the right of the target page number
        for($i = $pagenum+1; $i <= $last; $i++){
            $paginationCtrls .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> </li> ';
            if($i >= $pagenum+4){
                break;
            }
        }
        // This does the same as above, only checking if we are on the last page, and then generating the "Next"
        if ($pagenum != $last) {
            $next = $pagenum + 1;
            $paginationCtrls .= '<li class="page-item"><a class="page-link"  href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">volgende</a> </li>';
        }
    }

    $list = '';
    $items_in_row = 2 ;
    $index = 0 ;

        echo "<table width='100%' cellspacing='1' border='0'>


              <tr>";

        while($row = mysqli_fetch_assoc($query)) {
            $img_location = $row['luchthavennaam'];
            $img_lvm = $row['img_lvm'];
            $l = htmlspecialchars($row['luchtvaartmaatschappij']);
            $lvmID = $row['luchtvaartmaatschappijID'];
            $img_nmr = $row['img_nmr'];
            $t = $row['toestel'];
            $tid = $row['toestelID'];
            $f = $row['img_file'];
            $rID = $row['randimgID'];
            $img_file = $row['img_file'];

            $image = "http://globe-trekking.com/vg/img/gallery/$img_lvm/$img_file"; 
            $link = "http://globe-trekking.com/vg/gallery/details.php?pid=$rID&lvmID=$lvmID&in=$img_nmr"; 

            $index++ ;


        echo "<td width='370' align='left' valign='top' bgcolor='#292960'> ";           

        echo "<table width='540' border='0' cellpadding='0' cellspacing='0'>";
        echo "<tbody>";
        echo "  <tr>";
        echo "  <td height='15' colspan='3' bgcolor='#292960'></td>";
        echo "  </tr>";
        echo "  <tr>";
        echo "    <td height='15' colspan='3' bgcolor='#000033'></td>";
        echo "  </tr>";
        echo "<tr>";
        echo "  <td width='15' bgcolor='#000033'>&nbsp;</td>";
        echo " <td bgcolor='#000033'><a href='" . $link ."'><img src='".$image."' width='510'></a></td>";

        echo "  <td width='15' bgcolor='#000033'></td>";
        echo "</tr>";
        echo "<tr>";
        echo "  <td bgcolor='#000033'>&nbsp;</td>";
        echo "  <td bgcolor='#000033'><table width='510' border='0' align='center' cellpadding='0' cellspacing='0'>";
        echo "    <tr>";
        echo "      <td height='15' colspan='2' valign='top' style='text-align: left'>&nbsp;</td>";
        echo "      </tr>";
        echo "    <tr>";
        echo "      <td width='72%' valign='top' style='text-align: left'><span style='font-size: 18px; color: #DEDEDE'><a class='airline' href='gallery_lvm.php?lvmid=$lvmID'>" . $l . "</a></span></td>";
        echo "      <td width='28%' style='text-align: left'><span style='font-size: 14.5px; color: #DEDEDE'>Reg: <a class='airline' href='gallery_reg.php?reg=$img_nmr'>" . $img_nmr . "</a></span></td>";
        echo "    </tr>";
        echo "    <tr>";
        echo "      <td height='10' colspan='2' valign='top' style='text-align: left'></td>";
        echo "      </tr>";
        echo "    <tr>";
        echo "      <td valign='top' style='text-align: left'><span style='font-size: 14.5px; color: #DEDEDE'><a class='airline' href='gallery_t.php?tid=$tid'>" . $t . "</a></span></td>";
        echo "      <td style='text-align: left'>&nbsp;</td>";
        echo "    </tr>";
        echo "    <tr>";
        echo "      <td height='10' colspan='2' valign='top' style='text-align: left'></td>";
        echo "      </tr>";
        echo "    <tr>";
        echo "      <td colspan='2' valign='top' style='text-align: left'><span style='font-size: 14.5px; color: #DEDEDE'>" . $img_location . "</span><br>
                <br>
              </td>";
        echo "    </tr>";
        echo "    <tr>";
        echo "      <td style='text-align: left'>&nbsp;</td>";
        echo "      <td style='text-align: left'>&nbsp;</td>";
        echo "    </tr>";
        echo "  </table></td>";
        echo "  <td bgcolor='#000033'>&nbsp;</td>";
        echo "</tr>";
        echo "<tr>";
        echo "  <td>&nbsp;</td>";
        echo "  <td>&nbsp;</td>";
        echo "  <td>&nbsp;</td>";
        echo "</tr>";
        echo "  </tbody>";
        echo "      </table>";
        echo "      </td>";

        if ($index%$items_in_row == 0){ 

        echo "    </tr>";

        echo "    <tr>";
             }
            } 

        echo "  </tr>";
        echo "  </table>";

    // Close your database connection
    mysqli_close($mysqli);

    ?>

    <br />

    <nav aria-label="Page Navigation">
        <ul class="pagination justify-content-center">
    <div class="pagination"><?php echo $paginationCtrls; ?></div>
    </ul>
    </nav>
    <br />

    </div>
    <br />
    <br />


    </div>
    <?php include '../includes/menu/footer.php'; ?>
    </body>
    </html>

当我在最后一个else语句之后添加它时,我最终会出现一个错误 注意:未定义的变量:第174行的/xxx/xxx/public_html/vg/gallery/search_post.php中的where_stmt

// query for luchtvaartmaatschappij, toestel and luchthaven
    elseif((isset($lvmid) && !empty($lvmid)) && (isset($tid) && !empty($tid)) && (isset($lid) && !empty($lid)))
    {
        $where_stmt="WHERE lvm.luchtvaartmaatschappijID='".$lvmid."' and t.toestelID='".$tid."' and img_location = '".$lid."'";
    }
    else
    {
    //where statement should be excluded as no filters are available
    }
}

如果我在mysqli_close()之前添加它,则会抛出错误:

  

PHP注意:未定义的变量:paginationCtrls in   第325行/xxx/xxx/public_html/vg/gallery/search_post.php

如果我将它全部放在一起,我会在托管公司为我提供的网页编辑器中收到以下红色x错误:

  

意外的语法,错误$ E0F

此时,我不确定应该放在哪里。

我应该注意,当我摆脱$where_stmt$sql查询上方的代码时,我可以让代码工作。

1 个答案:

答案 0 :(得分:-1)

我建议你看一下像http://propelorm.org/documentation/reference/model-criteria.html#finding-objects这样的ORM库,以避免手写SQL并连接where子句。

还要确保在使用之前声明$ paginationCtrls。