CodeIgniter Forums

Full Version: Invalid argument supplied for foreach() Filename: views/navigation.php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mehdy[/eluser]
my getSubCategories function in mcats
function getCategoriesNav(){
$data = array();
$this->db->select('id,name,parentid');
$this->db->where('status', 'active');
$this->db->orderby('parentid','asc');
$this->db->orderby('name','asc');
$this->db->groupby('parentid,id');
$Q = $this->db->get('categories');
if ($Q->num_rows() > 0){
foreach ($Q->result() as $row){
if ($row->parentid > 0){
$data[0][$row->parentid]['children'][$row->id] = $row->name;

}else{
$data[0][$row->id]['name'] = $row->name;
}
}
}
$Q->free_result();
return $data;
}



my view/navigation

if (count($navlist)){
echo "<ul>";
foreach ($navlist as $key => $list){
foreach ($list as $topkey => $toplist){
echo "<li class='cat'>";
echo anchor("welcome/cat/$topkey",$toplist['name']);
echo "</li>\n";
if (count($toplist['children'])){
foreach ($toplist['children'] as $subkey => $subname){
echo "\n<li class='subcat'>";
echo anchor("welcome/cat/$subkey",$subname);
echo "</li>";
}
}
}
}
echo "</ul>\n";
}

my welcome/cat

function cat($id){
//$this->benchmark->mark('query_start');
$cat = $this->MCats->getCategory($id);
//$this->benchmark->mark('query_end');
if (!count($cat)){
redirect('welcome/index','refresh');
}
$data['title'] = "Claudia's Kids | ". $cat['name'];

if ($cat['parentid'] < 1){
//show other categories
$data['listing'] = $this->MCats->getSubCategories($id);
$data['level'] = 1;
}else{
//show products
$data['listing'] = $this->MProducts->getProductsByCategory($id);
$data['level'] = 2;

}
$data['category'] = $cat;
$data['main'] = 'category';
$data['navlist'] = $this->MCats->getCategoriesNav();
$this->load->vars($data);
$this->load->view('template');
}


when i load home page , its shows the following errors .


A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# s
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# S
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
#
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 7
http://localhost/codeigniter2/index....e/cat/longdesc
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 9
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 10
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# a
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# 7
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# 0
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# 2
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# s
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# S
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
#
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 7
http://localhost/codeigniter2/index....e/cat/longdesc
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 9
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 10
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# a
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# 7
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# 0
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# 3
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# p
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
# S
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

Line Number: 10
#
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 7
http://localhost/codeigniter2/index....e/cat/longdesc
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 9
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 10
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/navigation.php

El Forum

[eluser]danmontgomery[/eluser]
I'm just guessing that in

Code:
foreach ($list as $topkey => $toplist){

$list is not an array. Since the error very clearly states that it's on line 10, maybe you'd consider telling us what line 10 is?

El Forum

[eluser]mehdy[/eluser]
thank you for your reply . my line 10 is

Code:
foreach ($toplist['children'] as $subkey => $subname){

El Forum

[eluser]danmontgomery[/eluser]
This is faulty logic:

Code:
if(count($var))

Since almost anything will return 1 or more. You should use is_array() in combination with count() or empty()

El Forum

[eluser]mehdy[/eluser]
thank you very much , im trying ,still get the same error . will you kindly post me the , code in description . thank u once again ,

El Forum

[eluser]danmontgomery[/eluser]
Code:
if(is_array($toplist['children']) && count($toplist['children'])) {
  foreach($toplist['children'] as $subkey => $subname) {
    ...

El Forum

[eluser]mehdy[/eluser]
thank you , now i have the following error
# 1
# s
# S
#
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 7
http://localhost/codeigniter2/index.php/welcome/cat/longdesc
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 9
# a
# 7
# 2
# s
# S
#
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 7
http://localhost/codeigniter2/index.php/welcome/cat/longdesc
A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: views/navigation.php

Line Number: 9

El Forum

[eluser]mehdy[/eluser]
i think my model is not working properly, please help me understand this line in function getCategoriesNav

$data[0][$row->parentid]['children'][$row->id] = $row->name ,, what dose this line means and where the children come from ????