[eluser]ede196620[/eluser]
i am working on a pagination for my website its first time i am trying to make this work the problem is that the pagination dose not turn pages when the number of the link is clicked. i very new to codeigniter and php and cant figure this out i tout this would be a great place to start to get some advice. Here is my code :
Controller
Code: <?php
class Result_controller extends CI_Controller{
function getall(){
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
// print_r($data['query']); die();
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
$config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 10;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();
$data['pagination'] = $this->pagination->create_links();
$this->load->view('result_view', $data);
}
}
?>
model
Code: <?php
class Result_model extends CI_Model{
function result_getall(){
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
//$this->db->limit($perPage, $page);
$query = $this->db->get();
return $query->result();
}
}
?>
and my view
Code: <html>
<head>
<title>Results</title>
</head>
<body>
<center>
<div>
<table border="1">
<tr>
<th>Name</th>
<th>Second Name</th>
<th>Phone</th>
<th>Email</th>
<th>Answer</th>
<th>Comment</th>
</tr>
<?php foreach ($query as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->second_name; ?></td>
<td><?php echo $row->phone; ?></td>
<td><?php echo $row->email; ?></td>
<td> <?php echo $row->answerA;?>
<?php echo $row->answerB;?>
<?php echo $row->answerC;?></td>
<td><?php echo $row->comment;?><br></td>
</tr>
<?php endforeach; ?>
</table>
<?php if (isset($pagination))
{
echo $pagination;
// echo "<pre>"; var_dump($query);
}
?>
</div>
</center>
<?php echo anchor('http://localhost/Surva/index.php/home', 'Home'); ?>
</body>
</html>
[eluser]TheFuzzy0ne[/eluser]
You are not passing your page number into the controller method, and then onto your model, so your model doesn't know what page to get. In fact, it has no concept of pages at the moment. It just returns all results.
[eluser]ede196620[/eluser]
Tnx for posting, can u explain more to me about this i daunt really understand this is all new to me. Maybe u can give me some sort of example that shows how to select pages ?
[eluser]TheFuzzy0ne[/eluser]
Sure, this is untested, and for illustration purposes only, but:
Controller
Code: class Result_controller extends CI_Controller{
function page($page_num=1){
$results_per_page = 25;
$this->load->model('result_model');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/page/';
$config['total_rows'] = $this->result_model->count_all();
$config['per_page'] = $results_per_page;
$config['use_page_numbers'] = TRUE
$config['num_links'] = 10;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['records'] = $this->result_model->get_page($page_num, results_per_page);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('result_view', $data);
}
}
Model:
Code: class Result_model extends CI_Model{
function get_page($page_num=1, $results_per_page=25) {
// Make sure we have a positive page number.
if ($page_num < 1)
{
$page_num = 1;;
}
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
$this->db->limit(($page_num - 1) * $results_per_page, $results_per_page);
$query = $this->db->get();
return $query->result_array();
}
function count_all()
{
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
return $this->db->count_all('tblanswers);
}
}
You might even find it works, but I suggest you clean it up a little. I just wanted to try and clarify how this works.
Hope this helps.
[eluser]ede196620[/eluser]
Hi tnx for the help i used the code but when i changed the parts it returns empty array for me now do u have any suggestions how to fix this or what is wrong ?
[eluser]TheFuzzy0ne[/eluser]
I've meditated for hours over this, but the force was unable to show me your code. Any chance you could just post it in here for me?
Try enabling the profilers with $this->output->enable_profiler(TRUE), and then you can see exactly what query is being fired at the database, and make sure it's right.
[eluser]ede196620[/eluser]
Sorry i forgot to post the code I fixed the code up with the best of my knowledge and the links work fine the pages are turning but instead of returning 2 records on a page like i want it keeps adding 2 records to every page that is turned. i think its something in the controller but i will post everything in case i am wrong
i think its something to do with this
Code: $this->db->limit(($page_num - 1) * $results_per_page, $results_per_page);
but i am not sure about it
controller
Code: <?php
class Result_controller extends CI_Controller{
function getall($page_num=1){
$results_per_page = 1;
$this->load->model('result_model');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
$config['total_rows'] = $this->result_model->count_all();
$config['per_page'] = $results_per_page;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 10;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['query'] = $this->result_model->get_page($page_num, $results_per_page);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('result_view', $data);
}
}
?>
model
Code: <?php
class Result_model extends CI_Model{
function get_page($page_num=1, $results_per_page=25){
if ($page_num < 1)
{
$page_num = 1;;
}
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
$this->db->limit(($page_num - 0) * $results_per_page, $results_per_page);
$query = $this->db->get();
return $query->result();
}
function count_all()
{
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
return $this->db->count_all('tblanswers');
}
}
?>
view
Code: <html>
<head>
<title>Results</title>
</head>
<body>
<center>
<?php echo anchor('http://localhost/Surva/index.php/home', 'Home'); ?>
<div>
<table border="1">
<tr>
<th>Name</th>
<th>Second Name</th>
<th>Phone</th>
<th>Email</th>
<th>Answer</th>
<th>Comment</th>
</tr>
<?php foreach ($query as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->second_name; ?></td>
<td><?php echo $row->phone; ?></td>
<td><?php echo $row->email; ?></td>
<td> <?php echo $row->answerA;?>
<?php echo $row->answerB;?>
<?php echo $row->answerC;?></td>
<td><?php echo $row->comment;?><br></td>
</tr>
<?php endforeach; ?>
</table>
<?php if (isset($pagination))
{
echo $pagination;
echo "<pre>"; var_dump($query);
}
?>
</div>
</center>
</body>
</html>
[eluser]TheFuzzy0ne[/eluser]
Sorry, I got the parameters for $this->db->limit() back-to-front.
That should be:
Code: $this->db->limit($results_per_page, ($page_num - 1) * $results_per_page);
[eluser]ede196620[/eluser]
its no problem tnx it works great codeigniter user guide makes pagination look so simple spent 2 days on this tnx for your help saved me a lot of time
[eluser]Aken[/eluser]
[quote author="ede196620" date="1362744632"]its no problem tnx it works great codeigniter user guide makes pagination look so simple spent 2 days on this tnx for your help saved me a lot of time [/quote]
It is simple. Your problem is unrelated to the Pagination library. All the library does is generate HTML -- it has no impact on how you retrieve records from your DB.
|