Passing 2 database results into view - php

I am having some trouble passing data from 2 queries into my view.
I have a blog system on which people can comment, but i want to show the blog post at the top of my comments page.
To do this I figured I would just pass the results from a query that retrieves the data for the blog entry.
The problem is that I can't acces the array from the view it seems like.
My code so far.
Controller
public function blog($page){
$this->load->library('pagination');
$this->load->model("model_get");
$config['base_url'] = base_url('main/blog/');
$config['total_rows'] = $this->model_get->getBlogcount();
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data["results"] = $this->model_get->getBlog($config['per_page'], $page, "desc");
$this->load->view("content_blog", $data);
}
function comments($page){
$this->load->library('pagination');
$this->load->model("model_get");
$config['base_url'] = base_url('main/comments/'.$this->uri->segment(3).'/');
$config['total_rows'] = $this->model_get->getCommentcount();
$config['per_page'] = 5;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$data["blog"] = $this->model_get->getBlog(1,6, 'asc');
$data["results"] = $this->model_get->getComments($config['per_page'], $page);
$this->load->view('content_comment', $data);
}
Model
function getBlog($limit, $start, $sort){
$this->db->order_by("id", $sort);
$results = $this->db->limit($limit, $start)->get('blog');
if ($results1)
{
return $results->result();
}else{
return FALSE;
}
}
function getComments($limit, $start){
$this->db->from('comments');
$this->db->where('blog_id', $this->uri->segment(3));
$this->db->limit($limit, $this->uri->segment(4));
$this->db->order_by("id", "desc");
$results = $this->db->get();
if ($results)
{
return $results->result();
}else{
return FALSE;
}
}
View
foreach($results as $row){
$text = $row->text;
echo "<p>". $text ."</p>";
$name = $this->model_get->getUserById($row->author);
echo "<h4>". $name[0]->login ."</h4>";
}

Related

how to works start and limit per page records in codeigniter php

I tried many ways but not working pagination. I have used segment but not working may be in this pagination between data working without pagination perfect.
Controller
class Modelcategory extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Db_model");
$this->load->library("pagination");
}
function index(){
$msg=$this->input->get('msg');
$action=$this->input->get('action');
$resultArr='';
if(empty($action)){
$config = array();
$config['base_url'] = base_url('index.php/modelcategory');
$config['total_rows'] = $this->Db_model->get_count('model_category');
$config["per_page"] = 2;
$config["uri_segment"] = 1;
$this->pagination->initialize($config);
$page = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;
$data["links"] = $this->pagination->create_links();
$data['resultArr']=$this->Db_model->GetAllData('model_category',$config["per_page"], $page);
print_r($data['resultArr']);
}
if($msg == 'success'){
$msg ="Record inserted Successfully!";
}
$data['msg']=$msg;
$data['action']=$action;
$this->load->view('includes/header');
$this->load->view('modelcat',$data);
$this->load->view('includes/footer');
}
}
Model
function GetAllData($Table,$limit = NULL, $start = NULL){ //Insert Data into Database
$this->db->select("*");
$this->db->from($Table);
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0 ){
return $query->result_array();
}else{
return false;
}
}
public function get_count($Table) {
return $this->db->count_all($Table);
}
VIEW
if(!empty($resultArr)){
$count=1;
foreach ($resultArr as $key => $value) {
echo $count++." ".$value['name']."<br>";
}
echo $this->pagination->create_links();
}
Let me know where is actually issue, I am using Codeigniter version 3.1.10
I figure out the issue, solution is in the 'base_url()' and with uri_segment.
Controller function :
function index(){
$msg=$this->input->get('msg');
$action=$this->input->get('action');
$resultArr='';
if(empty($action)){
$config = array();
$config['base_url'] = base_url('index.php/modelcategory/index');
$config['total_rows'] = $this->Db_model->get_count('model_category');
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["links"] = $this->pagination->create_links();
$data['resultArr']=$this->Db_model->GetAllData('model_category',$config["per_page"], $page);
print_r($data['resultArr']);
}
if($msg == 'success'){
$msg ="Record inserted Successfully!";
}
$data['msg']=$msg;
$data['action']=$action;
$this->load->view('includes/header');
$this->load->view('modelcat',$data);
$this->load->view('includes/footer');
}

Codeigniter pagination results shows more than selected per page items

