按对象显示数据库中的所有数据并通过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>

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>

Try this:

<?php foreach($data as $d): ?>
    <a href="#" class="list-group-item"><?php $d->Category_name; ?></a>
<?php endforeach; ?>

So here you are gor response as a standard object. hence the property of standard object will be used with -> operator.

In controller please assign data to view.

$this->load->view('customers/welcome',array('data'=>$data));

In your case

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

If your response is array than you can use below mentioned solution.

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

Let me know if it not works for you.