1

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>
Jc John
  • 1,799
  • 2
  • 34
  • 69

3 Answers3

1

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>
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
  • hello sir why is this error ?Undefined variable: data – Jc John Jul 14 '17 at 05:34
  • Make Sure Your loading the correct view and loading the correct data on it i mean please add your_view in this code `$this->load->view ( 'your_view', ['data'=>$data] );` – always-a-learner Jul 14 '17 at 05:36
  • if i add this code $this->load->view ( 'your_view', ['data'=>$data] ); i will have a duplicate view it destroys my view. because my view is in $this->load->view('customers/welcome',$data); – Jc John Jul 14 '17 at 05:38
  • like if you have `category.php` page inside the `view` directory of your CodeIgniter setup then it should be `$this->load->view ( ''category.php", ['data'=>$data] );` – always-a-learner Jul 14 '17 at 05:38
  • Ok then TRY this `$this->load->view('customers/welcome',['data'=>$data]);` – always-a-learner Jul 14 '17 at 05:40
  • I have updated my answer (added the Note), may it can improve your website performance. @JcJohn – always-a-learner Jul 14 '17 at 05:46
  • 1
    @ankitsuthar "You don't need to load data in every view". It is good to load variables/data just once. And for that best way would be, before loading view files, to put `$this->load->vars($data)` and then just load view files without data arrays. That way you would have access to your data in every view file. [Docs](https://www.codeigniter.com/user_guide/libraries/loader.html#CI_Loader::vars). – Tpojka Jul 14 '17 at 07:00
  • @Tpojka thanks for the suggestion. I will sure look into this. Thanks. – always-a-learner Jul 14 '17 at 07:02
  • 1
    @ankitsuthar Np. I am using `$this->load->vars()` method a lot because sometimes particular view file is not loaded in order you want. For example if you must pass some data to JS body file is being loaded before header where your JS initialization is etc. This way all variables will be available in view file of choice. – Tpojka Jul 14 '17 at 07:05
0

Try this:

<?php foreach($data as $d): ?>
    <a href="#" class="list-group-item"><?php $d->Category_name; ?></a>
<?php endforeach; ?>
  • hello sir it says two errors Invalid argument supplied for foreach() and Undefined variable: data – Jc John Jul 14 '17 at 05:36
  • Make sure the variable $data on controller is passed to your view when you render it. See this: https://stackoverflow.com/questions/12294527/passing-variable-from-controller-to-view-in-codeigniter – Jerick Allan Sernal Dimaano Jul 14 '17 at 08:42
0

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.

Alex Mac
  • 2,970
  • 1
  • 22
  • 39