CodeIgniter Forums
header white Data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Regional User Groups (https://forum.codeigniter.com/forumdisplay.php?fid=25)
+--- Thread: header white Data (/showthread.php?tid=69147)



header white Data - Germanikus - 10-13-2017

Hello everybody,
I wanted to include a loop in my template header.php.
Unfortunately I get with the foreach an error message.
What have I done wrong?

Controller:
PHP Code:
<?php
    
class Warenkorb extends CI_Controller
        
{
            function 
__construct()
                {
                    
parent::__construct();
                }

            public function 
header()
                {
                    
$data['header_warenkorbs'] = $this->Warenkorb_model->header_warenkorbs();
                    
$this->load->view('templates/header'$data);
                    
// $this->load->view('test/index');
                    // $this->load->view('templates/footer');
                
}
        } 

Model:
PHP Code:
<?php
    class Warenkorb_model 
extends CI_Model
        
{
            public function 
header_warenkorbs()
                {
                    
$this->db->select('*');
                    
$this->db->from('db_artikel');
                    
$query $this->db->get();
                    return 
$query->result_array();
                }
        } 

header.php
Code:
<?php foreach ($header_warenkorbs as $warenkorb): ?>
                                <li>
                                    <span class="item">
                                        <span class="item-left">
                                            <span class="item-info">
                                                <span>Item name</span>
                                                <span>23$</span>
                                            </span>
                                        </span>
                                        <span class="item-right">
                                            <button class="btn btn-xs btn-danger pull-right">x</button>
                                        </span>
                                    </span>
                                </li>
<?php endforeach; ?>



RE: header white Data - InsiteFX - 10-13-2017

What error are you getting you did not show one?


RE: header white Data - Germanikus - 10-13-2017

Sory forgettet

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined variable: header_warenkorbs
Filename: templates/header.php
Line Number: 61
Backtrace:
File: H:\xampp\htdocs\newKARTENLISTE\test2\application\views\templates\header.php
Line: 61
Function: _error_handler
File: H:\xampp\htdocs\newKARTENLISTE\test2\application\controllers\Pages.php
Line: 8
Function: view
File: H:\xampp\htdocs\newKARTENLISTE\test2\index.php
Line: 315
Function: require_once



RE: header white Data - PaulD - 10-14-2017

You should always check a foreach variable is not empty before doing the foreach loop. The error is telling you your variable is not set. If it is not set, but you are trying to set it with your model call, then your model must not be returning the values you expected.

Looking at your models called function, which looks fine, are you sure you have any data in the table you refer to?

PS You could write it like this btw:
PHP Code:
public function header_warenkorbs()
{
      return 
$this->db->select('*')
            ->
from('db_artikel')
            ->
get()
            ->
result_array();


And since select * is the default, and get accepts a table name, you could do:

PHP Code:
public function header_warenkorbs()
{
      return 
$this->db->get('db_artikel')->result_array();


Paul