Jump to content

pagination error


harkly

Recommended Posts

I have a 2 part issue, I don't believe they are related but maybe.

 

#1

 

I am trying to set up pagination, this is my 1st time and its not working out so great  :-\

 

What is happening is when you click on the link to go to the next set of data the variables don't seem to be passing.

 

Error message::

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND user.genderPref =' at line 12

 

If I hard code the info in it works fine.

 

WHERE user.bd_year <= 1992 AND user.bd_year >=1911 AND user.gender = 2 AND user.genderPref = 1

 

What I get when I echo out the $where

WHERE user.bd_year <= blank AND user.bd_year >= blank AND user.gender = blank AND user.genderPref = blank

 

I was thinking I could pass the variables in a session, never done sessions before but it appears to be working for the 1st page with same issue on 2nd page.

 

 

#2

 

I am using a dynamic WHERE to do my search and for some reason it is leaving out the beginning of the $where

WHERE user.bd_year <= 1992 AND user.bd_year >

 

[sELECT user.userID, user.gender, user.genderPref, user.city, user.state, photos.photo_1, CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)MISSING INFO= 1911 AND user.gender = 2 AND user.genderPref = 1

 

 

 

Can someone help me out??

 

 

My Code::

 

<?php

session_start();

$gender=$_POST["gender"];
$genderPref=$_POST["genderPref"];
$ageMin=$_POST["ageMin"];
$ageMax=$_POST["ageMax"];
$today = date("Y");
$year1 = $today-$ageMin;
$year2 = $today-$ageMax;

$_SESSION['gender'] = $gender; // store session data
$_SESSION['genderPref'] = $genderPref; // store session data
$_SESSION['ageMin'] = $ageMin; // store session data
$_SESSION['ageMax'] = $ageMax; // store session data

$_SESSION['today'] = $today; // store session data
$_SESSION['year1'] = $year1; // store session data
$_SESSION['year2'] = $year2; // store session data
?>

                        <?php

                            require_once('zipcode.class.php');

                            include('library/login.php');
                            login();
                            mysql_select_db('harkly');


                            $smoke=$_POST["smoke"];
                            $religion=$_POST["religion"];
                            $zip_code=$_POST["zip_code"];
                            $heightMin=$_POST["heightMin"];
                            $heightMax=$_POST["heightMax"];
                            $hairColor=$_POST["hairColor"];
                            $eyeColor=$_POST["eyeColor"];


                                if(empty($zip_code)) {
                                    $where = "WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref AND user.genderPref = $gender ";
                                    if (!empty($smoke))
                                    {
                                        $where .= " AND about_me.smoking = $smoke";
                                    }
                                    if (!empty($religion))
                                    {
                                        $where .= " AND bkgd.relg = $religion";
                                    }
                                    if (!empty($heightMin))
                                    {
                                        $where .= " AND appearance.height >= $heightMin";
                                    }
                                    if (!empty($heightMax))
                                    {
                                        $where .= " AND appearance.height <= $heightMax";
                                    }
                                    if (!empty($hairColor))
                                    {
                                        $where .= " AND appearance.hair_color = $hairColor";
                                    }
                                    if (!empty($eyeColor))
                                    {
                                        $where .= " AND appearance.eye_color = $eyeColor";
                                    }
                                    $result = mysql_query("SELECT user.userID, user.gender, user.genderPref, user.city, user.state, photos.photo_1,
                                                                  CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5) < RIGHT(birth_date,5)) AS age
                                                           FROM user
                                                           LEFT JOIN photos
                                                           ON user.userID = photos.userID
                                                           LEFT JOIN about_me
                                                           ON user.userID = about_me.userID
                                                           LEFT JOIN bkgd
                                                           ON user.userID = bkgd.userID
                                                           LEFT JOIN appearance
                                                           ON user.userID = appearance.userID 
                                                           $where")or die(mysql_error());
                                } // end of empty($zip_code)

                                $num_rows = mysql_num_rows($result);
                                                    echo "SELECT user.userID, user.gender, user.genderPref, user.city, user.state, photos.photo_1,
                                                                  CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age
                                                           FROM user
                                                           LEFT JOIN photos
                                                           ON user.userID = photos.userID
                                                           LEFT JOIN about_me
                                                           ON user.userID = about_me.userID
                                                           LEFT JOIN bkgd
                                                           ON user.userID = bkgd.userID
                                                           LEFT JOIN appearance
                                                           ON user.userID = appearance.userID
                                                           $where";

echo "<br><BR>$where<br><br>";
// ---------------------------------------

    // number of rows to show per page
    $rowsperpage = 12;

// find out total pages
    $totalpages = ceil($num_rows / $rowsperpage);

// get the current page or set a default
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
        // cast var as int
        $currentpage = (int) $_GET['currentpage'];
    }
    else {
        // default page num
        $currentpage = 1;
    } // end if

// if current page is greater than total pages...
    if ($currentpage > $totalpages) {
        // set current page to last page
        $currentpage = $totalpages;
    } // end if

// if current page is less than first page...
    if ($currentpage < 1) {
        // set current page to first page
        $currentpage = 1;
    } // end if

// the offset of the list, based on current page
    $offset = ($currentpage - 1) * $rowsperpage;


