我的问题是,我键入第一行文本框中的兄弟细节,我选中复选框没有错误显示。如果我没有选中该复选框,则会显示以下错误。你能说点什么吗?我是CodeIgniter的新手。
我收到错误消息:(我如何解决错误)
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: controllers/registration.php
Line Number: 288
在我的代码下面:
foreach($querystud_frm->result() as $row)
{
echo "<tr><td>".$i.")</td>";
$data=array('name'=>'s_sibling_name[]','class'=>'textbox','value'=>$row->s_sibling_name,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_nric[]','class'=>'textbox','value'=>$row->s_sibling_nric,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$sib_dob=date('d-m-Y',strtotime(trim($row->s_sibling_dob)));
$data=array('name'=>'s_sibling_dob[]','class'=>'textbox','value'=>$sib_dob,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_relation[]','class'=>'textbox','value'=>$row->s_sibling_relation,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_occupation[]','class'=>'textbox','value'=>$row->s_sibling_occupation,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$data=array('name'=>'s_sibling_income[]','class'=>'textbox','value'=>$row->s_sibling_income,'style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
$sib_yes = $row->s_sibling_yes;
echo $sib_yes;
if($sib_yes == 'YES')
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'YES', 'checked' => TRUE);
echo "<td >".form_checkbox($data)."</td>";
}
else
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'NO', 'checked' => FALSE);
echo "<td >".form_checkbox($data)."</td>";
}
echo "</tr>";
$i++;
}
if($i<10)
{
$var=array("s_sibling_name","s_sibling_nric","s_sibling_dob","s_sibling_relation","s_sibling_occupation","s_sibling_income");
for($j=$i;$j<=10;$j++)
{
echo "<tr><td>".$j.")</td>";
foreach($var as $value)
{
$value=$value.'[]';
$data_val=array('name'=>$value,'class'=>'textbox','value'=>'','style'=>'text-transform:uppercase;');
echo "<td>".form_input($data_val)."</td>";
}
if($sib_yes == 'YES')
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'YES', 'checked' => TRUE);
echo "<td >".form_checkbox($data)."</td>";
}
else
{
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'NO', 'checked' => FALSE);
echo "<td >".form_checkbox($data)."</td>";
}
echo "</tr>";
}
unset($var);
}
$querystud_frm->free_result();
}
else
{
$var=array("s_sibling_name","s_sibling_nric","s_sibling_dob","s_sibling_relation","s_sibling_occupation","s_sibling_income");
$var_sib=array("s_sibling_yes");
for($i=1;$i<=10;$i++)
{
echo "<tr><td>".$i.")</td>";
foreach($var as $value)
{
$name=$value.'[]';
$data=array('name'=>$name,'class'=>'textbox','value'=>'','style'=>'text-transform:uppercase;');
echo "<td >".form_input($data)."</td>";
}
$data= array('name' => 's_sibling_yes[]', 'id' => 's_sibling_yes', 'value' => 'YES', 'checked' => FALSE);
echo "<td >".form_checkbox($data)."</td>";
echo "</tr>";
}
unset($var,$i,$data,$var_sib);
}
控制器/和registration.php
if($this->input->post('siblingsfrm'))
{
$this->data['nric']=$this->input->post('s_nric');
//echo $this->input->post('name');
$name=$this->input->post('s_sibling_name');
$nric=$this->input->post('s_sibling_nric');
$dob=$this->input->post('s_sibling_dob');
//$dob=strtotime($dob);
//$dob=date('Y-m-d',strtotime($dob));
$relation=$this->input->post('s_sibling_relation');
$occupation=$this->input->post('s_sibling_occupation');
$income=$this->input->post('s_sibling_income');
$sib_yes=$this->input->post('s_sibling_yes');
$sib_id=$this->input->post('s_sibling_id');
//print_r($sib_id);
//print_r($name);
//exit();
//$this->load->helper('date');
for($i=0;$i<count($name);$i++)
{
if($name[$i]!='')
{
$income[$i]=trim($income[$i]);
if($income[$i]=='')
$income[$i]=0;
if($sib_yes[$i]!= 'YES')
$sib_yes[$i]='NO';
$date=date('Y-m-d',strtotime(trim($dob[$i])));
$insert_values=array('s_nric'=>$this->data['nric'],'s_sibling_name'=>strtoupper(trim($name[$i])),
's_sibling_nric'=>strtoupper(trim($nric[$i])),'s_sibling_dob'=>$date,
's_sibling_relation'=>strtoupper(trim($relation[$i])),
's_sibling_occupation'=>strtoupper(trim($occupation[$i])),
's_sibling_income'=>strtoupper(trim($income[$i])),
's_sibling_yes'=>trim($sib_yes[$i]));
if(is_array($sib_id) && isset($sib_id[$i]) )
{
$where=array('s_sibling_id'=>$sib_id[$i]);
$this->db->update('si_student_siblings',$insert_values,$where);
}
else
{
//print_r($insert_values);
$this->db->insert('si_student_siblings',$insert_values);
}
}
}
$this->data['level']=7;
}
答案 0 :(得分:0)
问题是如果没有选中复选框,它将不会向服务器发送任何数据。所以,这行代码......
$name=$this->input->post('s_sibling_name');
...实际上会将$name
变量设置为FALSE
(请参阅{{3>}中的 $ this-&gt; input-&gt; post()文档)
因此,第一次通过循环,您的代码正在$name[0]
查找数组项,而实际上$name
甚至不是数组,它是FALSE
。
你可以用if / else语句包围你的for循环来解决这个问题(不是最优雅的解决方案,但它会起作用):
if (is_array($name) && count($name) > 0)
{
for($i=0;$i<count($name);$i++)
{
if($name[$i]!='')
//etcetera...
}
}