I am doing pagination in in my page, but I am getting error like this.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$login_model
Filename: controllers/home.php
Line Number: 41
Fatal error: Call to a member function getDet() on a non-object in
C:\xampp\htdocs\picasso\new_design\photo_frame\application\controllers\home.php
on line 41
This is my code
Model:
function getDet(){
$query_str = "SELECT * FROM frame";
$result = $this->db->query($query_str);
return $result;
}
Controller:
public function frame1()
{
$things = $this->login_model->getDet();
$total_rows = $things->num_rows();
$this->load->library('pagination');
$config['base_url'] = base_url() . 'home/';
$config['total_rows'] = '$total_rows';
$config['per_page'] = '1';
$this->pagination->initialize($config);
$this->view_data['pagination'] = $this->pagination->create_links();
$this->view_data['things'] = $things;
$this->load->model('login_model');
$data1['wood']=$this->login_model->getDetails('wood');
$this->load->view('frame1',$data1);
}
View:
<div>
<?php if($wood!=""||$wood!=null){foreach($wood as $row){ ?>
<div class="frm1" style="border:1px solid rgb(204, 204, 204); background-color: rgb(244, 244, 244);list-style-type:none; float:left; display:inline; padding:20px;">
<img src="<?php echo base_url()."uploads/frame/icon/".$row['iconimage'];?>" style="cursor:pointer" class="changemount" data-id="<?php echo base_url()."uploads/frame/".$row['frameimage'];?>"
data-price="<?php echo $row['price'];?>" data-thick="<?php echo $row['thickness'];?>" data-change="0" data-w="515" data-h="356" data-ch="385" data-cw="554" /></div>
<?php }} ?>
</div>
<?php echo $pagination; ?>
</div>
You have to load the model, before using it.
public function frame1() {
$this->load->model('login_model'); //You have to load this model, before use it
$things = $this->login_model->getDet(); $total_rows = $things->num_rows();
$this->load->library('pagination');
$config['base_url'] = base_url() . 'home/';
$config['total_rows'] = '$total_rows';
$config['per_page'] = '1';
$this->pagination->initialize($config);
$this->view_data['pagination'] = $this->pagination->create_links();
$this->view_data['things'] = $things;
$data1['wood']=$this->login_model->getDetails('wood');
$this->load->view('frame1',$data1);
}
undefined login_model, means you call the model without loading it, add this to your _construct in controller
$this->load->model("model_name");
and also in your view you miss something important, to execute your code in loop do the code below
//add the ->result() in your var $wood
foreach($wood->result() as $row)
this will fix your error
Related
I am having the pagination kept in my codeigniter project. The links are all shown in the view page. But whichever link that I clicked is getting me to the same page...
The following is my controller code..
public function newsletter()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "index.php/welcome/newsletter";
$this->load->model('newsletter_model');
$total_row = $this->newsletter_model->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] = 1;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$this->load->model('newsletter_model');
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->model('newsletter_model');
$data['rm_newsletter'] = $this->newsletter_model->get_rm_newsletter();
$this->load->view('newsletter/newsletter',$data);
}
The following is my view code:
foreach ($lt_newsletter as $letter) {
echo $newsletter['nl_newsletter']; }
<div id="pagination">
<ul class="pagination">
<?php foreach ($links as $link)
{
echo "<li>". $link."</li>";
} ?>
</ul>
</div>
And finally in my model:
public function fetch_data($limit, $id)
{
$this->db->limit($limit);
$this->db->where('nl_id', $id);
$query = $this->db->get("ins_newsletter");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
}
}
public function record_count()
{
return $this->db->count_all("ins_newsletter");
}
public function get_rm_newsletter()
{
$data = $this->db->query('SELECT nl.* FROM ins_newsletter nl ORDER BY nl.nl_id DESC');
return $data->result_array();
}
In my browser all the links(1,2,3,4) are shown clearly, but after I click on every link(1,2,3,4), it is showing the same page with all the records. For presently, I have 4 records in my database and it lists all. Kindly point me where am i going wrong...! Thanks in Advance.
Maybe the error is in your model, I see that you have a
$this->db->where('nl_id', $id);
but in the function in the controller you pass two parameters
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
this are the pagination values, one for quantity of record,(not variable), and the other for the page you are passing (1,2,3,etc.)
So, you have to add another parameter to make the where. See this link in pagination does not work and if you have some other problem can tell me.
How I can change the color and width of table generated by table library in CodeIgniter
Controller
<?php
class Addbalance extends CI_Controller{
function index(){
if($this->session->userdata('logged_in')){
$this->load->library('pagination');
$this->load->library('table');
$config['base_url']='http://localhost/elvan/addbalance';
$config['total_rows'] = $this->db->get('products')->num_rows() - 1;
$config['per_page']=1;
$config['num_links']=10;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$this->pagination->initialize($config);
$data['records']=$this->db->get('products',$config['per_page'],$this->uri->segment(2));
$this->load->model('Addbalance_m');
$this->load->view('addbalance_v',
array('order'=>$this->Addbalance_m->index(),
'provider'=>$this->Addbalance_m->get_provider_data()
,'mydata'=>$data
));
}
else {
redirect('login','refresh');
}
}
}
?>
View
<?php
$this->table->set_heading('id','product name','quanity','yoo','yoo');
echo $this->table->generate($mydata['records']);
echo '<div id="pagination">'.$this->pagination->create_links().'</div>';
?>
I want to use for each if that possible, but I don't know how to fetch the columns from this library in the view
It is done by modifying the Table class template. Documentation Here
To add a particular class to the <table> and another to all <tr>s for example.
$template = array(
'table_open' => "<table class='my-table-class' id='my-fancy-table'>",
'row_start' => "<tr class='my-row-class'>",
);
//apply the above to the table
$this->table->set_template($template);
Here state_list, state3 showing undefined error in form_dropdown but here I give condition, how solve this error, I also give condition both controller and model but same error occurred?
<?php
if(!empty($state_list))
{
$state_list;
}
if(!empty($state3))
{
$state3;
}
$js = 'id="state" class="form-control" onchange="get_city(this.value)" onblur="setval(this.name)"';
echo form_dropdown('state', $state_list, $state3, $js);
?>
load the form helper
$this->load->helper('form');
form helper
<?php
$js = 'id="state" class="form-control" onchange="get_city(this.value)" onblur="setval(this.name)"';
if(!empty($state_list)){
echo form_dropdown('state', $state_list, $state3, $js);
}else{
$state_list = array();
echo form_dropdown('state', $state_list, $state3, $js);
}
?>
Hello I have a problem with my search in CI. In the first page the results are displayed ok, but on second page it shows a 1064 SQL error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND cpt_echip.cpt_echip_nr_inventar LIKE '%%' LIMIT 10 , 10' at line 1
Here is the code (functions that are used):
MODEL:
function search($start, $limit){
$match = $this->input->post('search');
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$q = $this->db->query("SELECT * FROM (cpt_echip)LEFT OUTER JOIN cpt_utl ON cpt_utl.cpt_utl_marca = cpt_echip.cpt_utl_marca WHERE cpt_echip.cpt_tip_echip_id = $tip_echip AND cpt_echip.cpt_echip_nr_inventar LIKE '%$match%' LIMIT $start , $limit");
$rezultat = $q->result();
return $rezultat;
}
function num_filter(){
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$this->db->where('cpt_tip_echip_id', $tip_echip);
$q = $this->db->get('cpt_echip');
$number = $q->num_rows();
return $number;
}
CONTROLLER:
function search(){
$data = $this->helpdesk_model->general();
$start_row = $this->uri->segment(3);
$per_page = 10;
if(trim($start_row) == ""){
$start_row = 0;
}
$config['base_url'] = base_url() . '/index.php/helpdesk/search/';
$config['total_rows'] = $this->helpdesk_model->num_filter();
$config['per_page'] = $per_page;
$config['num_links'] = 10;
$config['first_link'] = 'Prima pagina';
$config['last_link'] = 'Ultima pagina';
$this->pagination->initialize($config);
$data['pagini'] = $this->pagination->create_links();
$data['query'] = $this->helpdesk_model->search($start_row, $per_page);
$this->load->view('rezultat', $data);
}
VIEW:
<table border="1">
<tr>
<th>Nr. Inventar</th>
<th>Nume</th>
<th>Utilizator</th>
</tr>
<?php foreach($query as $row): ?>
<tr>
<td><?php echo anchor('helpdesk/detalii_echipament/'.$row->cpt_echip_nr_inventar, $row->cpt_echip_nr_inventar, array('data-fancybox-type'=>'iframe', 'class'=>'fancybox')); ?></td>
<td><?php echo $row->cpt_echip_nume; ?></td>
<td><?php echo $row->cpt_utl_nume; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $pagini; ?>
Without the search and filter, the pagination works fine.
It means the $_POST array is empty. There will not be anything posted when you navigate using links to the second page. You can pass the search data by url or store in session at first submission and use it.
Just check using print_r($_POST) to see what is there $_POST array.
I am using CI 2.1.0 with HMVC. I am going to implement pagination in view part. But I get 'Undefined table data' error. It displays paging number (1,2,3,..) but does not display records.
My code is :
included library for pagination :
$this->load->library('table');
controller
$data['title'] = "Test Events";
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/myproject/index.php/events/test/';
$config['total_rows'] = $this->eventModule->countRow();
$config['per_page'] = '3';
$this->pagination->initialize($config);
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
$data['main_content'] = 'test';
$this->load->view('event/template', $data);
view
<?php echo $this->table->generate('records'); ?>
<?php echo $this->pagination->create_links(); ?>
Thank you for support.
Finally I get the solution. It's silly mistake at
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
It should be $data instead of $config
$data['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));