Jump to content

session wont to all variables


Go to solution Solved by lovephp,

Recommended Posts

im trying to set the $criteria to session but when i print only the

 

 

$criteria[] = "Employment = '".$employment."'";
 
 

 

and 

 

 

 

 $criteria[] = "YEAR(FROM_DAYS(DATEDIFF(CURDATE(), dob))) BETWEEN '".$agefrom."' AND '".$ageto."'";

 

are getting the values of print_r($criteria);

 

 

Array ( [0] => Employment = 'Employed' [1] => YEAR(FROM_DAYS(DATEDIFF(CURDATE(), dob))) BETWEEN '18' AND '25' )

 

 

    if(isset($_POST['submit'])){            
    
            $employment = mysql_real_escape_string($_POST['employment']);
            $mstatus = mysql_real_escape_string($_POST['mstatus']);
            $agefrom = mysql_real_escape_string($_POST['agefrom']);
            $ageto = mysql_real_escape_string($_POST['ageto']);                    
            
            $criteria = array();
            if($employment !='')
            {
                $criteria[] = "Employment = '".$employment."'";
            }
            if($mstatus !='')
            {
                $criteria[] = "Maritalstatus = '".$mstatus."'";
            }
            if($agefrom !='' && $ageto !='')
            {
                $criteria[] = "YEAR(FROM_DAYS(DATEDIFF(CURDATE(), dob))) BETWEEN '".$agefrom."' AND '".$ageto."'";
            }
            
            $_SESSION['criteria'] = $criteria;
}

print_r($criteria);

Link to comment
https://forums.phpfreaks.com/topic/296617-session-wont-to-all-variables/
Share on other sites

Set them as associative named arrays.

//make sure starting session
session_start();

if(isset($_POST['submit'])){
//is a database connection started?            
    //trim and consider pdo or mysqli_* functions
            $employment = mysql_real_escape_string($_POST['employment']);
            $mstatus = mysql_real_escape_string($_POST['mstatus']);
            $agefrom = mysql_real_escape_string($_POST['agefrom']);
            $ageto = mysql_real_escape_string($_POST['ageto']);

if(!$_SESSION['criteria']){
$_SESSION['criteria'] = array();
}
            if($employment !='')
            {
                $_SESSION['criteria']['employment'] = $employment;
            }
            if($mstatus !='')
            {
                $_SESSION['criteria']['status'] = $mstatus;
            }
            if($agefrom !='' && $ageto !='')
            {
$_SESSION['criteria']['agefrom'] = $agefrom;
$_SESSION['criteria']['ageto'] = $ageto;


            }          


if(!empty($_SESSION['criteria'])){
echo "<pre>";
print_r($_SESSION['criteria']);
echo "</pre>";
}
}
Edited by QuickOldCar
  • Solution

got it, actually there was issues with the names of fields in search.php
 

but facing another issue if there isnt any search query the sesseion still displays results from more.php

 thought i added unset($criteria);

 

    if(isset($_POST['page'])):
     $paged=mysql_real_escape_string($_POST['page']);
    if(isset($_SESSION['criteria']) && !empty($_SESSION['criteria'])) { //assign to local var if available
    $criteria = $_SESSION['criteria'];
    $sql="SELECT * FROM profiles WHERE " . implode(' AND ', $criteria)." ORDER BY UserID DESC";
    if($paged>0){
           $page_limit=$resultsPerPage*($paged-1);
           $pagination_sql=" LIMIT  $page_limit, $resultsPerPage";
           }
    else{
    $pagination_sql=" LIMIT 0 , $resultsPerPage";
    }

    $result=mysql_query($sql.$pagination_sql);

    $num_rows = mysql_num_rows($result);
    if($num_rows>0){
    while($data=mysql_fetch_array($result)){
    $fname=$data['dob'];
    $lname=$data['Lname'];
    echo "<table border='0'>
    <tr>
    <td width='20%'>
    <img src='uploads/profiles/1432715714.jpg' class='loadimg'>
    </td>
    <td width='80%'>
    <h3>Balaswami Surraminiam</h3>
    <b>Age:</b> 60 <br/>    
    <b>Caste:</b> $fname <br/>
    <b>Height:</b> 4' 5'' <br/>
    <b>Profession:</b> Chartered Accountant <br/>
    <b>Location:</b> Badarpur Rly Township <br/>
    <a href='#'>view profile</a>
    </td>
    </tr>
    </table>";
    }
    }
    if($num_rows == $resultsPerPage){?>
     <li class="loadbutton"><button class="loadmore" data-page="<?php echo  $paged+1 ;?>">More. </button></li>
 <?php
  }else{
      echo "<li class='loadbutton'><span class='endload'> No more. </span></li>";
    unset($criteria);
 }
 }
  endif;
?>

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.