1

我使用CodeIgniter 2.1.3和。PHPMySQL

您好,我想显示数据库中的数据。我总是按 显示foreach($results as $data),但现在我想分几步显示所有数据。显示第一条记录,当用户单击时next显示数据库中的下一行。我现在必须使用 mysql_fetch_row() 但我不知道该怎么做......这是我的模型:

public function play($limit, $start) {
    $this->db->limit($limit, $start);
    $query = $this->db->get("quiz");
if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
}

控制器:

public function index()
{
    $config = array();
    $config["base_url"] = base_url() . "index.php/main_menu/Quiz/index";
    $config["total_rows"] = $this->quiz_model->record_count();
    $config["per_page"] = 11;
    $config["uri_segment"] = 4;

    $this->pagination->initialize($config);
    $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["results"] = $this->quiz_model->play($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();
    $this->load->view('left_column/quiz_open', $data);
}

分页并不重要。

并查看:

<form>
    <?php
    if (empty($results)) {

    }
    else {
    foreach($results as $data) { ?>

        <label style='width:450px;'> <b>  &nbsp <?php echo $data->pytanie?> </b> </label>
        <label style='width:300px;'> <input type="radio" name="Wiek" value=<?php echo $data->odp1 ?> /> <?php echo $data->odp1 ?> </label>
        <label style='width:300px;'> <input type="radio" name="Wiek" value=<?php echo $data->odp2 ?> /> <?php echo $data->odp2 ?> </label>
        <label style='width:300px;'> <input type="radio" name="Wiek" value=<?php echo $data->odp3 ?> /> <?php echo $data->odp3 ?> </label> 


        <?php }
        }?>
        <label style='width:300px;'>    <input type="submit" name="Wyslij" id="Wyslij" value="&nbsp Wyślij &nbsp"/> </label> 
        </form>
4

1 回答 1

0

codeIgniter 提供了一个内置的分页类。您可以在用户指南中找到它。

在函数中定义一个起始索引变量,您希望在其中使用分页为零。

public function pagination($start_index = 0)
{

 $result = $this->model->get_results($data); //this $data is the argument which you are passing to your model function. If you are using database to get results array.

 $items_per_page = 10;   //this is constant variable which you need to define

 $filtered_result = array_splice($result, $start_index, ITEM_PER_PAGE_USERS);

 $model['$filtered_result'] = $filtered_result;

 $total_rows = count($result);

 $model['page_links'] = create_page_links (base_url()."/controlelr_name/pagination",ITEM_PER_PAGE_USERS, $total_rows);

 $this->load->view('controller_name/view_file_name', $model);
}

这是分页的通用功能。您可以将此函数保存在一个帮助文件中,您可以在其中保存所有通用函数。

function create_page_links($base_url, $per_page, $total_rows)
{

 $CI = & get_instance();
 $CI->load->library('pagination');

 $config['base_url'] = $base_url;
 $config['total_rows'] = $total_rows;
 $config['per_page'] = $per_page; 

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

 return $CI->pagination->create_links();
}

此创建页面链接功能是一个通用功能......更多解释请查看用户指南中的分页类......

于 2012-12-14T14:18:33.590 回答