Welcome Guest, Not a member yet? Register   Sign In
Pagination ajax help
#1

[eluser]debow[/eluser]
I'm trying to display photos from a selected photo album and I'm having issues getting the pagination working. On one page/view I have listed all the photo albums created. Each has a link that has the id tied to it that takes you into the page/view of photos associated to that album. The photos will display based off the per_page I choose and so do the pages links but when I choose page 2 page 3 etc nothing happens. I'm not sure if its the call back to the ajax function since the id is somewhat dynamic or what the issue is. Below is the code.


[controller] When I click on the album I want to view it passes the id to this function
Code:
function gallery_view_test($id){
        
        $data['gid'] = $id;

        $this->load->view('includes/header', $this->data);
        $data['main_content'] = 'gallery/test/gallery_view_test';
        $this->load->view('includes/template', $data);
        //$this->ajax_get_gallery_full($gid);
        
    }
    
    function ajax_get_gallery_full_test($id = null){
        
        $id = $_POST['id'];
        $data['image'] = base_url() . 'uploads/';
        
        $start_row = $this->uri->segment(4);
        $per_page = 1;
        
        if (trim($start_row) == '') {
            $start_row = 0;
        }
        
        $data['rows'] = $this->gallery_tbl->viewCategories_gallery_test($id);//Gets the description for the gallery tbl=gallery_categories
        
        $data['query'] = $this->gallery_tbl->test($id);//Gets the data from the gallery tbl=gallery_assets
        $total_rows= $data['query']->num_rows();
        
        $this->load->library('pagination');
        $config['base_url'] = base_url() . 'test/ajax_get_gallery_full_test/';
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $per_page;
        $config['full_tag_open'] = '<p class="gallery_pagination">';
        $config['full_tag_close'] = '</p>';

        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();
        
        $data['query'] = $this->gallery_tbl->test_limited($start_row,$per_page,$id);//Gets the data from the gallery tbl=gallery_assets
        
        $this->session->set_userdata('table_start_row', $start_row);
        
        $_html = $this->load->view('gallery/test/gallery_full_table_test', $data, TRUE);
        echo $_html;
    }

[model]
Code:
function test($id){
        
        $query_str = "SELECT * from gallery_assets where cat_id = $id";
        $result = $this->db->query($query_str);

        return $result;
        
    }
    
    function test_limited($start_row,$limit,$id){
        
        echo $start_row; echo " start row"; echo "<br/>";
          echo $limit; echo " limit"; echo "<br/>";
          echo $id; echo " id"; echo "<br/>";
          
        $query_str = "SELECT * from gallery_assets where cat_id = $id limit $start_row, $limit";
        $result = $this->db->query($query_str);

        return $result;
        
    }

[view] This is the main view showing only the important code
Code:
<div id="gallery_table"></div>&lt;!-- Ajax Table--&gt;

[removed]
    $(document).ready(function(){
    
        var gid = $('#gid').val();
  
         $.post("&lt;?php echo base_url(); ?&gt;test/ajax_get_gallery_full_test/&lt;?php echo $this->session->userdata('table_start_row');?&gt;",{'id':&lt;?php echo $gid;?&gt;}, function(data) {
            $("div#gallery_table").html(data);        
        });
      
                      
        $("p.gallery_pagination a").live("click", function(){      
            var this_url = $(this).attr("href");    
            $.post(this_url, { }, function(data) {        
                $("div#gallery_table").html(data);        
            });
            return false;
        });
    
    });
[removed]


[view] This is where the table is created
Code:
<div class="header-title">
                      &lt;!--&lt;?php echo $test;?&gt;--&gt;
                    
                   &lt;?php foreach ($rows as $r): ?&gt;
                        <span class="page-desc">&lt;?php echo $r->description; ?&gt;</span>
                   &lt;?php endforeach; ?&gt;
                  

                </div>  


                <div id="gallery">
                    <ul class="portfolio">

                        &lt;?php foreach ($query->result() as $r): ?&gt;
                            <li class="">
                                <a class="image-wrap">filename;?&gt;" rel="prettyPhoto[gallery]" title="&lt;?php echo $r->caption; ?&gt;"><img width="300" height="300">filename; ?&gt;" class="attachment-portfolio-post-thumbnail wp-post-image" alt="&lt;?php echo $r->caption ?&gt;" title="&lt;?php echo $r->caption ?&gt;" /><span class="zoom-icon"></span></a>
                                <div class="folio-desc">
                                    <p class="excerpt">&lt;?php echo $r->caption; ?&gt; </p>


                                </div>
                            </li>

                        &lt;?php endforeach; ?&gt;

                    </ul>
                    <div class="clear"></div>
                </div>


                <div id="wp-pagenavi" align='center'>
                    &lt;?php if (strlen($pagination)): ?&gt;
                        Pages:&lt;?php echo $pagination ?&gt;
                    &lt;?php endif ?&gt;&lt;!-- Page navigation --&gt;

                    </tr>
                </div>
#2

[eluser]jvicab[/eluser]
check on error console on firefox to see if you have some javascript error. I usually check first if the ajax function works testing if it return any value putting alert(data) inside return function. Once I get what I am suppossed to get, I move on forward to check the rest of the code.
#3

[eluser]debow[/eluser]
I'm not seeing a script error but did see this after installing javascript. This is shown after I click on the page 2 link.

Code:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  id</p>
<p>Filename: controllers/test.php</p>
<p>Line Number: 36</p>

</div>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Database Error&lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;
    <div id="content">
        <h1>A Database Error Occurred</h1>
        <p>Error Number: 1064</p><p>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 '' at line 1</p><p>SELECT * from gallery_assets where cat_id = </p><p>Filename: C:\wamp\www\dspinc\system\database\DB_driver.php</p><p>Line Number: 330</p>    </div>
&lt;/body&gt;
&lt;/html&gt;
#4

[eluser]pickupman[/eluser]
You are setting $id to be the 3rd uri segment by putting into the function ajax_get_gallery_full_test($id = null). Then you are using the 3rd segment for the pagination offset. Try remove $id=null from the function to see what happens.
Code:
function ajax_get_gallery_full_test(){
        
        $id = $this->input->post('id');
        $data['image'] = base_url() . 'uploads/';
        
        $start_row = $this->uri->segment(4, 0); //second parameter is default if none is passed
        $per_page = 1;
}
#5

[eluser]debow[/eluser]
So I tried what you suggested, no errors but no luck. Then I looked at the error again and realized that the issue is when clicking on the next page link the "id" is lost it doesn't know what to select from the table. So I tried this in the pagination section and it worked.

Before
Code:
$("p.gallery_pagination a").live("click", function(){      
            var this_url = $(this).attr("href");    
            $.post(this_url, {}, function(data) {        
                $("div#gallery_table").html(data);        
            });
            return false;
        });

After
Code:
$("p.gallery_pagination a").live("click", function(){      
            var this_url = $(this).attr("href");    
            var gid = $('#gid').val();
            $.post(this_url, {'id':&lt;?php echo $gid; ?&gt; }, function(data) {        
                $("div#gallery_table").html(data);        
            });
            return false;
        });

Thanks for the help.




Theme © iAndrew 2016 - Forum software by © MyBB