Jump to content

[SOLVED] PHP Session Problem...


RIRedinPA

Recommended Posts

I'm building a photo archive site for my company (we're a publisher).

 

I'm working on the page navigation. Right now the site displays 18 results per page (6 to a row x 3 rows). If you get more than 18 results you obviously will need to navigate to see the other images. I'm trying to save the db query to a session variable so I can run the query and show the results that fit within the page your requesting - i.e. page 1 = results 1-18, 2 = results 19-36 and so on.

 

I do a check to see what kind of search you're performing. If it's from the search form it is a new one and I parse the code to build the query and then before I run it I have this code:

 

//save query to session
$_SESSION['query'] = $query;

 

which in this case is this: SELECT * FROM photorecord WHERE keywords LIKE '%school%' ORDER BY logNum DESC

 

This returns 29 results. If you click on the next link to see the results > than item 18 I pass the search type as 'nav' and use this code to set the $query var.

 

$query = $_SESSION['query'];

 

which comes out to the same as the initial query. Whoohoo! Brilliant. Except.

 

I am getting this error on my results page:

 

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Your request couldn't be procesed.  >:(

 

If I run the query through my mysql app (CocoaMySQL - I'm on a Mac) the query works fine.

 

Also, if do a new search where the query changes, such as this: SELECT * FROM photorecord WHERE keywords LIKE '%nurse%' ORDER BY logNum DESC, and save that to the session, when I recall $_SESSION['query'] I get the old sql query where the keyword equaled 'school'.

 

Any help would be appreciated.

 

 

Here's the relevant part of my code.

 

//get search type
$searchType = $_REQUEST['search'];
if ($searchType == "") { 
	$searchType = "new";
}

//do search based on type;
if ($searchType == "new") { 
	$keysToSearch = $_POST['keysToSearch'];
	$_SESSION['keysToSearch'] = $keysToSearch; 
	$keyList = split( chr(32), $keysToSearch);

	if (count($keyList) < 2) { 
		$query = "SELECT * FROM photorecord WHERE keywords LIKE '%" . $keyList[0] . "%'";
	} else { 
		for ($q=0; $q<=count($keyList); $q++) {
			if ($q == 0) { 
				$query = "SELECT * FROM photorecord WHERE keywords LIKE '%" . $keyList[$q] . "%'";
			} else if ($q > 0  && $q < count($keyList)) {
				$query .= " OR keywords LIKE '%" . $keyList[$q] . "%'";
			}
		}
	}

	$query .= " ORDER BY logNum DESC";

	//save query to session
	$_SESSION['query'] = $query;

} else if ($searchType == "nav") { 
	//$query = $_SESSION['query'];
	$query = $_SESSION['query'];
	$keysToSearch = $_SESSION['keysToSearch'];
}
echo $query;
//get results
$result = doQuery($query);
$itemCount = mysql_num_rows($result);


//show results
$i=0;
if ($itemCount > 0) { 
	while ($itemCount > $i) {
		$i++;
		$logNum = mysql_result($result,$i-1,'logNum'); 
		$logNumArray[] = $logNum; 
	}
}


//figure out all the page stuff
	$pageCount =  floor(round((count($logNumArray) / 18) + .5));
	$page = $_GET['page'];
	if ($page == "") { 
		$page = 1; 
	}

	$minPageItem = ($page * 18) - 17; 
	$maxPageItem = $minPageItem + 17;

?>

<div id="searchdata" style="position: absolute; top: 50px; left: 100px; z-index: 0">Your search for "<?php echo $keysToSearch; ?>" produced <?php echo $itemCount; ?> result(s)<p>Showing items 

<?php echo $minPageItem; ?> thru

<?php
if ($maxPageItem > count($logNumArray)) {  
	echo count($logNumArray); 
} else { 
	echo $maxPageItem; 
}
?>.
<br>

<?php 

if ($page == 1) {
	$page = 2; 
	echo "<a href='results.php?search=nav&page=" . $page . "'>[NEXT]</a>";
}  else if ($page < $pageCount) {
	$pageUp = $page + 1;
	$pageDown = $page - 1; 
	echo "<a href='results.php?search=nav&page=" . $pageUp . "'>[NEXT]</a>   <a href='results.php?search=nav&page=" . $pageDown . "'>[PREVIOUS]</a>";
} 	else if ($page == $pageCount) {
	$page = $pageCount - 1; 
	echo "<a href='results.php?search=nav&page=" . $page . "'>[PREVIOUS]</a>";
}
?>

</div> 

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.