简体   繁体   中英

CodeIgniter pagination active link not working

Probably there are lots of similar posts here in StackOverflow, but my scenario is a bit different. So, I decided to post this question using this title.

I'd like to describe my problem in small parts:

Part - 1: I have a normal view page, where I can select specific date. This part is working.

enter image description here

Part - 2: The view page will show some data for the selected date(by default, it's the current date). For example, on 2 November, 2014, there are some data. They will be displayed on load as such:

enter image description here

But, these data are coming from another view page, which I have loaded here using ajax call. I've done so because I want to restrict the displayed data to the selected date only. This part is also working.

Part - 3: This is the main problem description. This is the part which is not working.

To further restrict the amount of data to be displayed, I'm using pagination. The pagination is working fine, except for the active link part. No matter which link I click, the selected link by default is only the first one.

enter image description here

In the above picture, I have selected link 2, but link 1 is shown to be active instead of link 2.

So my view -

        <fieldset>
                <legend id="meal_view_page_legend">Meal</legend>
                <div id="content" class="box">
                    <a href="<?php echo base_url(); ?>index.php/admin_logins/meal2"><button id="button_add">Add Meal</button></a>

                    <br>
                        <br>

                            <div>
                                <?php
                                if (isset($message)) {
                                    if ($message == 'Meal information added successfully' || $message == 'Meal information edited successfully' || $message == 'Meal information deleted successfully') {
                                        ?>
                                        <p class="msg done">
                                            <?php echo $message; ?>
                                        </p>
                                        <?php
                                    } else if ($message == 'Employee ID doesn\'t exist') {
                                        ?>
                                        <p class="msg error">
                                            <?php echo $message; ?> 
                                        </p>
                                        <?php
                                    }
                                }
                                ?>
                            </div>

                            <label style="font-weight: bold">Select Date: </label>

                            <select onchange="ajaxGetInfoByDate()" id="day" name="day">
                                <!--option selected><--?php echo $day; ?></option-->
                                <option <?php if(date("d")=='01'){ ?> selected <?php } ?>>01</option>
                                <option <?php if(date("d")=='02'){ ?> selected <?php } ?> >02</option>
                                <option <?php if(date("d")=='03'){ ?> selected <?php } ?>>03</option>
                                <option <?php if(date("d")=='04'){ ?> selected <?php } ?>>04</option>
                                <option <?php if(date("d")=='05'){ ?> selected <?php } ?>>05</option>
                                <option <?php if(date("d")=='06'){ ?> selected <?php } ?>>06</option>
                                <option <?php if(date("d")=='07'){ ?> selected <?php } ?>>07</option>
                                <option <?php if(date("d")=='08'){ ?> selected <?php } ?>>08</option>
                                <option <?php if(date("d")=='09'){ ?> selected <?php } ?>>09</option>
                                <option <?php if(date("d")=='10'){ ?> selected <?php } ?>>10</option>
                                <option <?php if(date("d")=='11'){ ?> selected <?php } ?>>11</option>
                                <option <?php if(date("d")=='12'){ ?> selected <?php } ?>>12</option>
                                <option <?php if(date("d")=='13'){ ?> selected <?php } ?>>13</option>
                                <option <?php if(date("d")=='14'){ ?> selected <?php } ?>>14</option>
                                <option <?php if(date("d")=='15'){ ?> selected <?php } ?>>15</option>
                                <option <?php if(date("d")=='16'){ ?> selected <?php } ?>>16</option>
                                <option <?php if(date("d")=='17'){ ?> selected <?php } ?> >17</option>
                                <option <?php if(date("d")=='18'){ ?> selected <?php } ?>>18</option>
                                <option <?php if(date("d")=='19'){ ?> selected <?php } ?>>19</option>
                                <option <?php if(date("d")=='20'){ ?> selected <?php } ?>>20</option>
                                <option <?php if(date("d")=='21'){ ?> selected <?php } ?>>21</option>
                                <option <?php if(date("d")=='22'){ ?> selected <?php } ?>>22</option>
                                <option <?php if(date("d")=='23'){ ?> selected <?php } ?>>23</option>
                                <option <?php if(date("d")=='24'){ ?> selected <?php } ?>>24</option>
                                <option <?php if(date("d")=='25'){ ?> selected <?php } ?>>25</option>
                                <option <?php if(date("d")=='26'){ ?> selected <?php } ?>>26</option>
                                <option <?php if(date("d")=='27'){ ?> selected <?php } ?>>27</option>
                                <option <?php if(date("d")=='28'){ ?> selected <?php } ?>>28</option>
                                <option <?php if(date("d")=='29'){ ?> selected <?php } ?>>29</option>
                                <option <?php if(date("d")=='30'){ ?> selected <?php } ?>>30</option>
                                <option <?php if(date("d")=='31'){ ?> selected <?php } ?>>31</option>
                            </select>

                            <select onchange="ajaxGetInfoByDate()" id="month" name="month" >
                                <!--option selected><--?php echo $month; ?></option-->
                                <option <?php if(date("m")=='01'){ ?> selected <?php } ?>>01</option>
                                <option <?php if(date("m")=='02'){ ?> selected <?php } ?>>02</option>
                                <option <?php if(date("m")=='03'){ ?> selected <?php } ?>>03</option>
                                <option <?php if(date("m")=='04'){ ?> selected <?php } ?>>04</option>
                                <option <?php if(date("m")=='05'){ ?> selected <?php } ?>>05</option>
                                <option <?php if(date("m")=='06'){ ?> selected <?php } ?>>06</option>
                                <option <?php if(date("m")=='07'){ ?> selected <?php } ?>>07</option>
                                <option <?php if(date("m")=='08'){ ?> selected <?php } ?>>08</option>
                                <option <?php if(date("m")=='09'){ ?> selected <?php } ?>>09</option>
                               <option <?php if(date("m")=='10'){ ?> selected <?php } ?>>10</option>
                                <option <?php if(date("m")=='11'){ ?> selected <?php } ?>>11</option>
                               <option <?php if(date("m")=='12'){ ?> selected <?php } ?>>12</option>      
                            </select>

                            <select onchange="ajaxGetInfoByDate()" id="year" name="year">
                                <!--option selected><--?php echo $year; ?></option-->
                                <option <?php if(date("y")=='14'){ ?> selected <?php } ?>>2014</option>
                               <option <?php if(date("y")=='15'){ ?> selected <?php } ?>>2015</option>
                               <option <?php if(date("y")=='16'){ ?> selected <?php } ?>>2016</option>
                                <option <?php if(date("y")=='17'){ ?> selected <?php } ?>>2017</option>
                            </select>

                            <div id="indexView"></div>
                            </fieldset>

And another view page, the one that I have loaded inside the above view page using ajax load:

        <fieldset>      

        <table id = "meal_list_table">
            <tr>
                <th scope="col" >Employee Id</th>
                <th scope="col" >Guest?</th>
                <th scope="col" >No. of Guest</th>
                <th scope="col" >Remarks</th>
                <th scope="col" colspan="2" >Action</th>
            </tr>

            <?php foreach ($info as $list) { ?>
                <tr>
                    <td><?php echo $list['emp_id']; ?></td>
                    <td><?php echo $list['is_guest']; ?></td>
                    <td><?php echo $list['num_of_guest']; ?></td>
                    <td><?php echo $list['remarks']; ?></td>
                    <td><a href="<?php echo base_url(); ?>index.php/admin_logins/mealIdGet/<?php echo $list['id']; ?>"><button id="button_edit">Edit</button></a></td>
                    <td>
                        <a href="<?php echo base_url(); ?>index.php/admin_logins/meal4/<?php echo $list['id']; ?>" 
                           onclick="return confirm('Do you want to delete this Meal Information?');">
                            <button id="button_delete">Delete</button>
                        </a>
                    </td>
                </tr>
            <?php } ?>
        </table>
        <p><?php echo $links; ?></p>
</fieldset>

The ajax call(in first view page):

        function ajaxGetInfoByDate() {
            //alert("ok");
            var offset = document.getElementById("offset").value;
            var day = document.getElementById("day").value;
            var month = document.getElementById("month").value;
            var year = document.getElementById("year").value;
            var date = year + '-' + month + '-' + day;
            //alert(offset);

            //   alert(date);

            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("indexView").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "<?php echo base_url(); ?>index.php/admin_logins/ajaxGetInfo?date=" + date + "&&offset=" + offset, true);
            xmlhttp.send();
        }

