0

In cakephp, I executed this query to find the id field value of the corresponding username

$count = $this->User->find('all',array('conditions'=>array('User.username' => $n)))

How I will get take the value of a field from this array result to a variable?

I am new to cakephp and any help will be very useful.

1 Answer 1

4

Well, after calling find, you will notice that $count will be populated if something was brought from the database. I would change something, though, I would use "first" instead of "all" because you are finding only one record.

You can either use this

//in your controller
$count = $this->User->find('first', 
    array('conditions'=>array('User.username' => $n)));
//and set the variable to be used in the view in this way
$this->set('yourId', $count['User']['id']);

Then in your view

echo $yourId;

Or, you can also do this

$yourId = $this->User->field('id', array('User.username' => $n));
$this->set(compact('yourId'));

and then in your view

echo $yourId
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.