Hi
I have a website that can search for journal article and display it page by page. when you search, the results are displayed with check boxes and people can select items by ticking the check boxes. I am using PHP session to store checked items until they display selected items. It is woking with the 1st search. But if I do a second search the selection dont work. If I want it to wok I have to close the window and have to open a new window and seacrh, then it will work. Can any expert see what is the wrong with this script.

<?php
session_start();


// catch selected items
if(isset($_REQUEST['items'])) {
	$items = $_REQUEST['items'];
}
else {
	$items = array();
}

// get items from session
$sesItems = array();
if(isset($_SESSION['sItems'])) {
	$sesItems = $_SESSION['sItems'];
}
// add new items to session list
foreach($items as $item) {
	if(!in_array($item, $sesItems)) {
		array_push($sesItems, $item);
	}
}
$itemCount = count($sesItems); // get item count
$_SESSION['sItems'] = $sesItems; // set items back to session



$db_host="";
$db_name="";
$db_user="";
$db_pass="";

$per_page=20;

$connection=mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $connection);
$name=$_REQUEST['name'];
$lastSearch = $_SESSION['sName'];

//echo 'name: '. $name. ' last search: ' .$lastSearch;

if(isset($lastSearch) && $lastSearch != $name) { // new search
	$itemCount = 0;
	unset($_SESSION['sItems']);
}
else {
	 $_SESSION['sName'] = $name;
}

if($_REQUEST['mode'] == 'saved') {
	$itemsCsv = implode(',', $sesItems);
	$sql="SELECT COUNT(*) as total FROM journal WHERE rid IN ($itemsCsv)";
	$sql_result=mysql_query($sql, $connection) or die("Cannot find Server: ".mysql_error());
	$sql_result_array=mysql_fetch_array($sql_result);
	$total=$sql_result_array['total'];
	$sql="SELECT * FROM journal WHERE rid IN ($itemsCsv) ORDER BY DP DESC";
}
else {
	$sql="SELECT COUNT(*) as total FROM journal WHERE match (FAU, TI, AU, SO, PT, DP, MESH, KW, TA, AD, ISSN, NOTE) AGAINST ('$name' in boolean mode) ORDER BY DP DESC";
	$sql_result=mysql_query($sql, $connection) or die("Cannot find Server: ".mysql_error());
	$sql_result_array=mysql_fetch_array($sql_result);
	$total=$sql_result_array['total'];
	$sql="SELECT * FROM journal WHERE match (FAU, TI, AU, SO, PT, DP, MESH, KW, TA, AD, ISSN, NOTE) AGAINST ('$name' in boolean mode) ORDER BY DP DESC";
}

if ($total=="0"){$search_result="No Results Found</p>";}
elseif ($total > 0){$search_result="<p class='error'>".$total. " Articles Found</p>";}



if (empty($_REQUEST['result_set']))
{
$result_set=0;
$sql.=" LIMIT $result_set, $per_page";

}
else
{
$result_set=$_REQUEST['result_set'];
$sql.=" LIMIT $result_set, $per_page";
}
$sql_result=mysql_db_query($db_name, $sql);
$sql_rows=mysql_num_rows($sql_result);
?>

I think ur session value unset your code. May be ur session variable and some other variables same in your code that scenario session variable unset

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.