Jump to content

Sessions to pass variables through pagination?


turpentyne

Recommended Posts

I have a search page that then submits to a php page that queries the database. I've been struggling to figure out how to paginate it, but I think I've narrowed down the problem. I didn't realize $_POST results didn't pass through when people clicked the pagination links at the bottom. I verified this by replacing  the LIKE variable with a specific word and it worked.

 

I'm trying to learn how to set up sessions, thinking this might solve my problem. Doesn't seem to be working. Can I get some suggestions on how to do this? What's happening now is that it's pulling a random 8 pages that have nothing to do with the original query.

 

I start my entire page with:

<?php

session_start();

$_SESSION['leafshape'] = $_post['select1'];
$_SESSION['leafcolor'] = $_post['select2'];
$_SESSION['leafvein'] = $_post['select3'];
$_SESSION['leafmargin'] = $_post['select4'];

$leafshape = $_SESSION['leafshape']; 
$leafcolor = $_SESSION['leafcolor'];
$leafvein = $_SESSION['leafvein']; 
$leafmargin = $_SESSION['leafmargin']; 

 

and I'm setting up the queries like this:

$query = "SELECT COUNT(*) as num
                    FROM
                        descriptors
                    JOIN
                        plantae ON (descriptors.plant_id = plantae.plant_name)
                    WHERE
                        descriptors.leaf_shape LIKE '{$_SESSION['leafshape']}'
                        AND descriptors.leaf_venation LIKE '{$_SESSION['leafvein']}'
                        AND descriptors.leaf_margin LIKE '{$_SESSION['leafmargin']}'";

 

I also tried replacing the '{$_SESSION['leafshape']}' in the query with the assigned variable $leafshape

 

 

Link to comment
Share on other sites

1) Should it be $_POST rather than $_post ?

2) If you have that posted part at the top of both page 1 and page 2 you'll get this problem:

 

At the very start of your script (on page 2) the $_SESSION['leafshape'] variable will contain the correct value which was set on page 1 (you can check this by putting in var_dump( $_SESSION) immediately after the session_start()), but you're immediately overwriting it with the (now empty) $_post['select1']. You need to check if $_post['select1'] is empty, and if it is then don't include the line:

$_SESSION['leafshape'] = $_post['select1'];

Link to comment
Share on other sites

Hmm. I think I've just run into a whole different problem now. I didn't notice I hadn't capitalized $_POST. Once I did, now the query only pulls one record, and has no pagination links at the bottom. uh-oh. (it should pull 6 records, at 2 per page)

 

The Session script is not at the top of page 1, because that's just a simple html form that sends to the second page made with php. I dropped in the var_dump( $_SESSION) and it shows the variable is filled. So maybe that part is ok?

 

 

Link to comment
Share on other sites

<?php

session_start();
if(isset($_POST)){
    $_SESSION['leafshape']  = $_POST['select1'];
    $_SESSION['leafcolor']  = $_POST['select2'];
    $_SESSION['leafvein']   = $_POST['select3'];
    $_SESSION['leafmargin'] = $_POST['select4'];
}
$leafshape = $_SESSION['leafshape']; 
$leafcolor = $_SESSION['leafcolor'];
$leafvein = $_SESSION['leafvein']; 
$leafmargin = $_SESSION['leafmargin']; 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.