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.

Link to comment
Share on other sites

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']);

Link to comment
Share on other sites

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';

}

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.