CodeIgniter Forums
[Solved] Foreach loop Not Working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: [Solved] Foreach loop Not Working (/thread-65374.html)



[Solved] Foreach loop Not Working - wolfgang1983 - 06-04-2016

I have my two arrays but cannot get them to work in my foreach loop

I get error

A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'href'
Filename: common/Header.php
Line Number: 28

How to make i work properly



PHP Code:
<?php

class Header extends MX_Controller {

    public function 
__construct() {
        
parent::__construct();
    }

    public function 
index() {
        
$data['csslinks'] = array();

        
// Bootstrap
        
$csslinks = array(
     
       'href'  => 'app/modules/admin/plugins/bootstrap/dist/css/bootstrap.css',
     
       'rel'   => 'stylesheet',
     
       'type'  => 'text/css',
        );

        
// Custom
        
$csslinks = array(
     
       'href'  => 'app/modules/admin/stylesheets/custom.css',
     
       'rel'   => 'stylesheet',
     
       'type'  => 'text/css',
        );

        foreach (
$csslinks as $csslink) {
            
$data['csslinks'][] = array(
                
'href' => $csslink['href'], // Line 28
                
'rel' => $csslink['rel'],
                
'type' => $csslink['type']
            );
        }

        return 
$this->load->view('common/header_view'$data);
    }



View


Code:
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <base href="<?php echo base_url();?>" />
    <?php foreach ($csslinks as $csslink) {?>
        <link rel="<?php echo $csslink['rel'];?>" type="<?php echo $csslink['type'];?>" href="<?php echo $csslink['href'];?>">
    <?php } ?>
</head>
<body>



RE: Foreach loop Not Working - pdthinh - 06-04-2016

You can try this:
PHP Code:
<?php

 
public function index() {

 
// Bootstrap
 
$csslinks[] = array( // add new element to $csslinks array
 
       'href'  => 'app/modules/admin/plugins/bootstrap/dist/css/bootstrap.css',
 
       'rel'   => 'stylesheet',
 
       'type'  => 'text/css',
 );

 
// Custom
 
$csslinks[] = array( // add another element to $csslinks array
 
       'href'  => 'app/modules/admin/stylesheets/custom.css',
 
       'rel'   => 'stylesheet',
 
       'type'  => 'text/css',
 );

 
$data['csslinks'] = $csslinks;

 return 
$this->load->view('common/header_view'$data);
 } 



RE: Foreach loop Not Working - wolfgang1983 - 06-04-2016

(06-04-2016, 04:26 AM)pdthinh Wrote: You can try this:
PHP Code:
<?php

 
public function index() {

 
// Bootstrap
 
$csslinks[] = array( // add new element to $csslinks array
 
       'href'  => 'app/modules/admin/plugins/bootstrap/dist/css/bootstrap.css',
 
       'rel'   => 'stylesheet',
 
       'type'  => 'text/css',
 );

 
// Custom
 
$csslinks[] = array( // add another element to $csslinks array
 
       'href'  => 'app/modules/admin/stylesheets/custom.css',
 
       'rel'   => 'stylesheet',
 
       'type'  => 'text/css',
 );

 
$data['csslinks'] = $csslinks;

 return 
$this->load->view('common/header_view'$data);
 } 


Worked