I am new to Codeigniter and seeking some help here.
I am making a test app with pagination. I have selected $config['per_page'] = 1; but instead of showing one listing on first page, it is still showing all of them.
My controller:
public function pagination(){
$keyword = $this->input->post('search[1]');
$main_keyword = $this->search_model->get_city_id_by_input($keyword);
$config['base_url'] = base_url().'search/search-results/';
$config['total_rows'] = $this->search_model->total_number_of_rows($main_keyword);
$config['per_page'] = 1;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $this->search_model->total_number_of_rows($main_keyword);
$config['cur_tag_open'] = ' ';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['results'] = $this->search_model->get_search_results($main_keyword, $config['per_page'], $this->uri->segment(3));
$this->load->view('view',$data);
}
Model:
function get_city_id_by_input($keyword){
$this->db->select('id');
$this->db->from('vbc_city');
$this->db->where('v_city_name', $keyword);
$query = $this->db->get();
$query_result = $query->result_array();
return $query_result();
}
public function total_number_of_rows($main_keyword) {
$this->db->select('vbc_item_id');
$this->db->from('vbc_vacation_item_attri');
$this->db->where('v_item_city', $main_keyword);
$query = $this->db->get();
return $query->num_rows();
}
public function get_search_results($main_keyword) {
$this->db->select('*');
$this->db->from('vbc_vacation_item_attri');
$this->db->where('v_item_city', $main_keyword);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Please help.
compare your call of the function
$this->search_model->get_search_results($main_keyword, $config['per_page'], $this->uri->segment(3));
and signature of
get_search_results($main_keyword)
of course you will get all results.
I suppose you should convert your function to something like this
public function get_search_results($main_keyword, $perpage, $offset) {
$this->db->select('*');
$this->db->from('vbc_vacation_item_attri');
$this->db->where('v_item_city', $main_keyword);
$this->db->limit($perpage, $offset);
$query = $this->db->get();
$result = $query->result();
return $result;
}

How to Give Proper Pagination To Data Table using Pagination library in Codeigniter

I am Working pagination in CodeIgniter. I see tutorials and user manual also. but I can't find the way to do it properly they call the database in Controller for pagination. I want a proper solution.
This is The Controller
public function index() {
$this->load->library('pagination');
$config['base_url'] = 'index.php/Emp';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data = array();
$data['showEmployeeTable']=$this->Em->selectEmployeeData(); //FOR SHOWING A EMPLOYEE DATABASE TABLE
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
//$this->load->view('emp_update', $data);
$this->load->view('erv', $data);
}
This are My models
public function getDesignationData() {
$query = $this->db->get('emp_desig'); //model created for get data
return $query->result();
}
public function getCityRecord() {
$query = $this->db->get('city'); //model created for get data
return $query->result();
}
// ***************** VEDDING PLAN MODELS **********************************************************
public function selectEmployeeData() {
$query = $this->db->get('emp_manage');
return $query;
}
So how can I show Proper Pagination on the View Page? Kindly answer me step by step. I am a newbie in Codeigniter.
And this in the view.
<?php echo $this->pagination->create_links(); ?>
Your controllers should be
public function index() {
$this->load->library('pagination');
$config = array();
$config['base_url'] = 'index.php/Emp';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['showEmployeeTable']=$this->Em->selectEmployeeData($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
$this->load->view('erv', $data);
}
Your model should be
public function selectEmployeeData($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get('emp_manage');
return $query;
}
And in your views add following line
<?php echo $links; ?>
see this code and make some changes as per your requirement:
You controller should be:
function list($offset=0)
{
$num_rows=$this->db->count_all("table_name");
$config['base_url'] = site_url('list');
$config['total_rows'] = $num_rows;
$config['per_page'] = 100;
$config['num_links'] = round($config['total_rows']/$config['per_page']);
//$config['use_page_numbers'] = TRUE;
$config['page_query_string']=TRUE;
$this->pagination->initialize($config);
$data["results"] = $this->Em->selectEmployeeData($config["per_page"], $page);
$this->load->view('viewfile',$data);
}
Your Model :
function selectEmployeeData($limit,$offset)//fetch all data with pagination
{
$data = array();
$this->db->limit($limit, $offset);
$Q = $this->db->get('table');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
In View :
<?php echo $this->pagination->create_links();?>

Error in pagination of codeigniter 1

In model/rci_model.php
public function record_count() {
return $this->db->count_all("produk");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC");
return $query->result();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
In controller/sandal_clarudo.php
function sandals_for_men(){
$data['seo'] = $this->rci_model->tampil_meta(22);
$this->load->view('head', $data);
$config = array();
$config["base_url"] = "http://localhost/clarudo/index.php/sandal_clarudo/sandals_for_men/";
$config["total_rows"] = $this->rci_model->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->rci_model->
fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('men', $data);
$this->load->view('footer');
}
In view/men.php
<?php
foreach($results as $t){
echo "$t->nama_produk";
}
I want to show product that has category = 'Men' only, but using my code it shows all category products. Ideas?
Try This
in cotroller
$count = $this->rci_model->get_count(); //get number of rows
$config['base_url'] = base_url().'index.php/sandal_clarudo/sandals_for_men/';
$config['total_rows'] = $count;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['links'] = $this->pagination->create_links();
$data["results"] = $this->rci_model->fetch_countries($limit, $page);
in model
public function get_count()
{
//count
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men'");
$result = $query->result_array();
$count = count($result);
return $count;
}
//data from table
public function fetch_countries($limit, $page)
{
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
in view
//to show data
<?php
foreach ($results as $new_result)
{
echo $new_result['nama_podak'];
}
?>
//to show pagination
<?php
echo $link//Page segment tabs(you can config boostarp for this)
?>
Try this one. You wrong when using limit.
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC LIMIT $limit, $start");
$this->db->limit($limit, $start);
$this->db->where('id_kategori', 'men');
$this->db->order_by('nama_produk');
$query = $this->db->get("produk");
Hope this work!
for count use :
public function record_count($limit, $start) {
$this->db->from('count(id_produk) AS count');
$this->db->where('id_kategori', 'men');
$this->db->order_by('nama_produk');
$this->db->limit($limit, $start);
$return = $query->row_array();
return $return['count'];
}

How can I add a page?

Using CodeIgniter 2.0.2
I want to set up pagination for my website.
This is what I've in my controller:
$config = array();
$config["base_url"] = base_url() . "/credits";
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->credits_model->fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
Model:
public function record_count() {
return $this->db->count_all("sadmin_credits");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("sadmin_credits");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
I need some help in getting it to work. It shows the numbers but when I click on them it just shows the same results nothing changes.
try this
public function credits($offset='')
{
$total_rows = $this->credits_model->record_count();
$config['base_url'] = base_url() . "/credits";
$config['total_rows'] = $total_rows;
$config['per_page'] = 2;
$this->pagination->cur_page = $offset;
$this->pagination->initialize($config);
$data["results"] = $this->credits_model->fetch_countries($config["per_page"], $offset);
$data["links"] = $this->pagination->create_links();
}
or check this tutorials
http://only4ututorials.blogspot.in/2014/04/pagination-with-codeigniter.html

Categories