Reputation: 37
I am selecting checkboxes and want to save in an associative array with current page number $_GET['page_no']
as it's index, but only 1 value goes in, why no other?
$pageno = $_GET['page_no']; //Say page no is 1
$_SESSION['selected_vals'] = array();
foreach($_POST['record_num'] as $throw_rec_nums) {
$_SESSION['selected_vals'][$pageno] = $throw_rec_nums;
}
What I expect
$_SESSOION['selected_val'] (
[1] => 24
[1] => 46
[1] => 56
)
But I only get 24 even if 3 checkboxes are selected
Note: $_GET['page_no'] is returned as array
Upvotes: 0
Views: 58
Reputation: 24551
You cannot use an array as array index. You will have to iterate over $pageno too, for example with next():
$array[current($pageno)] = ...;
next($pageno);
Note that this will only work if you make sure rt that $pageno actually IS an array and contains enough elements.
Upvotes: 0
Reputation: 441
Only 1 value goes in because your are replacing $_SESSION['selected_vals'][$pageno] value on each loop of foreach.
try create a counter to index it
it is a option
$_SESSION['selected_vals'] = array();
$_SESSION['selected_vals'][$pageno] = array();
foreach($_POST['record_num'] as $throw_rec_nums) {
$_SESSION['selected_vals'][$pageno][] = $throw_rec_nums;
}
Upvotes: 2
Reputation: 4291
$pageno
is not incrementing. In order for more than one value to be added to the array, it needs to be incremented while in the loop.
A solution would be something like:
$_SESSION['selected_vals'][$pageno][] = $throw_rec_nums;
That way all record numbers would be saved to the array at the page number specified.
Upvotes: 3