CodeIgniter Forums
Invalid argument supplied for foreach() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Invalid argument supplied for foreach() (/thread-34665.html)



Invalid argument supplied for foreach() - El Forum - 10-06-2010

[eluser]Unknown[/eluser]
i have problem when foreach($array as $k=>$v); is show errors, because $array have argument == NULL.
how is pass error. i have input @$array but can not.


A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: csp/csp.php

Line Number: 68
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: csp/csp.php

Line Number: 68


Invalid argument supplied for foreach() - El Forum - 10-06-2010

[eluser]Twisted1919[/eluser]
what's your array ?
usually you'll do a check before you're going in the loop, something like :
Code:
if(!empty($array) && is_array($array))
{
foreach()...
}



Invalid argument supplied for foreach() - El Forum - 10-06-2010

[eluser]Unknown[/eluser]
[quote author="Twisted1919" date="1286381479"]what's your array ?
usually you'll do a check before you're going in the loop, something like :
Code:
if(!empty($array) && is_array($array))
{
foreach()...
}
[/quote]

this is code
controller

public function caddsp(){
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');
$this->form_validation->set_rules('size', 'Size', 'trim|required');
$this->form_validation->set_rules('color', 'Color', 'trim|required');
$this->form_validation->set_rules('chatlieu', 'Chat_lieu', 'trim|required');
$this->form_validation->set_rules('sku', 'Sku', 'trim|required');

if ($this->form_validation->run() == FALSE){
$this->headers();
$this->load->model('msp/msp');
$data['cate'] = $this->msp->mspcate();
$data['cateroot'] = $this->msp->mspcateroot();
$this->load->view('vsp/vaddsp',$data);
$this->footer();
}else{
$newName = time();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|gif|jpg|png';
$config['file_name'] = $newName.'.gif';
$config['max_size'] = '800';
$config['max_width'] = '1024';
$config['max_height'] = '768';

$this->load->library('upload',$config);
for($i=1;$i<=8;$i++){
if(!$this->upload->do_upload("img".$i)){
$error=array("error"=> $this->upload->display_errors());
}else{
@$dataupload[$i] = $this->upload->data();
}
}
for($i=1;$i<=8;$i++){
foreach($dataupload[$i] as $img=>$v):
if($i==1) @$thumb_image = $v;
if($i==2) @$full_image = $v;
if($i==3) @$a_image = $v;
if($i==4) @$b_image = $v;
if($i==5) @$c_image = $v;
if($i==6) @$d_image = $v;
if($i==7) @$e_image = $v;
if($i==8) @$f_image = $v;
break;
endforeach;
}

@$date = date("Y-m-d H:iConfused");
@$data['info'] = array(
"name" => $this->input->post('name'),
"chitiet" => $this->input->post('FCKEDITOR1'),
"price" => $this->input->post('price'),
"color" => $this->input->post('color'),
"size" => $this->input->post('size'),
"chatlieu" => $this->input->post('chatlieu'),
"sku" => $this->input->post('sku'),
"actived" => $this->input->post('actived'),
"spmoi" => $this->input->post('spnew'),
"spbanchay" => $this->input->post('spbanchay'),
"cateroot_id" => $this->input->post('cateroot'),
"cate_id" => $this->input->post('cate'),
"thumb_image" => "$thumb_image",
"full_image" => "$full_image",
"a_image" => "$a_image",
"b_image" => "$b_image",
"c_image" => "$c_image",
"d_image" => "$d_image",
"e_image" => "$e_image",
"f_image" => "$f_image",
"datecreated" => "$date",
);
$this->load->model('msp/msp');
$this->msp->maddsp($data);
$this->clistsp();
}
}

view
for($i=1;$i<=8;$i++){
echo form_label('Img'.$i." ")."&lt;input type=\"file\" name=\"img".$i."\" size=\"25\" /&gt;&lt;br/>";
}


Invalid argument supplied for foreach() - El Forum - 10-20-2010

[eluser]vamsee[/eluser]
Quote:Try this


Code:
print_r($array);



Invalid argument supplied for foreach() - El Forum - 10-20-2010

[eluser]LuckyFella73[/eluser]
Please use [CODE] tags when posting code and make a comment at the
line you have the error - in your case line 68. Otherwise it's hard
to find the right place to look for your error.