0

I am new to Zend and very keen to learn, so I would really appreciate some help and guidance.

I am trying to create a 'method in a class' that will save the session variables of product pages visited by members to a site i.e

i,e examplesite com/product/?producttype= 6

I want to save the number 6 in a session variable. I also do not want to have a global session for the entire site; I just want it for selected pages. So, I guess I have to have Zend_Session::start() on the selected page; but I am not clear how this should be done.

Should I instantiate it in the page view page. i.e products page or do this in the indexAction() method for the products page. I have attempted to instantiate it below but it did not work.

public function rememberLastProductSearched()

{          //my attempt to start a session start for this particular page.
      Zend_Session::start();

}

$session->productSearchCategory = $this->_request->getParam('product-search-category');
    return"  $session->productSearchCategory   ";
   }

else
{ 
  //echo " nothing there
 return "  $session->productSearchCategory";
 //"; 

}

}

With the rememberLastProductSearched() method I was trying to get the method to first check whether the user had searched for a new product or just arrived at the page by default. i.e whether he had used the get() action to search for a new product. If the answer is no, then I wanted the system to check whether their had been a previous saved session variable. so in procedural syntax it would have gone like this:

if(isset($_Get['producttype']))
 {
   //$dbc database connection
$producttype = mysqli_real_escape_string($dbc,trim($_GET['producttype']));

 }
  else
  if(isset($_SESSION['producttype'])){

   $producttype =   mysqli_real_escape_string($dbc,trim($_SESSION['producttype']));       

}

Can you please help me with the Zend/oop syntax. I am totally confused how it should be?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
andreea115
  • 289
  • 1
  • 3
  • 14

2 Answers2

0
$session = new Zend_Session_Namespace("productSearch");
if ($this->getRequest()->getParam('producttype')) { //isset GET param ?
    $session->productType = $this->getRequest()->getParam('producttype');
    $searchedProductType = $session->productType;
} else { //take the session saved value
    if ($session->productType) {
       $searchedProductType = $session->productType;
     }  
}
//now use $searchedProductType for your query
konradwww
  • 693
  • 7
  • 16
  • yes. i realise that part. but the issue is that i want to first check if the user has done a new search. i.e a new get product. if not, then i revert to the old session information. but if new product has been added, then i need to delete the old session variable and add the new information. is it clear waht i am seeking? – andreea115 Jun 11 '13 at 09:16
  • hi Konradwww and Rockford. i will try the codes now. thank you to both of you. – andreea115 Jun 11 '13 at 12:06
0

you're asking about simple work flow in an action, it should begin something like:

//in any controller
public function anyAction() 
{
    //open seesion, start will be called if needed
    $session  = new Zend_Session_Namespace('products');
    //get value
    $productCategory = $this->getRequest()->getParam('producttype');
    //save value to namespace
    $session->productType = $productCategory;
    //...
}

now to move this off to a separate method you have to pass the data to the method...

protected function rememberLastProductSearched($productType)
{
    //open seesion, start will be called if needed
    $session  = new Zend_Session_Namespace('products');

    $session->productType = $productType;
}

So now if you want to test for presence of a value...

 //in any controller
    public function anyAction() 
    {
        //open seesion, call the namespace whenever you need to access it
        $session  = new Zend_Session_Namespace('products');

        if (!isset($session->productType)) {
            $productCategory = $this->getRequest()->getParam('producttype');
            //save value to session
            $this->rememberLastProductSearched($productCategory)
        } else {
            $productCategory = $session->productType;
        }
    }

That's the idea.

Be mindful of your work flow as it can sometimes be very simple to inadvertently overwrite your session values.

RockyFord
  • 8,529
  • 1
  • 15
  • 21