mysql - 查询数据库并显示

标签 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;
}

Controller :

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

分页并不重要。

和 View :

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

最佳答案

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

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

关于mysql - 查询数据库并显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13879765/

相关文章:

php - 打印超过 1 个用户

mysql - SQL获取指定用户获胜的民意调查

php - MySql 喜欢不工作

php - 带有限制和偏移量的 CodeIgniter sql 查询不显示结果

php - 将 "$message"放入 codeigniter 的表内

CodeIgniter "Unable to load the requested class: iofactory"中的 PHPExcel 错误

php - 使用 if 语句显示 JOIN 的多个结果

MySQL - 如何转向 NVP?

IN where 语句中的 mysql 用户变量

ios - coreData executeFetchRequest 方法的竞争条件导致 nil 数据的问题