Jump to content

passing url parameter to a controller in cakephp


sonoton345

Recommended Posts

How can I pass a url parameter from a view to a controller. I have a link that's like this in my view...

 

<a href="http://localhost/jobsite/jobseekers/companyjobs/<?php echo $result_new['employers']['id'];  ?>">View jobs by this company</a>

 

I would like to pass the value of $result_new['employers']['id'] to a function in my controller to use in running a sql query. Is this possible and if yes how can I do this?

Many thanks.

This should be pretty easy.  Just pass the variable like so

 


function test($id) {
  if($id) {
    // do something with $id
  }
}

 

in the view you should use the html helper

 

echo $html->link('View jobs by this company', '/controller/test/' . $result_new['employees']['id']);

I actually want to pass from view to controller, not from controller to view. I want to pass the value of $result['employer']['id'] to the controller.

I tried ur code earlier on while struggling with but it's not sending any value to the controller.

function companyjobs($id){
	if($this->Session->check('userid'))
	{
		if($this->Session->read('usertype')=='jobseeker')
		{

			$id = $this->params['Employer']['id'];
			if($this->data['Employer']['id']==' ')
			  {
				  $this->redirect(array('action'=>'companymatch1'));
			  }

			else {					
			  
			  $result = $this->Postjob->query("SELECT postjobs.* FROM employers,postjobs WHERE postjobs.employer_id=employers.id AND employers.id='".$id."' ");
			     
			}
			if(!empty($result))
					{						  
					  
					  $this->set('result',$result);
						  
					}
					else
					{

						$this->redirect(array('action'=>'companymatch'));
					}


		}
		if($this->Session->read('usertype')=='employer')
		{
			$this->Session->setFlash('You are already logged in as an employer.', true);
			$this->redirect(array('controller'=>'employers','action'=>'welcome/'));	
		}
	}
	else
	{
		$this->Session->setFlash('You are not logged in or your session has expired. Please login again.', true);
		$this->redirect(array('action'=>'index/'));
	}
	$this->layout = 'mytemp2';

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.