?
Solved

how can I hold a result set in a session

Posted on 2005-04-20
5
Medium Priority
?
322 Views
Last Modified: 2013-12-12
Below is a script I am using to search a database. First thing the script does is generates a random number to hold in a session for use in the order of the results.

Now I need to store that order and make acsessable to the paginating part of the script other wise the script does not have the rest of the results to print on the second or 3 rd pages if there are any.

SO what I need to do is:

 1. When a search is done store that those results in a session eash and everytime a   search is done. Thats result set can be destroyed if the person does another search.

2. The random generated number used for the ordering in all results sets should not be destroyed unless the person closes there browser.

3. Make the result set stored in a session available to the paginating part of the script so when there are multiple pages it has them to print out.

Hope I explained this ok

PS right now the script works fine the pagnating script works fine except it will only print results on the first page if there are other pages it prints nothing on those pages. Because it needs the results held in a session.
<?php
         session_start();


 require("login.php");

  if(!array_key_exists('generate', $_SESSION))
    $_SESSION['generate'] = rand(1,100);

  $generate = $_SESSION['generate'];
      $limit = 3;
?>

<?php







       
                         $id = str_replace(",", "','", $_POST['id']);
                    $name_last = str_replace(",", "','", $_POST['name_last']);
               $name_first = str_replace(",", "','", $_POST['name_first']);
             $country = str_replace(",", "','", $_POST['country']);
                 $sex = str_replace(",", "','", $_POST['sex']);




if(isset($_POST["Submit"]) && !empty($_POST["Submit"])){
    $country_block = $sql = "SELECT * FROM models WHERE country IN ('$country') AND sex IN ('$sex') ORDER BY RAND($generate)";

     $result = mysql_query($sql,$getconx) or die (mysql_error());
     $total = mysql_num_rows($result);
     if($total > $limit ) {

     $pages = floor(($total / $limit));

     if(($total % $limit) > 0){$pages++;}
     }
     if($total == 0) {
     echo"<b>No Models Where Found With Those Parameters.... Please Try Again!</b><br>";
             } else {
               echo"$total Models Found<br>";

    $color ="#FFFFFF";

     $records = $_GET['r'];
     if($records == 0) $records = $limit;
     if($_GET['f'] == 0) $row = 0; else $row = $_GET['f'];

     while ($row <  $records){

         
     // Echo out the results

     echo"ID: ".mysql_result($result,$row,'id')." &nbsp; Country: ".mysql_result($result,$row,'country')." &nbsp; &nbsp; Name: ".mysql_result($result,$row,'name_first')." &nbsp; ".mysql_result($result,$row,'name_last')." &nbsp; Sex: ".mysql_result($result,$row,'sex')." <br>";

     $row++;
     if($row == $total)break;
     if($color == "#ffffff") $color = "#F0F0F0"; else $color ="#FFFFFF";
     }
     }
}
                                   echo "<table border=0 valign=bottom height=25 align=left width=100% cellpadding=0 cellspacing=0><tr><td valign=bottom>"; #This piece of code makes the pages have link to the sections. Don't change it!
                                        $jrw = $jrr = 0;
                                        if($total > $limit) echo "&nbsp;Pages: ";
                                        for($ploop=1;$ploop<=$pages;$ploop++)
                                             {
                                              if($ploop != $pages) $spot = " | "; else $spot = "";
                                              if($f == $jrw) echo "<font class=pageact>[X]</font>". $spot;
                                              else
                                               echo "<a href=".$PHP_SELF."?f=".$jrw."&r=".$jrr." class=pagelnk>".$ploop ."</a>". $spot;
                                              $jrw += $limit;
                                              $jrr = $jrw + $limit;
                                             }
                                   echo "</td></tr></table>";


     ?>
0
Comment
Question by:smphil
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:iSeeCode
ID: 13830021
Instead of holding ALL RESULTS in the session, simple do something like this;


<?php
// Do this for the next page code:
echo "<a href='?current_page=".($current_page+1)."'>Next Page</a>";
echo "<a href='?current_page=".($current_page-1)."'>Previous Page</a>";

//Then to translate that..
if($current_page == "") {
   $current_page = 1;
}

// Amount per page
$amount = 30;

// Then to add to your result set each page

$country_block = $sql = "SELECT * FROM models WHERE country IN ('$country') AND sex IN ('$sex') ORDER BY RAND($generate) LIMIT ".(($current_page-1)*$amount)." , ".$amount;

?>

I think this will work better for you because a session can expire; this simply pulls what you need! =D



0
 
LVL 1

Author Comment