Controller method to view the first view page:

function meal() {
    $data['message'] = $this->session->flashdata('message');
    $this->load->view('/admin_logins/meal_list', $data);
}

And controller method to load the second view page by ajax call:

    function ajaxGetInfo() {

    $customDate = $this->input->get('date');
    $offset = $this->input->get('offset');
    $query_string = "SELECT m.id, e.emp_id, m.is_guest, m.num_of_guest, m.remarks 
    FROM meal m 
    LEFT JOIN employee e 
    ON e.id = m.emp_id
    WHERE m.entry_date ='$customDate' ";
    $query = $this->db->query($query_string);

    //pagination codes
    $config = array();
    $config['base_url'] = site_url('admin_logins/meal');
    $config['per_page'] = 10;
    $config['uri_segment'] = 3;
    $config['total_rows'] = $query->num_rows();
    $choice = $config['total_rows'] / $config['per_page'];
    $config['num_links'] = round($choice);
    $this->pagination->initialize($config);
    $page = ($offset) ? $offset : 0 ;
    $conf = $config['per_page'];      

    $query_string2 = "SELECT m.id, e.emp_id, m.is_guest, m.num_of_guest, m.remarks 
    FROM meal m 
    LEFT JOIN employee e 
    ON e.id = m.emp_id
    WHERE m.entry_date ='$customDate'
    LIMIT $page, $conf ";

    $query2 = $this->db->query($query_string2);
    $dataByDate['info'] = $query2->result_array();
    //echo "<pre>";
    //print_r($dataByDate['info']);
    //die();
    $dataByDate['links'] = $this->pagination->create_links();
    $this->load->view('admin_logins/ajaxIndex', $dataByDate);
}

What/where is wrong in my code?

您是否检查了$ offset返回什么值,以及它是否正确?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM