doumao1917 2018-09-26 09:24
浏览 77
已采纳

codeigniter中的动态数据

I am new in codeigniter and i am working on ecommerce template. By default whenever index() has been hit, it's showing all products in different sections (in html) and I want to make this dynamic so should I use queries in index() for get different type of records or there is any other way? This is my code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
    public function index()
    {
        //echo "hello world";
        $this->load->view('index');
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dto52236 2018-09-26 10:09
    关注

    First need to create database and table as per your requirement. Then you can create model for reference check this link : https://www.codeigniter.com/userguide3/general/models.html

    <?php 
    class Blog_model extends CI_Model {
    
            public function get_data_first()
            {
                    $query = $this->db->get('entries', 10);
                    return $query->result();
            }
    
            public function get_data_second()
            {
                   $query = $this->db->get('entries', 10);
                    return $query->result();
            }
    }
    ?>
    

    After making model go to your controller and include it like:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class Home extends CI_Controller {
        public function index()
        {
            $this->load->model('my_model');
    
            $data['first_list'] = $this->my_model->get_data_first();
            $data['second_list'] = $this->my_model->get_data_second();
            //echo "hello world";
            $this->load->view('index', $data);
        }
    }
    ?>
    

    Then use you param in index file like :

    <html>
    <head></head>
    <body>
    <?php
    if($first_list){
     foreach($first_list as $each){
        echo $each->my_param;
     }
    }
    ?>
    <?php
    if($second_list){
     foreach($second_list as $each){
        echo $each->my_param2;
     }
    }
    ?>
    </body>
    </html>
    

    I hope this helpful for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题