by:smphil
ID: 13830610
Would mind showing me where I intergrate this into?
Where do I put that into the script?
Soorrrrry if I sound dopey.

Thanks for your time and help
Phil

// Do this for the next page code:
echo "<a href='?current_page=".($current_page+1)."'>Next Page</a>";
echo "<a href='?current_page=".($current_page-1)."'>Previous Page</a>";

//Then to translate that..
if($current_page == "") {
   $current_page = 1;
}

// Amount per page
$amount = 30;
0
 
LVL 1

Expert Comment

by:iSeeCode
ID: 13831048
Simple use the Next and Previous links for your linkage between results, ( $current_page being the var that needs to be the current page ), and then the OTHER code just needs to be near the top of your code. =D

-Scotty
0
 
LVL 1

Author Comment

by:smphil
ID: 13832796
What do I do with my original pagination script do I dump it?
Could you cut and paste the script above and put the stuff in for me.

Phil
0
 
LVL 1

Accepted Solution

by:
iSeeCode earned 1500 total points
ID: 13836495
I don't have "login.php" so I can't test this but here we go...

This code will auto-pull ONLY that page of records from the database. You can declare the current page by changing to $current_page variable. Included is an example of having a link change the page (thus the Next and Previous links).

This code is entirely dependent upon the LIMIT modifier. (Check out Google on that.)

-Scotty

PS: If this doesn't work I'll just rewrite ALL your code for you; You have some lines in there currently I don't know WHAT you're doing with them, hehe. =D


<?php

session_start();

// Do this for the next page code:
echo "<a href='?current_page=".($current_page+1)."'>Next Page</a>";
echo "<a href='?current_page=".($current_page-1)."'>Previous Page</a>";

//Then to translate that..
if($current_page == "") {
   $current_page = 1;
}

// Amount per page
$amount = 30;

 require("login.php");

  if(!array_key_exists('generate', $_SESSION))
    $_SESSION['generate'] = rand(1,100);

  $generate = $_SESSION['generate'];
      $limit = 3;




       
                         $id = str_replace(",", "','", $_POST['id']);
                    $name_last = str_replace(",", "','", $_POST['name_last']);
               $name_first = str_replace(",", "','", $_POST['name_first']);
             $country = str_replace(",", "','", $_POST['country']);
                 $sex = str_replace(",", "','", $_POST['sex']);




if(isset($_POST["Submit"]) && !empty($_POST["Submit"])){
$country_block = $sql = "SELECT * FROM models WHERE country IN ('$country') AND sex IN ('$sex') ORDER BY RAND($generate) LIMIT ".(($current_page-1)*$amount)." , ".$amount;

     $result = mysql_query($sql,$getconx) or die (mysql_error());
     $total = mysql_num_rows($result);
     if($total > $limit ) {

     $pages = floor(($total / $limit));

     if(($total % $limit) > 0){$pages++;}
     }
     if($total == 0) {
     echo"<b>No Models Where Found With Those Parameters.... Please Try Again!</b><br>";
             } else {
               echo"$total Models Found<br>";

    $color ="#FFFFFF";

     $records = $_GET['r'];
     if($records == 0) $records = $limit;
     if($_GET['f'] == 0) $row = 0; else $row = $_GET['f'];

     while ($row <  $records){

         
     // Echo out the results

     echo"ID: ".mysql_result($result,$row,'id')." &nbsp; Country: ".mysql_result($result,$row,'country')." &nbsp; &nbsp; Name: ".mysql_result($result,$row,'name_first')." &nbsp; ".mysql_result($result,$row,'name_last')." &nbsp; Sex: ".mysql_result($result,$row,'sex')." <br>";

     $row++;
     if($row == $total)break;
     if($color == "#ffffff") $color = "#F0F0F0"; else $color ="#FFFFFF";
     }
     }
}
                                   echo "<table border=0 valign=bottom height=25 align=left width=100% cellpadding=0 cellspacing=0><tr><td valign=bottom>"; #This piece of code makes the pages have link to the sections. Don't change it!
                                        $jrw = $jrr = 0;
                                        if($total > $limit) echo "&nbsp;Pages: ";
                                        for($ploop=1;$ploop<=$pages;$ploop++)
                                             {
                                              if($ploop != $pages) $spot = " | "; else $spot = "";
                                              if($f == $jrw) echo "<font class=pageact>[X]</font>". $spot;
                                              else
                                               echo "<a href=".$PHP_SELF."?f=".$jrw."&r=".$jrr." class=pagelnk>".$ploop ."</a>". $spot;
                                              $jrw += $limit;
                                              $jrr = $jrw + $limit;
                                             }
                                   echo "</td></tr></table>";


     ?>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question