0

我需要按每个请求 50 行的限制请求数据并在搜索中找到。

这是我从 mysql 获取数据的函数:

    function index()
   {
      $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');


      $this->load->library("pagination");

        $config = array();
        $config["base_url"] = base_url() . "products/index";
        $config["total_rows"] = $this->products_model->products_count();
        $config["per_page"] = ROWS_PER_PAGE;
        $config['num_links'] = 4;
        $config["uri_segment"] = 3;
        //$config['use_page_numbers'] = TRUE;
        $config['full_tag_open'] = '<div class="pagerSC">';
        $config['full_tag_close'] = '<div style="clear: left;"></div></div>';
        $config['cur_tag_open'] = '<span class="currentSC">';
        $config['cur_tag_close'] = '</span>';
        $config['prev_link'] = '&lt; Previous';
        $config['next_link'] = 'Next &gt;';

        $config['first_link'] = '&lt;&lt; First';
        $config['last_link'] = 'Last &gt;&gt;';

        $config['num_tag_open'] = '';
        $config['num_tag_close'] = '';
        $config['next_tag_open'] = '';
        $config['next_tag_close'] = '';
        $config['first_tag_close'] = '';
        $config['last_tag_open'] = '';

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



        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $prods = $this->products_model->fetch_products($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();

        //$this->load->view("example1", $data);

      //$products = $this->products_model->getAllProducts();
      $warehouses = $this->products_model->getAllWarehouses();
      $warehouse = DEFAULT_WAREHOUSE;

      //if(!empty($products)) {
      foreach($prods as $product) {
      $id[] = $product->id;   
      $image[] = $product->image;   
      $code[] = $product->code;
      $name[] = $product->name;
      $size[] = $product->size;
      $cost[] = $product->cost;
      $price[] = $product->price;
      $qt = 0;
      foreach($warehouses as $house) {
          $warehouse_product = $this->products_model->getProductQuantity($product->id, $house->id);

                $qt += $warehouse_product['quantity'];

      }
      $quantity[] = $qt;
      $unit[] = $product->unit;
      $alert_quantity[] = $product->alert_quantity;
      }

      $keys = array("id","image","code","name","quantity","unit","size","cost","price","alert_quantity");

      $final = array();
      foreach ( array_map(null, $id, $image, $code, $name, $quantity, $unit, $size, $cost, $price, $alert_quantity) as $key => $value ) {
            $final[] = array_combine($keys, $value);
      }

      $data['rows'] = $final;
     // }
      $data['warehouses'] = $warehouses;
      $meta['page_title'] = $this->lang->line("products");
      $data['page_title'] = $this->lang->line("products");
      $this->load->view('commons/header', $meta);
      $this->load->view('content', $data);
      $this->load->view('commons/footer');
   }

这是我的带有数据结果的 php,但都是来自 mysql 的数据,我需要像分页函数一样按需加载:

<table id="fileData" cellpadding=0 cellspacing=10 width="100%">
        <thead>
    <tr>
            <th style="width:45px;"><?php echo $this->lang->line("image"); ?></th>
            <th><?php echo $this->lang->line("product_code"); ?></th>
        <th><?php echo $this->lang->line("product_name"); ?></th>
        <th><?php echo $this->lang->line("quantity"); ?></th>
            <th><?php echo $this->lang->line("product_unit"); ?></th>
            <th><?php echo $this->lang->line("product_size"); ?></th>
        <th><?php echo $this->lang->line("product_cost"); ?></th>
         <th><?php echo $this->lang->line("product_price"); ?></th>
        <!-- <th><?php echo $this->lang->line("alert_quantity"); ?></th> -->
        <th style="width:45px;"><?php echo $this->lang->line("actions"); ?></th>
        </tr>
    </thead>
        <tbody>
        <?php foreach ($rows as $row):?>
            <tr>
            <td style="text-align:center;">
                <?php echo '<a class="ajax" href="uploads/' . $row['image'] .'"><img src="uploads/' . $row['image'] . '" alt="' . $row['image'] . '" width="24" height="24" /></a>'; ?></td>
                <td><?php echo $row['code']; ?></td>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['quantity']; ?></td>
            <td><?php echo $row['unit']; ?></td>
            <td><?php echo $row['size']; ?></td>
            <td><?php echo $row['cost']; ?></td>
            <td><?php echo $row['price']; ?></td>                
            </tr>
        <?php endforeach;?>
    </tbody>
    </table>
    <?php if($links) { echo $links; } else { echo "<div class=\"pagerSC\"></div>"; }?>

任何帮助表示赞赏。

4

2 回答 2

0

由于您使用的是 MySQL,因此您可以使用LIMIT 子句来获取结果。如链接页面所述:

LIMIT 接受一个或两个数字参数,它们都必须是非负整数常量(使用准备好的语句时除外)。

有两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为 0(不是 1):

于 2012-12-15T17:11:17.177 回答
0

如果您需要获取 50 条记录,只需添加LIMIT 50到您的 SQL 查询的末尾(我在上面的代码中没有看到它)

于 2012-12-15T00:41:46.790 回答