Welcome Guest, Not a member yet? Register   Sign In
header white Data
#1

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; ?>
Reply
#2

What error are you getting you did not show one?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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
Reply
#4

(This post was last modified: 10-14-2017, 07:52 AM by PaulD.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB