简体   繁体   中英

How can i looping calling every 2 row in my database when clicking next button codeigniter php

I try to make my program called every 2 row every time i click next button. where should i put the increment function?. i use codeigniter

so far i only could manage to make this program which is stopped after i clicked the next button only once

This is my controller

    public function form_1(){
    $data = array();
    $limit_per_page = 2;
    $total_records = $this->keur_model->get_total();
    if(isset($_POST['next'])){
        $this->session->set_userdata('counter', 0);
        $start_index = $this->session->userdata('counter') + 2; 
        // die(var_dump($start_index));
    }else{
        $start_index = 0;   
    }        

    $data["listpertanyaan"] = $this->keur_model->form1($limit_per_page, $start_index);
        // $data["links"] = $this->pagination->create_links();

    $this->load->view('form2',$data);
}

And This is my model

    public function form1($limit_per_page, $start_index){
    $this->db->limit($limit_per_page, $start_index);
    $query = $this->db->get("tb_pertanyaan");

    if ($query->num_rows() > 0) 
    {
        foreach ($query->result() as $row) 
        {
            $data[] = $row;
        }    
        return $data;
    }
    return false;
}

and this is my html

 <body> <form method="POST"> <?php $count = 0; foreach ($listpertanyaan->result_array() as $list) { ?> <label><?php echo $list['desk_pertanyaan'] ?></label> <input type="text"></input><br> <?php $count++; if ($count == 2){ break; } } ?> <input type="submit" name="next" value='Next Question'> </form> </body> 

i expect the loop is actually continue based on total rows in the database. the x variable is supposed to be a parameter that know where the next row started because i have a column called no_urut in my database which is a sequential number

Can you use pagination concept, you can fetch the data from the db with limit 2 and the next button will include the limit and offset

$this->db->from('nation');
$this->db->limit($limit, $start);
$query  = $this->db->get();

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