Reputation: 157
This might be simple but it makes me nuts. Anyway, I have created a PHP session.
<?php
session_start();
$_SESSION['sort'] = 'product_name';
$_SESSION['type'] = 'DESC';
$_SESSION['limit'] = '5';
$sort = $_SESSION['sort'];
$type = $_SESSION['type'];
$limit = $_SESSION['limit'];
$query = mysql_query("SELECT * FROM table_name ORDER BY $sort $type LIMIT $offset, $limit");//This query is working...
I've created a conditional statement to change the sessions variables.
<a href="?action=orderList&sort=product_price&type=ASC">Sort Items from Lowest to Highest</a>
I've also created a loop process for product lists limit.
for($i=10; $i<100; $i+=10){
blah blah.../// Anyway, this is working already... Just give you a hint.
}
Considering that the current "Sort of Items" is set into "product_name" and "Type" is set into DESCENDING(DESC), and LIMIT is 5;
I want to change the values of registered sessions when the users click on the link above for sorting or limiting the items.
So what I did is :
if($_GET['action']=='orderList'){
///I've extracted the values of provided link
$_SESSION['sort'] = $_GET['sort'];
$_SESSION['type'] = $_GET['type'];
$_SESSION['limit'] = $_GET['limit'];
///PUT THEME INTO VARIABLES
$sort = $_SESSION['sort'];
$type = $_SESSION['type'];
$limit = $_SESSION['limit'];
}
Now, the session works only if the "?action=orderList" is present then after I navigate to another page, the sessions variables back to its original values as (sort=product_name, type=DESC and limit=5). Is there any ways or another PHP code to force the session to changed it's values?
EDITED :
Note that after the user click the "LINK" which contains different new variables for sessions, the session will be overwritten by the new variables provided by the link. Even the user click on (e.g. navigation ) the session should not be destructed.
Upvotes: 0
Views: 96
Reputation: 7880
Okay, so you really don't need to look at the action statement at all. We can check to see if a $_GET
is existent, and if so, assume that the $_GET
was passed.
What we do here is we see if they're passing a $_GET
var, if they are, then we replace their session variable. If they are not and their session variable is empty, then we reset the session variable to default. If their session variable is not empty, then we leave it alone and pull it into a var for cleaning (just in case).
<?php
session_start();
if(!empty($_GET['sort'])){
$sort = $_GET['sort'];
$_SESSION['sort'] = $sort;
} elseif(empty($_SESSION['sort'])) {
$sort = 'product_name';
$_SESSION['sort'] = $sort;
} else {
$sort = $_SESSION['sort'];
}
if(!empty($_GET['type'])){
$type = $_GET['type'];
$_SESSION['type'] = $type;
} elseif(empty($_SESSION['type'])) {
$type ='DESC';
$_SESSION['type'] = $type;
} else {
$type = $_SESSION['type'];
}
if(!empty($_GET['limit'])){
$limit = $_GET['limit'];
$_SESSION['limit'] = $limit;
} elseif(empty($_SESSION['limit'])) {
$limit = '5';
$_SESSION['limit'] = $limit;
} else {
$limit = $_SESSION['limit'];
}
//in the event someone slips in a ; delete from * where 1=1
$sort = mysql_real_escape_string(preg_replace('!;\*=!','',$sort));
$type = mysql_real_escape_string(preg_replace('!;\*!=','',$type));
$limit = mysql_real_escape_string(preg_replace('!;\*=!','',$limit));
/*Set $offset in here somewhere */
$query = mysql_query("SELECT * FROM table_name ORDER BY $sort $type LIMIT $offset, $limit");
?>
Upvotes: 1