// ---------------------------------------

                                if(empty($zip_code)) {
                                    $where = "WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref AND user.genderPref = $gender ";
                                    if (!empty($smoke))
                                    {
                                        $where .= " AND about_me.smoking = $smoke";
                                    }
                                    if (!empty($religion))
                                    {
                                        $where .= " AND bkgd.relg = $religion";
                                    }
                                    if (!empty($heightMin))
                                    {
                                        $where .= " AND appearance.height >= $heightMin";
                                    }
                                    if (!empty($heightMax))
                                    {
                                        $where .= " AND appearance.height <= $heightMax";
                                    }
                                    if (!empty($hairColor))
                                    {
                                        $where .= " AND appearance.hair_color = $hairColor";
                                    }
                                    if (!empty($eyeColor))
                                    {
                                        $where .= " AND appearance.eye_color = $eyeColor";
                                    }
                                    $result = mysql_query("SELECT user.userID, user.gender, user.genderPref, user.city, user.state, photos.photo_1,
                                                                  CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age
                                                           FROM user
                                                           LEFT JOIN photos
                                                           ON user.userID = photos.userID
                                                           LEFT JOIN about_me
                                                           ON user.userID = about_me.userID
                                                           LEFT JOIN bkgd
                                                           ON user.userID = bkgd.userID
                                                           LEFT JOIN appearance
                                                           ON user.userID = appearance.userID
                                                           $where
                                                           LIMIT $offset, $rowsperpage")or  die(mysql_error());

                                } // end of search

                                $num_rows = mysql_num_rows($result);
                                if ($num_rows == 0){
                                    echo "Sorry No Results found, please try again with a boarder search area."; }
                                else {
                                    // format search results
                                    $cells_wide = 4;
                                    echo " <table cellspacing='0' cellpadding='3' border='0' width='700'><tr> ";
                                    $c = 0;

                                    while ($r=mysql_fetch_array($result)){
                                        $userID=$r["userID"];
                                        $bd_year=$r["bd_year"];
                                        $gender=$r["gender"];
                                        $genderPref=$r["genderPref"];
                                        $photo_1=$r["photo_1"];
                                        $city=$r["city"];
                                        $state=$r["state"];
                                        $zip=$r["zip"];
                                        $age = $r['age'];

                                        if (0 < $c && 0 == $c % $cells_wide){
                                            echo " </tr><tr> "; }
                                            echo " <td width=175> ";
                                            echo "<div id='profiles'>
                                                  <a href='profile.php'>
                                                  <div id='name'>$userID</div> ";
                                            if (empty($photo_1)){
                                                echo " <div id='image'><img src='uploads/noPhoto.gif' width='100' height='100' border='0'></div></a> ";
                                            }
                                            else {
                                                echo " <div id='image'><img src='uploads/$photo_1' width='100' height='100' border='0'></div></a> ";
                                            }
                                            echo " <div id='status'>$age - $city, $state</div>
                                                   </div>
                                                   </td>";
                                            $c++;
                                        } // end of while
                                        echo " </tr>";
                                        echo " </table> ";
                                    }// end search results

/******  build the pagination links ******/

// range of num links to show
    $range = 3;

// if not on page 1, don't show back links
    if ($currentpage > 1) {
        // show << link to go back to page 1
        echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>‹‹ </a> ";

        // get previous page num
        $prevpage = $currentpage - 1;

        // show < link to go back to 1 page
        echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'> ‹ </a> ";
    } // end if

// loop to show links to range of pages around current page
    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
        // if it's a valid page number...
        if (($x > 0) && ($x <= $totalpages)) {
        // if we're on current page...
            if ($x == $currentpage) {
            // 'highlight' it but don't make a link
            echo " [<b>$x</b>] ";
            // if not current page...
        } else {
             // make it a link
            echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
        } // end else
        } // end if
    } // end for

// if not on last page, show forward and last page links
    if ($currentpage != $totalpages) {
        // get next page
        $nextpage = $currentpage + 1;

        // echo forward link for next page
        echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'> › </a> ";

        // echo forward link for lastpage
        echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>›› </a> ";
    } // end if

/****** end build pagination links ******/

  ?>

Link to comment
https://forums.phpfreaks.com/topic/208701-pagination-error/
Share on other sites

I didn't see a form on that page, so try your Post or Session data like this.

$gender= (isset($_POST["gender"])) ? $_POST['gender'] : $_SESSION['gender'];
$genderPref= (isset($_POST["genderPref"])) ? $_POST['genderPref'] : $_SESSION['genderPref'];
$ageMin= (isset($_POST["ageMin"])) ? $_POST['ageMin'] : $_SESSION['ageMin'];
$ageMax= (isset($_POST["ageMax"])) ? $_POST['ageMax'] : $_SESSION['ageMax'];
$today = date("Y");
$year1 = $today-$ageMin;
$year2 = $today-$ageMax;

$_SESSION['gender'] = $gender; // store session data
$_SESSION['genderPref'] = $genderPref; // store session data
$_SESSION['ageMin'] = $ageMin; // store session data
$_SESSION['ageMax'] = $ageMax; // store session data

//$_SESSION['today'] = $today; // store session data (NOT NEEDED, as date('Y') will return today.
//$_SESSION['year1'] = $year1; // store session data (NOT NEEDED, Let the calcs work.
//$_SESSION['year2'] = $year2; // store session data (NOT NEEDED, Let the calcs work.

Link to comment
https://forums.phpfreaks.com/topic/208701-pagination-error/#findComment-1090603
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.