-4

I'm very new to PHP and MySQLI, however, I have been dabbling with them for a couple of years.

I am working on adding a search function for some photos I have on my website based off of 3 criteria.

I've been successful in implementing the script on a different page, which does not include the syntax for the page limits. However, now, when I add in my $where_stmt code, I end up with some errors, and I believe it has to do with the placement of a needed }.

The script in question is:

<?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>

When I add it after the last else statement, I end up with an error that indicate Notice: Undefined variable: where_stmt in /xxx/xxx/public_html/vg/gallery/search_post.php on line 174

// 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
    }
}

If I add it just before the mysqli_close() then it throws an error of:

PHP Notice: Undefined variable: paginationCtrls in /xxx/xxx/public_html/vg/gallery/search_post.php on line 325

If I leave it out all together I get the following red x error in the web editor my hosting company provides me with:

Unexpected syntax, error $E0F

At this point, I'm not sure where this should be placed.

I should note that I can get the code to work when I get rid of the $where_stmt and the code above the $sql query.

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
  • Try adding `$where_stmt="";` in your `else` so that it always is set. – Nigel Ren Dec 20 '17 at 15:00
  • FYI: [Why check both isset() and !empty()](https://stackoverflow.com/q/4559925/476) – deceze Dec 20 '17 at 15:03
  • @NigelRen, thank you. i have added that. it didn't help, but I added it. [@]deceze, is it affecting the code negatively? Probably not. I did not write the code, personally, and I just translated it over to the new page. I'm not sure I see that it will affect me. So, really, your comment is unhelpful. – Daniël Cronk Dec 20 '17 at 15:09

1 Answers1

-1

I can recommend you to have a look at ORM libraries like http://propelorm.org/documentation/reference/model-criteria.html#finding-objects to avoid handwritten SQL and concatenated where clauses.

Also make sure that $paginationCtrls is declared before using it.

5422m4n
  • 790
  • 5
  • 12