Jump to content

destroy session?


kvnirvana

Recommended Posts

How do I loose the session “rand” if leaving the page? The problem is that if the user search it shows a random order, but if the user wants to go to another page and then back to the search page to make a new search the order I still the same. Think I somehow need to loose the session “rand”?

 

<?php

function search() 
{ 
if (isset($_POST)) {  foreach($_POST as $k=>$v) {    $_SESSION[$k]=$v;  }}






//base sql 
  mysql_connect("***", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());


//Parse input for where clause
$whereParams = array();
//get the values from the form //NOTE: You should do way more valdation on the values before you attempt to process anything
if ((!empty($_SESSION['be']))&&($_SESSION['be'] != 'alle'))
{
    $beSearch = mysql_real_escape_string(trim($_SESSION['be']));
    $whereParams[] = "`be` LIKE '{$beSearch}%'";
}
if ((!empty($_SESSION['omraede']))&&($_SESSION['omraede'] != 'alle'))
{
    $omraedeSearch = mysql_real_escape_string(trim($_SESSION['omraede']));
    $whereParams[] = "`omraede` LIKE '{$omraedeSearch}%'";
}
if ((!empty($_SESSION['pr']))&&($_SESSION['pr'] != 'alle'))
{
    $prSearch = mysql_real_escape_string(trim($_SESSION['pr']));
    $whereParams[] = "`pr` LIKE '{$prSearch}%'";
}
//Create where clause
$WHERE_CLAUSE = (count($whereParams)>0) ? " WHERE " . implode(' AND ', $whereParams) : '';

//Define the max records on a page
$records_per_page = 3;
//Run query to get total record count (for current filter)
$query1 = "SELECT COUNT(*) FROM `beha` {$WHERE_CLAUSE} GROUP BY `navn`";
$getcount = mysql_query($query1) or die(mysql_error());


$total_records  = mysql_num_rows($getcount);
//Calculate number of pages (for current filter)
$num_pages =  ceil($total_records/$records_per_page);



//Determine the page to display
$current_page = (isset($_GET['pg'])) ? (int) $_GET['pg'] : 1;
if($current_page<1 || $current_page>$num_pages) { $current_page = 1; }

//Define the limit start position for the current page of records (for current filter)
$limitstart = (($current_page-1)*$records_per_page);


if($_SESSION['pr'] != 'alle'){   

//Create query for the page of records to be displayed
$query = "SELECT * FROM `beha` {$WHERE_CLAUSE} GROUP BY navn ORDER BY total_value/total_votes DESC LIMIT $limitstart, $records_per_page";
}

if($_SESSION['pr'] == 'alle'){   

$rand = $_SESSION['rand'];
if (empty($rand)) {
srand((float)microtime()*1000003);
$rand = rand();
$_SESSION['rand'] = $rand;
}



$query = "SELECT * FROM `beha` {$WHERE_CLAUSE} GROUP BY navn ORDER BY RAND($rand) LIMIT $limitstart, $records_per_page";
}	

  //run query 
$result = conn($query);
  if (!$result){ die("No results due to database error.<br>".mysql_error());  }
  if (mysql_num_rows($result)==0 && ($result)!='alle')
  {
echo "<TR>";   
echo "<td style='text-align:left;'>"; 	
echo "<p>no results!</p>";
echo "<Td>";
echo "</TR>"; 

  }
  ?>

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.