SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination Session Problems

    I am currently having a problem with Pagination that I have been unable to over come. I am using PHP and MySQL. Currently I have Global Variables turned ON in my PHP.ini

    I have tried googling and I have searched through these forums along with other forums, but I havn't really found a solution to my problem. I must say I did not read through all 353 posts for pagination in this forum I only read through the first page of results.

    I am searching a database that has 380 some odd fields and around 500,000 records. I have built a basic search form that uses POST to submit the information to a page that searches the database and displays the records in the proper formatting.

    My code works perfectly fine for displaying all the records on one page. And my pagination works to the point of breaking up the pages evenly, and calculating how many pages there need to be, and the first page of the pagination displays without error. But my problem begins when I click next or click on page X. When I select another page other than page 1 I get no results displayed.

    I tracked my problem down to the fact that I am using POST in my form, it will POST the variables from the form into the first page, but it will not retain state and have the variables for any subsequent pages. The way I found this out was doing an Echo on my variables to see what was happening. And any page other than page 1 my variables were cleared, causing my SQL queries to up and die.

    Here is a copy of my code. (Please note this code is not yet complete, I still need to build the classes and use includes for some of the sensitive info. But as I am a beginner at PHP any suggestions would be much appreciated).

    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <?php
    $connection 
    mysql_connect('localhost''username''password');
    if (!
    $connection)
    {
       echo 
    "There was an Error connecting to the Database.  Please try again later.<br>";
    }

    $db mysql_select_db('NinjaRap'$connection);

    $limit 25

    $City strip_tags($City);
    $City trim ($City);
    $YearMin strip_tags($YearMin);
    $YearMin trim ($YearMin);
    $YearMax strip_tags($YearMax);
    $YearMax trim ($YearMax);

       
    $TypeSelection "";
       
    $key = array();
       foreach (
    $_POST['mlstype'] as $key[] => $value
          
    $TypeSelection implode("','"$key);

    If (
    $TypeSelection == "")
    {
       
    $TypeSelection "Single Family', 'Single Family w/ Acreage', 'Condo', 'Mobile/Manu Rented Lot',
          'Mobile/Manu Owned Lot', 'Recreational Land w/ Home', 'Townhouse', 'Business', 'Commercial', 'Industrial', 'Office', 'Other', 'Retail',
          'Agricultural', 'Build to Suit Commercial', 'Build to Suit Residential', 'Building Lots', 'Development', 'Recreation',
          'Apartment', 'Duplex', 'Fourplex', 'Triplex', 'Dairy', 'Farm', 'Multi Use', 'Ranch"
    ;
    }

    If (!
    $date checkdate(02,04,$YearMin))
    {
       
    $YearMin "1500";
    }
    If (
    $YearMax == "")
    {
       
    $YearMax date(Y)+10;
    }

    $AcresQuery " AND mlsacres BETWEEN " $Acreage " ";
    $AgeQuery "";
    $BathsQuery " AND mlsbaths " $Baths " ";
    $BedsQuery " AND mlsbeds  " $Beds " ";
    $GarageCapQuery " AND mlsgaragecapacity " $Garage " ";
    $LandSizeQuery "";
    $LandUseQuery "";
    $LevelsQuery " AND mlslevel " $mlsLevels " ";
    $ParkingQuery "";
    $ResInclQuery "";
    $SqftQuery " AND mlssqft " $sqfoot " ";
    $SubDivQuery "";
    $UnitsQuery "";
    $YearBuiltQuery " AND mlsyearbuilt BETWEEN '" $YearMin "' AND '" $YearMax "' ";

    If (
    $PropertyClass == "= 'Residential'")
    {
       
    $ParkingQuery "";
       
    $UnitsQuery "";
       
    $SubDivQuery "";
       
    $LandUseQuery "";
       
    $LandSizeQuery "";
       
    $AgeQuery "";
       
    $ResInclQuery "";
    }

    If (
    $PropertyClass == "= 'Business/Commercial'")
    {
       
    $AcresQuery "";
       
    $SubDivQuery "";
       
    $ResInclQuery "";
       
    $BathsQuery "";
       
    $BedsQuery "";
       
    $ParkingQuery "";
       
    $UnitsQuery "";
       
    $YearBuiltQuery "";
       
    $GarageCapQuery "";
       
    $LevelsQuery "";
    }

    If (
    $PropertyClass == "= 'Farm & Ranch'")
    {
       
    $LandSizeQuery "";
       
    $LandUseQuery "";
       
    $SubDivQuery "";
       
    $SqftQuery "";
       
    $ParkingQuery "";
       
    $UnitsQuery "";
       
    $YearBuiltQuery "";
       
    $GarageCapQuery "";
       
    $LevelsQuery "";
    }

    If (
    $PropertyClass == "= 'Residential Income'")
    {
       
    $AcresQuery "";
       
    $ResInclQuery "";
       
    $SqftQuery "";
       
    $BathsQuery "";
       
    $BedsQuery "";
       
    $YearBuiltQuery "";
       
    $GarageCapQuery "";
       
    $LevelsQuery "";
    }

    If (
    $PropertyClass == "= 'Land'")
    {
       
    $AgeQuery "";
       
    $ResInclQuery "";
       
    $SqftQuery "";
       
    $BathsQuery "";
       
    $BedsQuery "";
       
    $ParkingQuery "";
       
    $UnitsQuery "";
       
    $YearBuiltQuery "";
       
    $GarageCapQuery "";
       
    $LevelsQuery "";
    }

       
    $table_query "SELECT * FROM tblmls WHERE mlsclass " $PropertyClass "
                   AND mlsstatus IN('Active', 'New', 'Back on Market', 'Price Change', 'Contingent Finance', 'Contingent Sale', 'Contingent Multiple', 'Contingent Other')
                   AND mlscity LIKE  '%" 
    $City ."%'
                   AND mlslistprice BETWEEN '" 
    $PriceMin "' AND '" $PriceMax "'
                   AND mlstype IN ('"
    $TypeSelection "' )
                   
    $AcresQuery
                   
    $AgeQuery
                   
    $BathsQuery
                   
    $BedsQuery
                   
    $GarageCapQuery
                   
    $LandSizeQuery
                   
    $LandUseQuery
                   
    $LevelsQuery
                   
    $ParkingQuery
                   
    $ResInclQuery
                   
    $SqftQuery
                   
    $SubDivQuery
                   
    $UnitsQuery
                   
    $YearBuiltQuery
                   "
    ;

       
       
    $query_result mysql_query ($table_query$connection);

    $totalrows mysql_num_rows($query_result);

    if(empty(
    $page)){
            
    $page 1;
        }
        
    $limitvalue $page $limit - ($limit);

    mysql_free_result($query_result);

       
    $table_query "SELECT * FROM tblmls WHERE mlsclass " $PropertyClass "
                   AND mlsstatus IN('Active', 'New', 'Back on Market', 'Price Change', 'Contingent Finance', 'Contingent Sale', 'Contingent Multiple', 'Contingent Other')
                   AND mlscity LIKE  '%" 
    $City ."%'
                   AND mlslistprice BETWEEN '" 
    $PriceMin "' AND '" $PriceMax "'
                   AND mlstype IN ('"
    $TypeSelection "' )
                   
    $AcresQuery
                   
    $AgeQuery
                   
    $BathsQuery
                   
    $BedsQuery
                   
    $GarageCapQuery
                   
    $LandSizeQuery
                   
    $LandUseQuery
                   
    $LevelsQuery
                   
    $ParkingQuery
                   
    $ResInclQuery
                   
    $SqftQuery
                   
    $SubDivQuery
                   
    $UnitsQuery
                   
    $YearBuiltQuery
                   LIMIT 
    $limitvalue$limit
                   "
    ;

       
       
    $query_result mysql_query ($table_query$connection);
             
       Print 
    "<form method='POST' action='form3.html'>\n";

    ?>
    <body>
    <table width="100%"  border="1" cellspacing="0" cellpadding="0">
      <tr align="center">
        <td colspan="3">header</td>
      </tr>
      <tr>
        <td width="15%">&nbsp;</td>
        <td width="70%" align="center">Content Header </td>
        <td width="15%">&nbsp;</td>
      </tr>
      <tr>
        <td width="15%"><div align="left">Banneres Tables</div></td>
        <td width="70%" align="center"><table width="100%" height="104"  border="1" cellpadding="0" cellspacing="0">
          <tr>
           
    <?php
             
    While ($row mysql_fetch_object($query_result))
       {
          
          print 
    "\t<tr>";
          print 
    "\t\t<td width='100' height='120'> <a href=details3.php?MLSID=" $row->mlsnumber "><img src='\images\AsianDT.jpg'vspace=22 hspace=22 width='78' height='104'></a>";
          print 
    "\t\t<td border='1'>";
          print 
    "\t\t Price: $" number_format($row->mlslistprice,"","",",") . "<br>";
          print 
    "\t\t City: " $row->mlscity "<br>";
          print 
    "\t\t Beds: " $row->mlsbeds "<br>";
          print 
    "\t\t Baths: " $row->mlsbaths "<br>";
          print 
    "\t\t</td>";
          print 
    "\t\t<td>";
          print 
    "\t\t <a href=details3.php?MLSID=" $row->mlsnumber ">See Details</a>";
          print 
    "\t\t <a href=details3.php?MLSID=" $row->mlsnumber ">See Pictures</a>";
          print 
    "\t\t</td>";
          print 
    "\t</tr>";
       }

    mysql_free_result($query_result);
    mysql_close($connection);
    if(
    $page != 1){
            
    $pageprev $page-1;
           
            echo(
    "<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a> ");
        }else{
            echo(
    "PREV".$limit." ");
        }

        
    $numofpages $totalrows $limit;
       
        for(
    $i 1$i <= $numofpages$i++){
            if(
    $i == $page){
                echo(
    $i." ");
            }else{
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }


        if((
    $totalrows $limit) != 0){
            if(
    $i == $page){
                echo(
    $i." ");
            }else{
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }

        if((
    $totalrows - ($limit $page)) > 0){
            
    $pagenext $page++;
             
            echo(
    "<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
        }else{
            echo(
    "NEXT".$limit);
        }

    echo 
    $page; echo "page<br>";   
    echo 
    $totalrows; echo "totalrows<br>";
    echo 
    $limit; echo "limit<br>";
    echo 
    $pagenext; echo "pagenext<br>";
    echo 
    $numofpages; echo "numofpages<br>";
    echo 
    $pageprev; echo "pageprev<br>";
       
    ?>
    From what I have been reading most people will put this information into the URL, my issue with that is that I will be running rather large queries that could possibly search all 350 some odd fields by the time that I am done. And storing all that into the URL to me seems kind of bad. There are also some people that will store all this information in a cookie on the client machine. My understanding of cookies is that there is a limit to the size and storing this kind of information in a cookie probably isn't the best idea.

    Currently I have been playing around with using session variables but I have not had too much success with that. Earlier today I had thought I had resolved my problem by adding session_start(); to the beginning of my script and it appeared that my pagination was working. At least until the point where I changed my search criteria and then the whole thing crashed.

    If need be I am also willing to create another database that holds the session information or the query information. I will be building around 5 databases already and all will be about the same size as this one.

    I aplogize for the length of the post, I just wanted to be sure that I got my point across and gave all pertinent information. If you would like to see the code for the form please let me know and I will post that also. I also apologize if this problem has already been posted, if it has please show a link so that I may read through.

    Any links, tutorials or code that can be supplied would be greatly appreciated. I have been stuck on this problem on and off for the past week.

    I look forward to being a functioning member of this forum, from what I have read as to the posts and the information I have to say that I a rather impressed.

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    read this thread

    reg register globals, it's not a good practice turning this on.
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply Ernie1, I took your advice and I disabled Global variables and then changed all my variables to $_POST[variable], I tested the code and it still works.

    I read the link that you supplied and I was reviewing the code, but it appears that the solution is requireing me to use the URL to store all my variables.

    I am still attempting to use $_SESSION but I am having a problem keeping my variables active even in the $_SESSION array.

    I still do not completely understand Sessions and how to use them, I am still reading up on that so forgive my coding if it is completly incorrect.

    I tried changing my variables over to a $_SESSION after they were created from POST.
    PHP Code:
    if(empty($page)){
            
    $_SESSION[City]=$_POST[City];
            
    $_SESSION[PropertyClass]=$_POST[PropertyClass];
            
    $_SESSION[PriceMin]=$_POST[PriceMin];
            
    $_SESSION[PriceMax]=$_POST[PriceMax];
            
    $_SESSION[TypeSelection]=$_POST[TypeSelection];
            
    $_SESSION[AcresQuery]=$AcresQuery;
            
    $_SESSION[AgeQuery]=$AgeQuery;
            
    $_SESSION[BathsQuery]=$BathsQuery;
            
    $_SESSION[BedsQuery]=$BedsQuery;
            
    $_SESSION[GarageCapQuery]=$GarageCapQuery;
            
    $_SESSION[LandSizeQuery]=$LandSizeQuery;
            
    $_SESSION[LandUseQuery]=$LandUseQuery;
            
    $_SESSION[LevelsQuery]=$LevelsQuery;
            
    $_SESSION[ParkingQuery]=$ParkingQuery;
            
    $_SESSION[ResInclQuery]=$ResInclQuery;
            
    $_SESSION[SqftQuery]=$SqftQuery;
            
    $_SESSION[SubDivQuery]=$SubDivQuery;
            
    $_SESSION[UnitsQuery]=$UnitsQuery;
            
    $_SESSION[YearBuiltQuery]=$YearBuiltQuery;
        }


        
    $table_query "SELECT COUNT(*) AS 'numrec' FROM tblmls WHERE mlsclass " $_POST[PropertyClass] . 
                        AND mlsstatus IN('Active', 'New', 'Back on Market', 'Price Change', 'Contingent Finance', 'Contingent Sale', 'Contingent Multiple', 'Contingent Other')
                        AND mlscity LIKE  '%" 
    $_POST[City] ."%' 
                        AND mlslistprice BETWEEN '" 
    $_POST[PriceMin] . "' AND '" $_POST[PriceMax] . "'
                        AND mlstype IN ('"
    $_POST[TypeSelection] . "' )
                        "
    $_SESSION[AcresQuery] ."
                        "
    $_SESSION[AgeQuery] ."
                        "
    $_SESSION[BathsQuery] ."
                        "
    $_SESSION[BedsQuery] ."
                        "
    $_SESSION[GarageCapQuery] ."
                        "
    $_SESSION[LandSizeQuery] ."
                        "
    $_SESSION[LandUseQuery] ."
                        "
    $_SESSION[LevelsQuery] ."
                        "
    $_SESSION[ParkingQuery] ."
                        "
    $_SESSION[ResInclQuery] ."
                        "
    $_SESSION[SqftQuery] ."
                        "
    $_SESSION[SubDivQuery] ."
                        "
    $_SESSION[UnitsQuery] ."
                        "
    $_SESSION[YearBuiltQuery] ."
                        "

    For some reason those variables are being cleared out also.

    I will keep playing with it to see if I can find a solution, and I am also taking a look at possibly storing my variables and session information into a SQL database, but as I said I am still reading about this and trying to come up with a good solution.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    post vs get
    http://www.cs.tut.fi/~jkorpela/forms/methods.html

    you probably want to use get for this. if youre concerned that the amount of data will be too much for a url, you could use post to submit the search.

    you could then either:
    -store the value of the post array in session variables

    -or just run the query and save the result for a short period of time in sometable designed for this. i havent looked at the source code but that appears to be what vbulletin does. they store the result and then create an id for it, and then redirect the user to another url with the id in the url, and then the pagination is done based off of that id. this is probably a good idea if your query is expensive(takes a lot of resources to perform)

    Off Topic:


    string array keys should be quoted.
    eg $_POST[foo] should be $_POST['foo']

  5. #5
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have resolved the pagination problem.

    My problem wasn't my sessions having issues (like I thought) it was my pagination script itself.

    What happened was that it was not recognizing a page change.

    My old code.
    PHP Code:
    if($page != 1){
            
    $pageprev $page-1;
           
            echo(
    "<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a> ");
        }else{
            echo(
    "PREV".$limit." ");
        }

        
    $numofpages $totalrows $limit;
       
        for(
    $i 1$i <= $numofpages$i++){
            if(
    $i == $page){
                echo(
    $i." ");
            }else{
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }


        if((
    $totalrows $limit) != 0){
            if(
    $i == $page){
                echo(
    $i." ");
            }else{
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }

        if((
    $totalrows - ($limit $page)) > 0){
            
    $pagenext $page++;
             
            echo(
    "<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
        }else{
            echo(
    "NEXT".$limit);
        } 
    And my new code
    PHP Code:
    if($_GET['page'] > 1){ 
            
    $pageprev $_GET['page']-1;
            
            echo(
    "<a href=\"$PHP_SELF?page=$pageprev\"> PREV ".$limit."</a> "); 
        }

        
    $numofpages $totalrows $limit
        
        for(
    $i 1$i <= $numofpages$i++){
            if(
    $i == $page){
                echo(
    $i." ");
            }else{
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }


        if((
    $totalrows $limit) != 0){
            if(
    $i == $page){
                echo(
    $i." ");
            }else{
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> ");
            }
        }

        if((
    $totalrows - ($limit $_GET['page'])) > 0){
            
    $pagenext = ($_GET['page'] + 1);
             
            echo(
    "<a href=\"$PHP_SELF?page=$pagenext\"> NEXT ".$limit."</a>"); 
        } 
    My whole problem with creating my session was that I was doing an
    if(empty($page)){
    Create Session variables}
    The problem with that code though was $page was always empty. Once I disabled the global variables and went through line by line of the code changing everyting to $_POST and realized that $page was not being pulled from anywhere. So by forcing it to be $_GET['page'] it was pulling the correct variable, and when the page changed the value of $_GET['page'] changed.

    I still have to play with my sessions a little bit more to hammer out some bugs, and get it a little more secure. And I believe that I will use a virtual table to store the info, but for now I will leave it as is, as it is "functional".

    Thank you clamcrusher, I completely spaced putting the quotes in, and to be quite honest I bet I would have never noticed they were missing.

    Also thank you Ernie1, I read through the link that you gave me several times trying to figure out what it had to do with my problem with pagination and sessions, but along the way it gave me a better handle on pagination which led me to the solution to my problem.

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can do things better if you sanitize user input
    ex:
    PHP Code:
    //set the $self var
    $self=$_SERVER['PHP_SELF'];

    //set the $page var 
    $page=isset($_GET['page']) ? (int) $_GET['page'] : 1//or you can chage 1 to whatever you wish

    //then you can control the $page var

    if($page1){ 
            
    $pageprev $page-1
             
            echo(
    "<a href=\"$self?page=$pageprev\"> PREV ".$limit."</a> "); 
        } 

        
    $numofpages $totalrows $limit
         
        for(
    $i 1$i <= $numofpages$i++){ 
            if(
    $i == $page){ 
                echo(
    $i." "); 
            }else{ 
                echo(
    "<a href=\"$self?page=$i\">$i</a> "); 
            } 
        } 


        if((
    $totalrows $limit) != 0){ 
            if(
    $i == $page){ 
                echo(
    $i." "); 
            }else{ 
                echo(
    "<a href=\"$self?page=$i\">$i</a> "); 
            } 
        } 

        if((
    $totalrows - ($limit $page)) > 0){ 
            
    $pagenext = ($page 1); 
              
            echo(
    "<a href=\"$self?page=$pagenext\"> NEXT ".$limit."</a>"); 
        } 
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •