Codeigniter Limit Start - php

Hi i got a problem with a query with CodeIgniter 3.
I'm trying to get all users from database with limit and start excluding the ID of the user who is getting the list.
That's my model:
public function getUserList($id,$limit,$start){
$this->db->select('*');
$this->db->from('register');
$this->db->where('register.id' != $id);
$this->db->order_by("id");
$this->db->limit($limit,$start);
$result = $this->db->get();
return $result->result_array();
}
And that's the controller:
public function users(){
$this->load->model('User_model');
$id = $_GET['id'];
$limit = $_GET['limit'];
$start = $_GET['start'];
$data = $this->User_model->getUserList($id,$limit,$start);
echo "<pre>"; print_r($data); die;
$this->load->view('api', $data);
}
The problem is this function return me an empty array instead an array of 5 array.

Custom key/value method:
You can include an operator in the first parameter in order to control the comparison:
Change below line.
$this->db->where('register.id !=', $id);

Related

Why I can't retrieve all data given an ID? php-codeigniter

I'm trying to retrieve data from datbase given an ID number.
Controller:
Usuario.php
public function seleccionar_por_id_estado(){
$id_estado = $this->input->post('id_estado');
$result = $this->usuario_m->ver_datos_por_id($id_estado);
$data['result'] = $result;
$this->load->view('paginas/administrar_usuarios', $data);
}
My Model
usuario_m.php
public function ver_datos_por_id($id_estado){
$condicion = "estado =" . "'". $id_estado ."'";
$this->db->select('*');
$this->db->from('usuarios');
$this->db->where($condicion);
$query = $this->db->get();
return $query->result();
}
My View
administrar_usuarios.php
<form action="usuario/seleccionar_por_id_estado">
<div class="content well">
<label>Ver por:
<select id="id_estado">
<option>Activo</option>
<option>Inactivo</option>
</select>
<?php
foreach ($result as $u){?>
<?php echo $u->usuario;?> <br>
<?php } ?>
</form>
The error that I'm getting is that the var $result is not defined.
Could you help me, please? Thanks!
In your model.Set condition inside $this->db->where() like this
<?php
public function ver_datos_por_id($id_estado){
$this->db->select('*');
$this->db->from('usuarios');
$this->db->where('estado',$id_estado);//set your condition here
$query = $this->db->get();
return $query->result();
}
OR use $this->db->get_where
$query = $this->db->get_where('usuarios',array('estado'=>$id_estado));
return $query->result();
For more see docs Codeigniter Query Builder
UPDATE
Controller:
public function seleccionar_por_id_estado(){
$id_estado = $this->input->post('id_estado');
$data['result'] = $this->usuario_m->ver_datos_por_id($id_estado);
$this->load->view('paginas/administrar_usuarios', $data);
}
I think issue is in your where condition in model.
use this query
$query = $this->db->get_where('usuarios', array('column name of your table' => $condicion));
First, I'd check to be certain that $id_estado isn't empty.
Another possible problem is that Codeigniter really wants you to capitalize class and file names. I'd try renaming the model to 'Usuario_m.php', name the class in the model Usuario_m, and then load it by calling $this->load->model('Usuario_m); (looks like you forgot to load the model too) and then finally call the model function like this:
$result = $this->Usuario_m->ver_datos_por_id($id_estado);
Update
Your where() parameters are wrong too. Your entire function in the model can be just this:
public function ver_datos_por_id($id_estado){
return $this->db->get_where('usurarios', array('estado' =>$id_estado)->result();
}
The get_where function combines get() and where(), chooses the table with the first parameter (so you don't need $this->db->from('usuarios');)

Get a single value from a result set returned from PHP codeigniter model

I am working on PHPStorm with a codeigniter framework and the MVC architecture.
I need a function in my model that would query the sysuser table in my database and return the uid (User Id) when the username is passed.
sysuser Table structure
+---------------+----------------+-----------------+
| uid | name | username |
+---------------+----------------+-----------------+
| 1 | kim | kcdla |
+---------------+----------------+-----------------+
| 2 | Sam | sammyG |
+---------------+----------------+-----------------+
In my function, if I pass, for example, the username kcdla I need to retrieve the uid as 1 so that I can store this to a variable as $uid and return it to another function that will use it. But currently the result is an array and when I tried to access only the first cell from the array, I get an Exception.
Code in Context
public function getUserUid($username)
{
$this->db->select("*");
$this->db->from('sysuser');
$this->db->where('username',$username);
$query = $this->db->get();
$tableArray = $query->result();
foreach ($tableArray as $row) {
$uid = $row->uid;
}
return $uid;
}
I need to use the $uid value returned from this method within another method as shown below.
public function getUserDetails($username)
{
$uid = $this->userModel->getUserUid($username);
$this->db->select("*");
$this->db->from('userProfile');
$this->db->where('uid',$uid);
$query = $this->db->get();
return $query->result();
}
Any suggestions in this regard will be highly appreciated.
Try this. As you need single row based on username, you have to use row()(returns Object) or row_array() (returns array) instead of result() because result() or result_array() returns result set
public function getUserUid($username)
{
$this->db->select("*");
$this->db->from('sysuser');
$this->db->where('username',$username);
$query = $this->db->get();
$row = $query->row();
return $row->uid;
}
call the function like this
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
^^^^^^^^^^^^^^^^^^^^^^^^^^
$this->db->select("*");
$this->db->from('userProfile');
$this->db->where('uid',$uid);
$query = $this->db->get();
return $query->result();
}
Return the single row id like this
row(), returns only the first row. It's also an object, and if you need an array, you can use row_array()
public function getUserUid($username)
{
$this->db->select("*");
$this->db->from('sysuser');
$this->db->where('username',$username);
$query = $this->db->get();
$row_values = $query->row_array();
return $row_values['uid'];
}
As per your comment try this
public function getUserDetails($username)
{
$uid = $this->userModel->getUserUid($username);
$this->db->select("*");
$this->db->from('userProfile');
$this->db->join('another_table', 'another_table.uid = userProfile.uid');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$this->db->where('userProfile.uid',$uid);
$query = $this->db->get();
return $query->result();
}

fetching products of current user_id

I have function to fetch the products from ci_products table
the code is as follows
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$products = $this->db->get();
return $products;
}
now I want to fetch from where user_id is current user_id
I have tried few things but not working
what I have tried`
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$this->db->where('user_id',$this->session->userdata('user_id'));
$products = $this->db->get();
return $products;
}
$data = $this->db->get_where('product_ids',$product_ids);
$this->db->where_in('product_ids',$data);
$this->db->where('user_id',$this->session->userdata('user_id'));
$query = $this->db->get();
return $query->result_array();
Try this one :
In your first code:
function get_product_selections($product_ids)
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('product_id', $product_ids);
$products = $this->db->get();
return $products->result_array();
}
what do you mean when you put array() in the parameter? you want to retrieve an array() of products?
function get_product_selections()
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('user_id',$this->session->userdata('user_id')); // the user who is currently login
$products = $this->db->get();
return $products->result_array(); // return the results
}

How can i make to get not only by id but by one more column also

this my
model
public function get_news($NewsId= FALSE)
{
if ($NewsId === FALSE)
{
$this->db->select('*');
$this->db->from('news');
$this->db->where('Status' , 1);
$this->db->where('Language' , 2);
return $query->result_array();
$config['per_page']; $this->uri->segment(3);
}
$query = $this->db->get_where('news', array('NewsId' => $NewsId));
return $query->row_array();
controller
public function single($NewsId)
{
$data['news_item'] = $this->news_model->get_news($NewsId);
if (empty($data['news_item']))
{
show_404();
}
$data['NewsId'] = $data['news_item']['NewsId'];
$this->load->view('news/single', $data);
}
view
<?php echo $news_item['TittleNews'];?>
i have other view that foreach all my news table and i want create read more the i have created on up my code it works fine
want to make read more with NewsId and one other column it mean first check the NewsId and then other colum and then show me on my view which is singly
you can use a model method like this:
function getBy($data){
foreach($data as $key=>$value){
$this->db->where($key,$value);
}
$query = $this->db->get('users_table');
return $query->row(); //or $query->result(); as you want
}
so then you call it with
$this->model->getBy(
array(
'username'=>'Mark',
'id'=>'90',
'email'=>'asd#asd.com'
);
);
obviously you can then extend the model method as much as you can

Codeigniter - Calling a model method within same model is buggy

I am trying to call a model method within same model and its not working as intended. Here is my class with two methods which do not work
class mymodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->tablea = 'tablea';
$this->tableb = 'tableb';
}
public function saveData($data){
$dataCopy['revisionkey'] = $this->getRevisionKey($data['id']);
//check and condition revision key to be int with +1 from last one
$this->db->insert($this->tableb, $dataCopy);
$this->db->where('id', $id);
return $this->db->update($this->user_table, $data) ? true : false;
}
public function getRevisionKey($id){
$this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
->from($this->revision_tablea)
->where($this->revision_tablea.'.id', $id)
->order_by($this->revision_table.'.revisions_number', 'asc')
->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->row_array();
}else{
return 0;
}
}
}
Now method getRevisionKey() should produce a query like following
SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1
but it produces a query like following
SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `id` = '26' AND `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1
This of course is due to same method being called within the model, this method works fine if used outside of model. Any solution to this problem?
EDIT
Rewriting the getRevisionKey() fixes this. Here is the new version
public function getRevisionKey($id){
$sqlQuery = $this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
->from($this->revision_tablea)
->order_by($this->revision_table.'.revisions_number', 'asc')
->limit(1);
$query = $sqlQuery->where($this->revision_tablea.'.id', $id)->get();
if ($query->num_rows() > 0){
return $query->row_array();
}else{
return 0;
}
}
Here is a simple hack that will give you exactly where you are making a mistake.
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
And save it. Before running the function i mean calling
$this->db->get() // Use $this->db->from() instead
use
$query = $this->db->_compile_select()
and echo $query;
These two functions also help in subquery in codeigniter active record.
How can I rewrite this SQL into CodeIgniter's Active Records?

Categories