查询数据库并显示

时间:2012-12-14 13:38:31

标签: mysql codeigniter fetch

我使用CodeIgniter 2.1.3PHPMySQL

您好,我想显示数据库中的数据。我始终按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;
}

CONTROLER:

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>

1 个答案:

答案 0 :(得分:0)

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

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

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

这个创建页面链接功能是一个通用函数.............更多解释从用户指南检查分页类......