Welcome Guest, Not a member yet? Register   Sign In
pagination not work
#1

[eluser]dinda[/eluser]
I am a beginner and i currently having problem with paging and the result.
and then the searching is works, but paging is not.

Help me please...

This is my code:

Code:
/* controller : */

class Test extends Controller {
    
    function test()
    {
        parent::Controller();    
        
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model("test_model");
        $this->load->library('pagination');
        $this->load->database();
    }
    
    function index()
    {
        
        /* Parameter Search Field */
        $data['keyword'] = false ;
        $this->test_model->keywordsearch = false;
        
        /*load pagination class */
        $config['uri_segment'] = 4;
        $config['base_url'] = base_url().'index.php/test/index/';
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        $config['per_page'] = '2';
        $config['total_rows'] = $this->db->count_all('ms_user');

        $this->pagination->initialize($config);
        
        /* Model Process */
        $data['queryuser'] = $this->test_model->search_test_data($config['per_page'],$this->uri->segment(3));
        
        
        /* View Passing */
        $this->load->library('table');    
        $this->table->set_heading('ID', 'Name');

        /* Show Output */
         $this->load->view("test_list_v", $data);    
        
    }
    
    function search_test()
    {
        
        /* Parameter Search Field */
        $data['keyword'] = $this->input->post('keyword');
        $this->test_model->keywordsearch = $this->input->post('keyword');
        
        /*load pagination class */
        $config['uri_segment'] = 4;
        $config['base_url'] = base_url().'index.php/test/index/'. $this->uri->segment(3);
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        $config['per_page'] = '2';
        $config['total_rows'] =  $this->db->count_all('ms_user');
        
        $this->pagination->initialize($config);
        
        $offset = (int)$this->uri->segment(4);
        
        /* Model Process */
        $data['queryuser'] = $this->test_model->search_test_data($config['per_page'], $offset);
        

         /* View Passing */
         $this->load->library('table');
         $this->table->set_heading('ID', 'Name');
        
        /* Show Output */
         $this->load->view("test_list_v", $data);    
        
    }
    
    
}

Code:
/* model : */
function search_test_data($num, $offset = false )
{
$this->db->select('userid,username');
$this->db->from('ms_user');      

if ($this->keywordsearch) {
$this->db->like("username", $this->keywordsearch);
}

if ($offset)
{
$this->db->limit($num,$offset);        
}
else
{
$this->db->limit($num);        
}
        
$this->db->order_by('userid');
        
$query = $this->db->get();

return $query;
}

Code:
&lt;!-- View --&gt;
&lt;form name="user_find" id="user_find" method="POST" action="&lt;?=site_url('test/search_test')?&gt;"&gt;
        Cari Nama User :
            &lt;input type='text' name='keyword' id='keyword' value='&lt;?= $keyword; ?&gt;'  /&gt;
            &lt;input type="submit" name="Submit" value="Find"&gt;
&lt;/form&gt;
    
echo $this->table->generate();
echo $this->pagination->create_links();
#2

[eluser]Colin Williams[/eluser]
Ah, hell man, you broke the Pagination Library! And Derek spent all this time writing it, testing it, documenting it... Shame, really.
#3

[eluser]dinda[/eluser]
thanks for the answer, and how can i broked it ?, i'm sorry, but i just beginner, and i wanna learn with CI, can you explaining me, please ?.
Thx.
#4

[eluser]Jeff Logan[/eluser]
Hi, did you get this solved?

I think the problem might be that your base url should read as below:
Code:
$config['base_url'] = base_url().'index.php/test/';

From my model, if the result was an array I used:
Code:
return $query->result();


Also, I have a few questions and suggestions for your code.

In your model, what does $offset = false do? could this be affecting your result, if the above didn't resolve?

I had a similar problem, where a value of 0 was being set as the $num variable - and therefore the pagination class did not display (it was working correctly as the number of results to display was set as zero!).

Try displaying the variable values for the offset, num to see if the values are as expected.


You can also make your code more efficient, by in your controller using $this->uri->segment(3,0));

And then removing part of the if statement to test if $offset is zero. Leaving just $this->db->limit($num,$offset); will be fine as pagination still works with an offset of zero.

Hope the above helps... please note I have just started using codeigniter, so i am still getting to grips with it.
#5

[eluser]dinda[/eluser]
it's not solve yet.

i change with try and error... and put this


Code:
$this->output->enable_profiler(TRUE);

so finally :

i check the result, when i put value on keyword to search : eg "teg"

Code:
// result from $this->db->count_all('ms_user');

SELECT COUNT(*) AS `numrows` FROM `ms_user`

// result from search_test_data()

SELECT `userid`, `username` FROM (`ms_user`) WHERE `username` LIKE '%teg%' ORDER BY `username` LIMIT 2

the result is right, but the pagination is still error, the pagination show number with all data result.

it's must show like <1,2> or nothing , but it's show with whole record in table so it's show as <1,2,3,4>

so i change the count same with like search_test_data(),
Code:
named as get_total() with return $query->num_rows();
so it's better ,

i check the result, when i put value on keyword to search : eg "teg"

Code:
// result from get_total()

SELECT id FROM `ms_user` WHERE `username` LIKE '%teg%'

// result from search_test_data()

SELECT `userid`, `username` FROM (`ms_user`) WHERE `username` LIKE '%teg%' ORDER BY `username` LIMIT 2

and the result is right, and the pagination is right. it's show : <1,2 >.


and the final confusing is : when i click the page 2 the query result like :

Code:
// result from get_total()

SELECT id FROM `ms_user` WHERE `username` LIKE '%%'

* result from search_test_data()

SELECT `userid`, `username` FROM (`ms_user`) WHERE `username` LIKE '%%' ORDER BY `username` LIMIT 2

that's final my report.

until now, i'm still try and error to make it right. thx.

or any body can help me with your sample, please...
#6

[eluser]Pascal Kriete[/eluser]
Ok, so the part that isn't working here is the offset. It's not being added to your page 2 query. Can you try just echoing segment 4 to see what it is.
#7

[eluser]Jeff Logan[/eluser]
I see what the problem might be...

You need to count the number of records found by your query (without the limit function):

SELECT `userid`, `username` FROM (`ms_user`) WHERE `username` LIKE '%teg%' ORDER BY `username`

I made the same mistake by counting all records.

Let me know if that helps.
#8

[eluser]dinda[/eluser]
in my opinion that's caused by gone the $data['keyword'] value,
when 1 press the page 2, the keyword in text input is changes to empty.

and the question is, can it cannot be empty without the user manually remove the value in text input ?
#9

[eluser]Jeff Logan[/eluser]
Is it because when you press the second page, you have not 'submitted' anything from the form (ie by pressing the go button). And therefore there is no post data.

Instead of using post, could you pass the information as part of the URL, to the search function as an ID...

example.com/test/search_test/ID

You could then read the ID by using the URI class or have the function use the variable/value.

Unfortunately I don't know much about the post variables
#10

[eluser]bluepicaso[/eluser]
Would anyone like to help here tooo PLease its kinda urgent....

http://ellislab.com/forums/viewthread/120770/

My keyword search is fetching result but showing all of then on first page...help




Theme © iAndrew 2016 - Forum software by © MyBB