How to overcome Uninitialized string offset error message
Hi there Im getting the following error when I try to do multiple saves and just change one if multiple checkboxes have been selected. The error message is this:
Notice (8): Uninitialized string offset: 1 [APP\controllers\selections_controller.php, line 46]
Not sure why but it saves the first checked checkbox value fine, but then for each one after that it throws the above error. Below is my code
In my view:
<?php
$i = 0;
foreach($members as $member)
{
echo $form->checkbox('Selection.member_id.'. $i, array('type' => 'checkbox', 'value' => $member['Member']['id']));
echo $member['MemberDetail']['first_name']. ' ' .$member['MemberDetail']['last_name'];
$i += 1;
}
echo $form->input('Selection.iValue', array('type' => 'hidden', 'value' => $i));
?>
In my controller:
function add(){
if(!empty($this->data))
{
$this->data['Selection']['comments'] = 'Test';
for ($i = 0; $i < $this->data['Selection']['iValue']; $i = $i + 1)
{
$value = $i;
***Line 46*** if($this->data['Selection']['member_id'][$value] != 0) {
$this->data['Selection']['member_id'] = $this->data['Selection']['member_id'][$i];
$this->Selection->save($this->data);
}
}
$this->Session->setFlash($i . ' selections have been added', 'default', array('class' => 'flash_success'));
}
Surely it's something quite easy to fix but I can't seem to crack it??
Thanks for the help
Trav.
Asked by tnbrooks, on 21/2/10
1 Answer
well try this
debug($this->data['Selection']['member_id']);
debug($this->data['Selection']['member_id'][$value]);
and you should see the problem
$this->data['Selection']['member_id'] would be something like "1" or "20" it is not an array
php works like this
$abc = 'hello';
echo $abc[0]; //output 'h'
echo $abc[10]; //error that you have cos there is not a 11'th char
Answered by dogmatic69on 21/2/10
Ok so I tidied it up a bit and have used $i where values was as $i is in the foreach loop. all I was doing was assigning value to it. Anyways, if i add this line:
debug("The value of I is " . $i"); it all renders fine.
But to do it like this:
debug("SelectMember is " . $this->data['Selection']['member_id'][$i]);
it works fine for the first loop, but entering the second time I still get the uninitialised string offset error.
tnbrooks - on 24/2/10
Your Answer
You can use Creole Wiki Syntax to format your text.
Tagged with
Rating
0
Viewed
425 times
Last Activity
on 24/2/10
I tried what you said however it rendered a multi select box instead of the checkbox, hence I went the other way with a foreach loop. My code in the controller is this:
$this->set('members', $this->Member->find('all', array('fields' => array('Member.id', 'MemberDetail.first_name', 'MemberDetail.last_name'))));
is this causing it to render incorrectly?
tnbrooks - on 24/2/10