I had a problem with my array_push, that i noticed.
So what i'm doing.:
I have a site, where there are some buttons with a specific value.
Each value is getting fetched from a database.
I have a session called test, that get's converted to an array(to store multiple in the same array)
Everytime one of the buttons are clicked, the value for that specific button, is getting pushed to the array.
But, i can ONLY see that it has been pushed at the second try.
[test] => Array( [0] => 21304 )
This is what i see, after second try. But my array count, says that there are 2 elements, in that array.
Here is my code:
if(isset($_POST['process'])) {
if(!isset($_SESSION['test'])) {
$_SESSION['test'] = array();
$array_merge = array_push($_SESSION['test'], $_POST['process']);
}
}
The $_POST['process'] is the button with the unique value.
Can somebody maybe see what I'm doing wrong here?
Kind regards
You are only adding to the $_SESSION['test'] array if $_SESSION['test'] was not previously set.
So you need to always add an occurance to the session array and only initialise the session array if it was not previously set
session_start();
// ...
if(isset($_POST['process'])) {
if(!isset($_SESSION['test'])) {
$_SESSION['test'] = array();
}
$_SESSION['test'][] = $_POST['process'];
}
NOTE from the manual
If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.
Related
I want to add an element to PHP array when form is submitted and then add that array to $_SESSION so I can display it on other page while $_SESSION is active, but when an element is added to an array, element that is already in it is deleted so I constantly have 1 item in array. Any suggestions?
Here's the code:
$korpa = array();
$_SESSION["korpa"] = $korpa;
if(isset($_POST["add"])){
array_push($korpa, $_POST["id"]);
}
You keep assigning an empty array to your session variable, so it will be empty at the start of your script, before you append the POST variable.
Instead, you can append directly to that session variable if the condition is met.
// Initialize the session array if its not set
if (!isset($_SESSION["korpa"])) {
$_SESSION["korpa"] = [];
}
// Then append the POST value to the session if that's set
if (isset($_POST["add"])) {
$_SESSION["korpa"][] = $_POST["add"];
}
Naturally you will need to call session_start() at the top of every page using sessions, otherwise they will not be set across your different pages.
I have this array in php code. I want to have that whenever page is being called it should print value of first array index and when next time second value of array index and so on... what modification I could do? for now it´s printing everything when being called single time.
<html>
<?php
$addresses = array('ifcbxespra', 'ifcheqjbmea', 'ifcqiknsa', 'ifcqirtjla', 'ifcwqsrlmn', 'ifclmkmzhz','ifcwdujhgc','ifcihddngh','icffhzudcd','ifchnsqzgs','ifcgssqrhg');
foreach ($addresses as &$value) {
echo $value ;
}
?>
</html>
I'm not sure if I understood what you want. But if you want to print the first array's value when the page loads one time, the second array's value when the page loads another time and so on, you can do this:
<?php
if(!isset($addresses) || empty($addresses)){ //checks if the array is not initialized or if it's empty
$addresses = array('ifcbxespra', 'ifcheqjbmea', 'ifcqiknsa', 'ifcqirtjla', 'ifcwqsrlmn', 'ifclmkmzhz','ifcwdujhgc','ifcihddngh','icffhzudcd','ifchnsqzgs','ifcgssqrhg');
echo $addresses[0]; //print the first value
array_splice($addresses, 0, 1); //removes the first element of the array and reindexes it
}else{
echo $addresses[0]; //print the first value
array_splice($addresses, 0, 1); //removes the first element of the array and reindexes it
}
The logic behinds it is: if the array already exists and is not empty (it has values), print the first value and then remove it, so next time the first value will be the second actual value. When the array is empty, redefine it as to start again.
You can search for more information on array_splice() here.
P.S.: you have to use PHP's $_SESSION to save the array between the pages.
You can use something like $_SESSION and store there the last index.
For example:
$array = array('one', 'two', 'three');
if (!$_SESSION['nextIndex'] || $_SESSION['nextIndex'] >= count($array)) {
$_SESSION['nextIndex'] = 0
}
// print the value
echo $array[$_SESSION['nextIndex']];
// increment the nextIndex
$_SESSION['nextIndex']++;
NOTE: This will only work for the same user. Each page reload will increment the array index. But if you need some cross-user counting, then you have to store the information somewhere on the server, like a DB or even a simple txt file.
Check out this example: http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php/
Finally I solved this problem with the MySQL. created a column with all code. and then call the script every-time when user press button. In the script first I fetch first raw and print that value, and then, delete that raw. so every-time user will get unique value from the list of code And it is working fine.
I've two page , first page there's an array data and second page I want call array data
Like this
First page index.php
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second page next.php
I want to call array from first page
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one'])
May I know what's wrong? As array can't display in second page.
I think you should change this code $array = $_SESSION['one']; to $array[] = $_SESSION['one'];. I`m not sure & not tested as well but I think so. Hope this will help.
You need to start the session if you already have not. Without starting the session, you can't assign value to the session variable. So the first code snippet would be like this:
session_start();
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second snippet looks fine but the last line is missing a semicolon. It might sound silly, but that can prevent the whole script from running. Here's the code fixed.
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one']);
Post more code if this does not work.
I have a variable which changes its value in every ajax request.
What I want to accomplish is to have a session array $_SESSION["tmp_arr"] and fill it with the values of this same variable. I dont want to overwrite the value of the $_SESSION variable, but append it in a array.
Which is the correct way to accomplish that?
If there is no such element in _SESSION or if it's not an array create a new one with the first/initial value. Otherwise append the new value to the existing array.
session_start();
[...]
if ( !isset($_SESSION["tmp_arr"]) || !is_array($_SESSION["tmp_arr"]) ) {
$_SESSION["tmp_arr"] = array( $newValue );
}
else {
$_SESSION["tmp_arr"][] = $newValue;
}
foreach($_POST['door_check'] as $door_check)
{
$_SESSION['front_door']['door'] = $door_check;
}
I have this little section of code that checks how many boxes were checked and then creates an array of the check box values.
The thing is, when I add that 'door' key, the array only adds one value no matter how many checkboxes were checked. When I just leave it empty, it adds all of them like [0], [1], [2] etc
Why is this?,
Your foreach() loops overwrites old variable each time. You need to make your session variable an array, for example
foreach($_POST['door_check'] as $door_check)
{
$_SESSION['front_door']['door'][] = $door_check;
}
edit: Don't forget to validate that data when you save it for later use.
Try something like this:
foreach($_POST['door_check'] as $door_check) {
$_SESSION['front_door']['door'][] = $door_check;
}
or maybe even:
$_SESSION['front_door']['door'] = $_POST['door_check'];