Pagination in Codeigniter with join - codeigniter

I had my pagination working but then the structure of the database changed so it now doesn’t work because I have to join two tables to get the results I once had…
My db script is:
$query = $this->db->select('*');
$query = $this->db->from('user_entry');
$query = $this->db->join('user_details', 'user_entry.UserID = user_details.id');
$query = $this->db->limit($limit, $offset);
$query = $this->db->get();
If you could help me, I would be so grateful - thanks.

I still don't think there's enough information.. but pagination needs to know the total rows in order to create the links. Assuming this code is in your model and you're calling the model function from your controller in order to set the pagination config variables:
$this->db->select('*');
$this->db->from('user_entry');
$this->db->join('user_details', 'user_details.id = user_entry.UserID');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->num_rows();
in your controller:
$config['base_url'] = 'http://yoururl.com/controller/function';
$config['total_rows'] = $this->model_name->function(); // Model and function to the code above
$config['per_page'] = 10; // # of results you want to display per page
$config['num_links'] = 10; // # of pagination links you want to display
$this->pagination->initialize($config);
in your view where you want the pagination displayed:
<?=$this->pagination->create_links();?>

Related

Laravel merge logic

In my case i need to use collections together. But also i need that model too. I decided to merge eloquents. Like this:
$products = Product::orderBy('created_at', 'desc')->get();
$sliders = Slider::orderBy('order', 'asc')->get();
$news = News::orderBy('order', 'asc')->get();
$collection = new ModelCollection();
$result = $collection->merge($products)->merge($news)->merge($sliders)->sortByDesc('created_at');
dd($result);
$products has 4 data, $slider also has 4 data, $news has 3 data.
But i am allways get 4 data by sorting created_at. Why i can't get 11 data from the collection?
https://laravel.com/docs/5.6/collections#method-merge
in this case there is no limit or rules.
in the interest of brevity i am trying to make an array with my selected models. If its a collection it keeps model. If its just an array also i need to add the model name for each data collections.
array(
Products > product1,product2,product3,product4
News > news1, news2, news3
Slider > slider1, slider2, slider3
)
If you want all collections in one array you can do by this,
$products = Product::orderBy('created_at', 'desc')->get();
$sliders = Slider::orderBy('order', 'asc')->get();
$news = News::orderBy('order', 'asc')->get();
$result = array();
$result['products'] = $products;
$result['sliders'] = $sliders;
$result['news'] = $news;
This will create collection $result like you want,
array(
products > product1,product2,product3,product4
news > news1, news2, news3
sliders > slider1, slider2, slider3
)
Hope this will help you. Comment if any doubts.

Codeigniter, how to pass COUNT result too my view

i have problem with passing data from model to my view. In Database i have Project with a few tasks i need to COUNT it. My problem is: How to pass the results to the view with foreach.
(echo $this->db->count_all_results(); in MODEL show good results)
Project 1: 5 tasks
Project 2: 10 tasks
Project 3: 1 task
MODEL:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
CONTROLLER
function WszystkieProjekty(){
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty();
$arrayid = array();
foreach( $data['moje'] as $row ){
$row->id;
$aaa = $row->id;
$arrayid[] = $this->Todo_model->wyswietl($aaa);
}
$data['tabid'] = $arrayid;
$this->load->view('Projekty.html', $data);
}
MODEL:
function wyswietlWszystkieProjekty(){
$query = $this->db->query("SELECT * FROM todo");
return $query->result();
}
You're passing back to your controller a result set, so all you need to do is iterate over the result set, generating actual rows. It's not too hard, and you've got the right idea.
You have:
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT( id_zadania ) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
return $query->result();
As your model function that is supposed to return the count, right? Easiest way to do this is right from the model to keep your controller from having to do the legwork. When you run the count function in mysql, you'll only get back one row in your case, so using num_rows() isn't going to help because this query will always generate 1 row.
function wyswietl($arrayid){
$query = $this->db->query("SELECT COUNT(id_zadania) AS zlicz
FROM projekty_zadania
WHERE id =$arrayid");
$rs = $query->result();
return $rs[0]['COUNT(id_zaedania)'];
}
In your query, you have counted the data, so i think you can get the total data in the model from $query->rows()->zlics. And in the controller, just pass it to the view like usual
$data['moje'] = $this->Todo_model->wyswietlWszystkieProjekty(); $this->load->view('Projekty.html', $data);

Codeigniter: Join with 'where in' clause

I want to join tables to list books of two categories fiction and romance. How do I do a JOIN with a WHERE IN clause (is that even possible? sorry, noob here)
Here's where I'm stuck at:
$categories = array(10,12);
$query = select()->from('books');
$query->join('genre_map','genre_map.gid IN ('.implode(",", $categories).')');
Jn Table genre_map: bookid, genre_id
Any SQL Query/CodeIgniter help would be much appreciated.
never used codeigniter, but I would say
$categories = array('10', '12');
$this->$db->select('*');
$this->$db->from('books');
$this->$db->join('genre_map','genre_map.bookid = books.bookid');
$this->$db->where_in('genre_map.gid', $categories);
$query = $this->db->get();
$categories = array(10,12);
$query = select()->from('books');
$query->join('genre_map','genre_map.gid IN ('.implode(",", $categories).')');
For codeigniter you can use as:
$categories = array('10', '12');
$this->$db->select('*')->from('books')->join('genre_map','genre_map.bookid = books.bookid')->where_in('genre_map.gid', $categories)->$this->db->get();

get the last row in the db

What is the correct way to retrieve the last row, sorted desc by id, in CodeIgniter?
I've tried select_max but I want the whole row.
function getLastPagination(){
$data = array();
$this->db->query("SELECT id,sort FROM pagination GROUP BY id DESC LIMIT 1");
$query = $this->db->get('pagination');
$data = $query->row_array();
return $data;
}
$data['allpag'] = $this->Model_cats->getLastPagination();
$data['pagin'] = $data['allpag']['sort'];
$per_page = $data['pagin'];
Here I am getting the first value, but I want the last row.
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
Try this.............
function getLastPagination(){
$query ="select * from pagination order by id DESC limit 1";
$res = $this->db->query($query);
if($res->num_rows() > 0) {
return $res->result("array");
}
return array();
}
}
In controller function you need to do following things.......
$allpag = $this->Model_cats->getLastPagination();
$per_page = $allpag[0]['sort'];
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();
Should work?

how to join columns in codeigniter

login(id, name, role,user_id)
teacher(teacher_id)
I need to check role is 2.
$this->db->select('*');
$this->db->from('teacher');
$this->db->where('teacher_branch_id',$this->session->userdata('branch_id'));
$this->db->join('login','teacher.teacher_id = login.login_user_id');
$query = $this->db->get();
Just add a where condition like this: $this->db->where('login.role', 2);
$this->db->select('*');
$this->db->from('teacher');
$this->db->join('login', 'teacher.teacher_id = login.login_user_id');
$this->db->where('teacher_branch_id', $this->session->userdata('branch_id'));
$this->db->where('login.role', 2);
$query = $this->db->get();

Resources