Session array accessing on next button click of next button - php

How to get next array element from session array on next button click?
I tried next($_SESSION['qid']) it didn't work
if((int)$_SESSION['qn']<=20) {
$_SESSION['qn']=$_SESSION['qn']+1;
$_SESSION['qid']++;
}
I also tried
$_SESSION['qid']=next($_SESSION['qid']);
But this neither worked. Can someone help me?

_SESSION array is an associative array. You can't access it by numeric index, but you must specify the index name (e.g. in your code, $_SESSION['qid']). Anyway, you can still use next() function, passing the array $_SESSION (see here: http://php.net/manual/en/function.next.php). The correct way to use it is:
$element = next($_SESSION)
you'd likely want to put this code in a cycle.
Additionally, your code:
$_SESSION['qn']=$_SESSION['qn']+1;
means: assign to $_SESSION['qn'] the value of $_SESSION['qn'] plus 1, which is not what you want.
In case you'd want the next element in a NON associative array, you should use:
$arr = $arr[$i+1]
where $i is a integer value.
Update: regarding your comment, why don't you save a regular array (non associative) inside $_SESSION['questions']? This way you'd have the questions accessible this way:
$_SESSION['questions'][0], $_SESSION['questions'][1]...
Now you can use it within a cycle, or whatever you want. E.g.:
echo $_SESSION['questions'][$current_question_id+1];
where $current_question_id would be the current question index, which will be updated (+1) every clic on the next button

Related

Push or Merge data into existing Array

When working with existing code, it takes one array and places it into another in the fashion shown below.
I believe the empty brackets are the same thing as simply pushing it and appending it to the first available index.
$g['DATA'][] = $p;
After this is done, I have my own array that I would like to append to this as well. I tried using array_merge() with $g['DATA'][]as a parameter, but this is invalid for obvious reasons.
My only thought is to create a foreach loop counter so I can figure out the actual index it created, however I have to assume there is some cleaner way to do this?
Just simply use the count() of your $g["DATA"] array as index and then you can merge it like this:
$g['DATA'][count($g["DATA"])-1] = array_merge($g['DATA'][count($g["DATA"])-1], $ownArray);
//^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
// -1 Because an array is based index 0 -> means count() - 1 = last key

PHP Cycle through array. Move the first element to the last

Just thought I would like to share this should someone find a use for it.
Basically I needed a list of HTML colors to loop / cycle through. So I need to remove the first element of the array, place it at the end and then get the current HTML color.
Given the following array:
$colors = array(
"#2265fa", "#b61d1e", "#53b822", "#a9d81c", "#d6e9f5", "#43cc7d", "#e3159a",
"#80c85e", "#17b303", "#989240", "#014c07", "#d265f3", "#22bbb9", "#6c69a9",
"#7ea13a", "#0dcea2", "#99c27d", "#41405b", "#731801"
);
So this is what I came up with. Sure there will be hundreds of ways to do this. This is my take on it.
# Array_shift returns the value it takes off the beginning of the array.
# And I merely append this to the end of the array
$colors[] = array_shift($colors);
# Using current I am able to get the current first element of the array back
echo current($colors);
In this case it would be "#b61d1e" that is the current index for the array. May you find this useful somewhere.

Problems with nested array after using unset

I have a dynamically built nested array from which i need to drop one or more indexes.
Having scoured the php information pages, i found that using
unset($quotes_array[0]['methods'][3]);
would remove the last set of data in the array.
But, if i try to use
unset($quotes_array[0]['methods'][0]);
or any other set apart from the last, it messes up the output that is generated from the array.
For instance, if i had a,b,c,d: I can remove d without issue, but if i try to remove a,
i get a blank radio button followed by b,c, with d missing all together when the array data is processed.
I am assuming i need to reindex the array, but every attempt i've made so far has failed to give the required results, most likely because i'm reindexing $quotes_array, whereas the data i actually need to reindex is that within the 'methods' indices.
Is there a way to fix this issue?
unsetting the first element is easy if you use array_shift. array_shift will pop the first element off and reindex your array for you.
array_shift($quotes_array[0]['methods']);
if you need to unset something in the middle (such as [ 2 ] out of [ 3 ]) you could use array_values. First unset the element you want to remove, then reindex them with array_values.
unset($quotes_array[0]['methods'][2]);
array_values($quotes_array[0]['methods']);
if you wanted to remove the last element of an array you could use array_pop. array_pop will pop the last element off the array. There is no need to reindex in this situation.
array_pop($quotes_array[0]['methods']);
try
array_values($quotes_array[0]['methods']);

validate multidimensional array

As the question says i need to validate a multidimensional array, just so you know this is my first time actually using arrays so it might be a pretty bad script but it works and that's all I'm after at the moment. Okay so it's working I have two sessions displaying in this array, when i remove one of the sessions I get this error
"Notice: Undefined index: pop in C:\inetpub\wwwroot\dropdown\test.php on line 30"
I think i know how to fix it but I don't actually know how to implement it. this is me talking through what im after
$myarray (
IF isset session cityname
add the value to my array
ELSE
add a blank value in its place (or just remove it from the array altogether)
IF isset session pop
add the value to my array
ELSE
add a blank value in its place (or just remove it from the array altogether)
echo myarray
please note cityname is mandatory whereas pop is not
That's essentially what I'm trying to achieve but i haven't the slightest how to actually go about doing it, here is my current code
if(isset($_SESSION['cityname'])){
$myarray = array(array($_SESSION['cityname']),
array($_SESSION['pop'])
);
foreach($myarray as $key=>$value){
echo $myarray[$key][0];
}
Any help MUCH appreciated I've lost to much hair to this problem the last couple of weeks!
That notice is telling you that you're using $_SESSION['pop'] that has never been set.
In fact in your code you just checked for $_SESSION['cityname'] but then you add $_SESSION['pop'] to your array.
EDIT
If you want $_SESSION['pop'] to be optional and you want to get rid of that notice, just check if $_SESSION['pop'] is set or not:
if(isset($_SESSION['cityname'])){
$myarray = array(array($_SESSION['cityname']));
if(isset($_SESSION['pop'])) { $myarray[] = array($_SESSION['pop']); }
foreach($myarray as $key=>$value){
echo $myarray[$key][0];
}

What does this code do?

if (in_array($form['#submit'], 'search_box_form_submit')) {
$key = array_search('search_box_form_submit', $form['#submit']);
unset($form['#submit'][$key]);
}
array_unshift($form['#submit'], 'mymodule_search_box_submit');
What does the code do? I don't follow it well; I expect someone can explain it to me, line by line.
If the submitted form contains a variable named "search_box_form_submit", delete it, and then add a new variable called "mymodule_search_box_submit".
Perhaps somebody wanted to override the drupal search function and didn't want the default processor to fire at all. Thanks kiamlaluno in the comments.
if (in_array($form['#submit'], 'search_box_form_submit')) {
If the value 'search_box_form_submit' is present in the array $form['#submit']
$key = array_search('search_box_form_submit', $form['#submit']);
Then set the variable $key to the array key for the value 'search_box_form_submit' in the array $form['#submit']
unset($form['#submit'][$key]);
Then unset (delete) that array element
array_unshift($form['#submit'], 'mymodule_search_box_submit');
Put a new element at the beginning of the array $form['#submit'] with the value 'mymodule_search_box_submit'
is the text "search_box_form_submit" in the array $form['#submit']
if so find the key for search_box_form_submit
then remove from array
put the value mymodule_search_box_submit in the front of the array $form['#submit']
i recommend reading the manual page for the functions used.

Categories