NotJay
NotJay

Reputation: 4076

Create Session, Establish Variable and Transfer Variable to Next Page

This page I am working on has a list of links which I do not want to append url variables (.html?id=12345), so I thought it might be worth a shot to try sessions for the first time.

The page does a query to the database, gets the rows and then displays them in a list. The problem that I am running into is that the session variable is being overwritten by the next row in the while results. So the session carries the last variable on the page regardless of what the user clicks.

I am thinking that it is an issue of the placement of the code (rearranging), but I have had no luck yet getting this to properly pass the correct variable to the next page.

<?php
    session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
$tourQuery = mysql_query("SELECT * table WHERE ID = '".$page['main_id']."' AND DateTime >= CurDate() ORDER BY DateTime ASC");
$tourcount = mysql_num_rows($tourQuery);
while($tour = mysql_fetch_array($tourQuery))
    {
        $_SESSION['sessionname'] = $tour['EventID'];
    ?>
            <div class="schedule-row vevent" style="overflow:auto;"> 
                <div class="schedule-button">
                    <a class="schedlink url" href="<?=$pg['request']?>.html">
                        View
                    </a>
                </div>
                <br clear="all" /> 
            </div>
<?php       
}

So as you can see, a session variable is being determined for each result from the query, I am just trying to figure out how to pass that to the next page rather than the last variable on the page.

If you think the code from the next page is relevant, I can provide it, however, everything seemingly works on this page so I thought it would make sense to focus on the code above.

so, please help me.

Upvotes: 0

Views: 55

Answers (2)

bug
bug

Reputation: 342

Instead of using this:

$_SESSION['sessionname'] = $tour['EventID'];

Use this:

$_SESSION['EventID'][$tour['EventID']] = $tour['EventID'];

or

$_SESSION['Events'][$tour['EventID']] = $tour;

You don't need a session name if you don't manage cookies.

Upvotes: 0

jeroen
jeroen

Reputation: 91792

If you don't want to add variables to the url and there is no relation between the page $pg['request'] and the value $tour['EventID'] in the loop, the only way to send the user to the next page with the corresponding value, is to use a form, post it and add $tour['EventID'] as a (hidden) form field.

Something like:

<form action="<?php echo $pg['request']; ?>" method="post">
  <input type="hidden" name="id" value="<?php echo $tour['EventID']; ?>">
  <input type="submit" value="View">
</form>

(you might need to encode / cast your variables to be able to echo them out without problems)

Upvotes: 3

Related Questions