dqb14659 2017-07-14 03:05
浏览 48
已采纳

按对象显示数据库中的所有数据并通过LI显示它

I have a problem in this line, where i manage to get the data in my database but, i have a problem because what i've got is a object. and also i have two questions:

  1. Displaying the Data from my view, i dont know how to display the object type.
  2. From question number 1, i want the object type to be viewed by something like <li> where i have my code in view below

Here is my code:

From Controller i have this:

public function index() {
        $data = $this->Category_model->loadCategories();
        if(empty($data)) {
            $this->load->view('customers/header');
            $this->load->view('customers/welcome');
            $this->load->view('customers/footer');  
        }
        else {
            $this->load->view('customers/header',$data);
            $this->load->view('customers/welcome',$data);
            $this->load->view('customers/footer',$data);
            var_dump($data);
        }
    }

and in view now here is my problem, i manage to var_dump this data and the data is look like this:

array(2) { [0]=> object(stdClass)#21 (2) { ["Category_id"]=> string(1) "1" ["Category_name"]=> string(7) "Gadgets" } [1]=> object(stdClass)#22 (2) { ["Category_id"]=> string(1) "2" ["Category_name"]=> string(5) "Books" } }

so my view is this: how can i determine the size of the object so that i will make the size of the object as how many <a href="#" class="list-group-item">Category 1</a> i will put.

 <div class="list-group">
                    <?php foreach($data) { ?>
                        <a href="#" class="list-group-item"><?php $Category_name ?></a>
                    <?php } ?>

    </div>
  • 写回答

3条回答 默认 最新

  • dongyu1918 2017-07-14 04:20
    关注

    To Display Data to the View, this should be your controller:

    public function index() {
        $data = $this->Category_model->loadCategories();
        if(empty($data)) {
            $this->load->view('customers/header');
            $this->load->view('customers/welcome');
            $this->load->view('customers/footer');  
        }
        else {
            $this->load->view('customers/header',$data);
            $this->load->view('customers/welcome',['data'=>$data]);
            $this->load->view('customers/footer',$data);
            var_dump($data);
        }
    
    
    }
    

    Note: You Should change your view technique. I mean you are loading $this->load->view('customers/header',$data); and $this->load->view('customers/footer',$data); with the welcome page. If you need data only on that welcome page then you can do this.

    $this->load->view('customers/header');
    $this->load->view('customers/welcome',['data'=>$data]);
    $this->load->view('customers/footer');
    

    You Don't need to load data on every view. Load only on that view where you using this $data.

    Now On the View Side to display this:

    array(2) 
    { 
        [0]=> object(stdClass)#21 (2) 
            { 
                ["Category_id"]=> string(1) "1" ["Category_name"]=> string(7) "Gadgets" 
            } 
        [1]=> object(stdClass)#22 (2) 
            { 
                ["Category_id"]=> string(1) "2" ["Category_name"]=> string(5) "Books" 
            } 
    }
    
    
    <div class="list-group">
        <?php foreach($data as $single_data) { ?>
            <a href="#" class="list-group-item"><?= $single_data->Category_name ?></a>
        <?php } ?>
    </div>
    

    And As your second problem:

    <ul>
        <?php foreach($data as $single_data) { ?>
            <li><?= $single_data->Category_name ?></li>
        <?php } ?>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题