根据mysql php的请求加载数据

时间:2012-12-15 00:35:17

标签: php mysql ajax load

我需要通过限制请求数据,每个请求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>"; }?>

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

  

LIMIT需要一个或两个数字参数,这些参数必须都是   非负整数常量(使用预准备语句时除外)。

     

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