Search code examples
phpcodeigniterpagination

How to move to next entries according to the recno in codeigniter


I have four buttons first,last,previous and next button if a user clicks on first button then the min no of recno have to be dispalyed and if the user clicks on next then the minof next no entries have to be displayed..my problem is i didn't get any error and i have four buttons..(First,next,prev,last) none of the buttons are working..Help me to move to the next entries according to the recno..Thanks in advance.... This is my controller code:

    $first = $this->db->query('SELECT MIN(recno) AS `first` FROM `daybook` ORDER BY recno DESC LIMIT 1')->row_array();

        $firstID = $this->db->query("SELECT * FROM `daybook`  where recno='$first[first]' ORDER BY recno ASC")->result_array();
        //$firstID = $firstIDQuery->result_array();
         $last = $this->db->query('SELECT MAX(recno) AS `last` FROM `daybook` ORDER BY recno DESC LIMIT 1')->row_array();
        $lastID = $this->db->query("SELECT * FROM `daybook` where recno='$last[last]'  ORDER BY recno")->result_array();
        //$lastID = $lastIDQuery->result_array();

        $id = 1;
        if(!empty($id)){
       $result = $this->db->query("SELECT * FROM daybook WHERE recno = $id")->result_array();
   }
            //$result = $resultQuery->result_array();

        $data['currentID'] = $id;
        $data['firstID'] = $first;
        $data['lastID'] = $last;
        $data['result'] = $result;
        $this->load->view('BookKeeping/DayBookEntry', $data, FALSE);

This is my view page:

 <ul class='pagination'>
        <a href='<?php echo base_url().'/BookKeeping/daybook/'.$firstID; ?>' class='button'>First</a><br>
        <?php
            if($currentID != $firstID){
                foreach ($result as $next_key => $next_value) {
                    if($currentID == $next_value['recno']){
                        $nextID = $result[$next_key]++;
                    }
                }}
        ?>
            <a href='<?php echo base_url().'/BookKeeping/daybook/'.$nextID['recno']; ?>' class='button'>Next</a><br>
        <?php
            if($currentID != $lastID){
                foreach ($result as $prev_key => $prev_value) {
                    if($currentID == $prev_value['recno']){
                        $prevID = $result[$prev_key]--;
                        }
}}
        ?>


            <a href='<?php echo base_url().'/BookKeeping/daybook/'.$prevID['recno']; ?>' class='button'>Previous</a><br>
        <a href='<?php echo base_url().'/BookKeeping/daybook/'.$lastID; ?>' class='button'>Last</a><br>
    </ul>

It's printing correct value in first,last,next,prev i checked it by using print_r(exp)..and my problem is how to display it in link(href links)


Solution

  • Working answer Controller Code:

    $first = $this->db->query('SELECT MIN(recno) AS `first` FROM `daybook` ORDER BY recno DESC LIMIT 1')->row_array();
    
        $firstID = $this->db->query("SELECT * FROM `daybook`  where recno='$first[first]' ORDER BY recno ASC")->result_array();
        //$firstID = $firstIDQuery->result_array();
        $last = $this->db->query('SELECT MAX(recno) AS `last` FROM `daybook` ORDER BY recno DESC LIMIT 1')->row_array();
        $lastID = $this->db->query("SELECT * FROM `daybook` where recno='$last[last]'  ORDER BY recno")->result_array();
        //$lastID = $lastIDQuery->result_array();
    
        //$id = 1;
        if(!empty($id)){
            $result = $this->db->query("SELECT * FROM daybook WHERE recno = $id")->row_array();
        }else{
            $result = "";
        }
    
        $allData = $this->db->query("SELECT * FROM daybook ORDER BY recno ASC")->result_array();
    
            //$result = $resultQuery->result_array();
    
        if(!empty($id)){
            $data['currentID'] = $id;
        }else {
            $data['currentID'] = $first['first'];
        }
        $data['firstID'] = $first;
        $data['lastID'] = $last;
        $data['result'] = $result;
        $data['allData'] = $allData;
    

    View Page:

     <a href='<?php echo base_url().'/BookKeeping/daybook/'.$firstID['first']; ?>' class='button'><i class="icon-first">First</i></a><br><br>
                                    <?php
                                        if($currentID != $lastID['last']){
                                            foreach ($allData as $next_key => $next_value) {
                                                //echo $next_key+1;
                                                //echo '<pre>';print_r($next_value);exit();
                                                if($currentID == $next_value['recno']){
                                                   $nextID = $allData[$next_key+1];
    
                                                }
                                            }
                                            $anchorTagNext = base_url().'/BookKeeping/daybook/'.$nextID['recno'];
                                        }else {
                                            $anchorTagNext = '#';
                                        }
                                    ?>
                                        <a href='<?php echo $anchorTagNext; ?>' class='button'><i class="icon-next">Next</i></a><br><br>
                                    <?php
                                        if($currentID != $firstID['first']){
                                            foreach ($allData as $prev_key => $prev_value) {
                                                //echo '<pre>';print_r($prev_value);exit();
    
                                                if($currentID == $prev_value['recno']){
                                                    //echo $allData[$prev_key-1]['recno'];exit();
                                                   $prevId = $allData[$prev_key-1];
                                                }
                                            }
                                            $anchorTagPrevious = base_url().'/BookKeeping/daybook/'.$prevId['recno'];
                                        } else {
                                            $anchorTagPrevious = '#';
                                        }
                                    ?>
                                    <a href='<?php echo $anchorTagPrevious; ?>' class='button'><i class="icon-previous">Previous</i></a><br><br>
                                    <a href='<?php echo base_url().'/BookKeeping/daybook/'.$lastID['last']; ?>' class='button'><i class="icon-last">Last</i></a>
    </ul>