I need to get array MAX ID from column pr_id at my pr_data table. Here's my code what I have tried so far.
public function get_id_max($id) {
$this->db->select_max(array('pr_id' => $id));
$query = $this->db->get('pr_data');
return $query->row_array();
}
Try this:
$this->db->select_max('pr_id');
$this->db->where('pr_id', $id);
$result = $this->db->get('pr_data');
return $query->row_array();
Related
I am getting an error on line 10 :
Call to a member function getUserId() on a non-object in
/** #var $users User*/
$users= $this->getRepository("repo")->findAll();
$response = array();
foreach ($users $user) {
$response[] = array(
**line10 'user_id' => $user->getUserId()
);
}
basicly in line 10 it did not recognize the call to getUserId
so how could I fetch the data to array or json ?
thanks
Inside your loop on $campaigns, one of the elements of the array seem to be null, so this element does not have the method getUserId. Try to var_dump your $campaigns array to check its content, I'm pretty sure you would be able to find the mistake after that.
Try: $campaigns = $this->getDoctrine->getRepository("repo")->findAll();
Looks like $campaigns have no results, try:
if($capaigns) {
foreach ($campaigns as $user) {
$response[] = array('user_id' => $user->getUserId());
}
}
I have searched around for examples but i am getting an odd result
i wish to have the following occur /pages/1 URL to return from the database the markup column of row 1 of the pages table.
controller
public function index()
{
$id = $this->uri->segment(2);
$content = $this->page->specificMarkup($id);
$this->template->set('nav', 'support');
$this->template->set('content', $content);
$this->template->load('master', 'contact');
}
model
public function specificMarkup($id)
{
$query = $this->db->get_where('pages', $id);
$row = $query->row();
return $row->markup;
}
it works when i specifically set the $id, but now returns a 404 error when i try to use segments, the user guide gives me the impression that this should work.
I will answer my own question for others that run into this issue.
1.
the model needs to be
public function specificMarkup($id)
{
$query = $this->db->get_where('pages', array('id' => $id));
$row = $query->row();
return $row->markup;
}
2.
Their does need to be an entry in the routes.php
$route[ 'pages/(:num)' ] = 'pages/index/$1'
This is so codeigniter knows where to direct the traffic
Lets say I have the following tables: categories, posts and categories_posts. Category acts as a Tree. There is a habtm between Category and Post.
What I want to do from the Posts Controller is find a specific Post and list the Categories it belongs to in a threaded/nested format.
temporary solution: (my coding is probably rather poor - my apologies)
public function view($id=null) {
$this->Post->id = $id;
if($this->Post->exists()) {
$data = $this->Post->read();
$data['Category'] = $this->_thread($data);
$this->set(compact('data'));
}
}
public function _thread($data, $parent_id=null) {
$out = array();
$parents = Set::extract("/Category[parent_id={$parent_id}]", $data);
foreach($parents as $k => $item) {
$out[$k] = $item;
$out[$k]['Category']['children'] = Set::extract("/Category[parent_id={$item['Category']['id']}]", $data);
foreach($out[$k]['Category']['children'] as $key => $child) {
$out[$k]['Category']['children'][$key]['Category']['children'] = $this->_thread($data, $child['Category']['id']);
}
}
return $out;
}
This may help you out :
$data = $this->Post->find('threaded', array(
'conditions' => array('id' => 1)
));
'conditions' array is for example,you can pass your required conditions if any.
I want copy a row of data from one table to other. The table from i want to copy has id, name, password fields. The table where the values should be copied has id, name, password, post_count fields.
I am trying to use the following code but its not helping.
$data=$this->Register->find(null, $id);
if($this->User->save($data)) {
$this->Session->setFlash(__('Data copied'), true);
}
What should i do??
I have already removed the validations of the table where i want to copy the values to to.
Thanks in advance
The query $this->Register->find(null, $id); returns data in some different format. Instead I changed the query. The code is below
$condition = array('id'=>$id);
$data=$this->Register->find('first', array('conditions'=>$condition));
$newdata = array(
'User' => array(
'name' => $data['Register']['name'],
'password' => $data['Register']['password']
)
);
if($this->User->save($newdata)) {
$this->Session->setFlash(__('Data copied'), true);
}
This works as the return of the find('first') returns an array which can be modified as per our needs and then we can call the save method of User model.
$data = array();
$data = $this->Register->read(null, $id);
$newdata = array();
$newdata = array(
'User' => array(
'name' => $data['Register']['name'],
'password' => $data['Register']['password']
)
);
// If you are in Register controller then import User model
if($this->User->save($newdata, false)) {
$this->Session->setFlash(__('Data copied'), true);
}
I think the reason this isn't working for you is because the Register record by that ID doesn't exist. You must check if the results contain data before attempting to save it. This is why you get NULL,NULL in the INSERT statement.
$record = $this->Register->findById($id);
if(!empty($record))
{
$this->User->create();
if($this->User->save(array('User'=>$record['Register']),false,array('name','password')))
{
$this->Session->setFlash(__('Data copied'), true);
}
}
else
{
$this->Session->setFlash(__('Data not found'), true);
}
At the moment if I am doing a query on the database that should only return one row, using:
...query stuff...
$query = $this->db->get();
$ret = $query->result();
return $ret[0]->campaign_id;
Is there a CodeIgniter function to return the first row?
something like $query->row();
Or even better would be the ability to, if there was only one row,
to just use the query object directly.
e.g. $query->campaign_id;
You've just answered your own question :)
You can do something like this:
$query = $this->db->get();
$ret = $query->row();
return $ret->campaign_id;
You can read more about it here: http://www.codeigniter.com/user_guide/database/results.html
This is better way as it gives you result in a single line:
$this->db->query("Your query")->row()->campaign_id;
To add on to what Alisson said you could check to see if a row is returned.
// Query stuff ...
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$row = $query->row();
return $row->campaign_id;
}
return null; // or whatever value you want to return for no rows found
To make the code clear that you are intending to get the first row, CodeIgniter now allows you to use:
if ($query->num_rows() > 0) {
return $query->first_row();
}
To retrieve the first row.
$this->db->get()->row()->campaign_id;
Change only in two line and you are getting actually what you want.
$query = $this->db->get();
$ret = $query->row();
return $ret->campaign_id;
try it.
If you require to get only one record from database table using codeigniter query then you can do it using row(). we can easily return one row from database in codeigniter.
$data = $this->db->get("items")->row();
We can get a single using limit in query
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
You can do like this
$q = $this->db->get()->row();
return $q->campaign_id;
Documentation :
http://www.codeigniter.com/user_guide/database/results.html
Option 1
$limit = 1
$offset = 0
$query = $this->db->get_where('items', array('id' => $id), $limit, $offset);
Option 2
$this->db->get("items")->row();
class receipt_model extends CI_Model {
public function index(){
$this->db->select('*');
$this->db->from('donor_details');
$this->db->order_by('donor_id','desc');
$query=$this->db->get();
$row=$query->row();
return $row;
}
}