2014-11-02 79 views
0

在StackOverflow中可能有很多类似的帖子,但我的情况有点不同。所以,我决定使用这个标题发布这个问题。CodeIgniter分页活动链接不工作

我想描述的小零件我的问题:

部分 - 1: 我有一个正常的视图页面,在那里我可以选择特定的日期。这部分工作。

enter image description here

部分 - 2: 视图页面将显示选定日期的一些数据(默认情况下,它是当前日期)。 例如,2014年11月2日,有一些数据。他们将显示在负载为这样:

enter image description here

但是,这些数据从另一个视图页面,这是我使用Ajax调用加载这里来。我这样做是因为我想将显示的数据限制在选定的日期。这部分也在工作。

Part -3: 这是主要的问题描述。这是不工作的部分。

为了进一步限制要显示的数据量,我正在使用分页。分页工作正常,除了活动链接部分。无论我点击哪个链接,默认选中的链接都只是第一个链接。

enter image description here

在上述画面,我已经选择链接2,但链路1被示出为活动的,而不是链接2.

所以我的视图 -

 <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> 

而另一查看页面,我已经加载上面的查看页面内使用ajax负载:

 <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> 

AJAX调用(在第一视图页):

 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(); 
     } 

控制器方法,以查看所述第一视图页:

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

和控制器的方法通过AJAX调用加载第二视图页:

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); 
} 

我的代码中哪里有错?

回答

0

您是否检查$ offset返回的值以及它是否是正确的值?

+0

我已经检查过$ $ offset值。这是正确的。 – 2014-11-03 10:45:13

+0

将$ config ['base_url'] = site_url更改为base_url – mdgeus 2014-11-06 13:53:04