how do i create post values as a session array? - php

How do I create post values as a session array passing session to another pages? I'm working on a code it is showing only Array on the other pages when i echo $_SESSSION['size'];?
Here My Both Functions:
$size = array('size');
$_POST['size'];
$_SESSION['size'] = array();
foreach ($size as $key)
{
if (isset($_POST[$key]))
{
$_SESSION['size'][$key] = $_POST[$key];
}
}
$size=$_POST['size'];
$max= count($_POST['size']);
for($i=0; $i<$max; $i++)
{
$_SESSION['size']= $_POST['size'];
}
Both Function Are Showing Only Array On The Other Pages...

When you echo $_SESSION['size'] it will show Array because its an array, not single value.
If you want to check values, use this construction:
foreach($_SESSION['size'] as $key=>$val) {
echo $key . '=>' . $val . '<br />';
}
Instead of
echo $_SESSION['size'];
You can also use:
var_dump($_SESSION['size']);
or
print_r($_SESSION['size']);

On the first page simply store the value(s):
$_SESSION['size'] = $_POST['size'];
Now, if your POST contained only a single value for size, you simply use this:
echo $_POST['size'];
// or this:
some_function($_POST['size']);
However, if your POST really had an array of values for size (which is possible), you would also have an array in your session. You then need a simple loop:
foreach ($_SESSION['size'] as $size) {
echo $size;
// or this:
some_function($size);
}
Combined code that handles both cases:
if (is_array($_SESSION['size'])) {
foreach ($_SESSION['size'] as $size) {
echo $size;
// or this:
some_function($size);
}
} else {
echo $_SESSION['size'];
// or this:
some_function($_SESSION['size']);
}

Related

Display contents of $_POST one bye one

Hello I am submitting a form with POST method and I want its contents to be echo'ed one by one apart from the last one. So far I am using
<?php foreach($_POST as $data){
echo $data;
}
?>
which displays the whole array of $_POST, how can I make it using common "for" loop to not echo the last item of the array? It doesnt seem to work
<?php
$length=count($_POST)-1;
for($i=0; $i<$length; $i++) {
echo $_POST[$i];
?>
<br>
<?php } ?>
I am getting 5 errors, undefined offset 0 through 4 where the echo line is present
Do the following:
<?php
$counter = 0;
$lastItemOrder = count($_POST);
foreach($_POST as $value) {
$counter++;
if( $counter !== $lastItemOrder) {
echo $value;
}?>
<br><?php
} ?>
Your loop for just get numerical index like $_POSR[0], $_POST[1]... This just would work if in the HTML the attribute name of the input elements be numerical also, like name="0" and so on.
foreach perform loop on array independently of index, numerical or string.
Try this:
<?php
$counter = 0;
$lastItemOrder = count($_POST);
foreach($_POST as $index => $value) {
$counter++;
if( $counter !== $lastItemOrder) {
echo $index . ": " . $value;
}?>
<br><?php
} ?>
ok now I get it, I didnt know the difference between associative and numeric arrays. I fixed it with an if statement

Get last two items of json_decode results with PHP

The following code works and pulls all the images in from the json file.
$content = file_get_contents('URL');
$json = json_decode($content, true);
foreach($json['format'] as $item) {
echo '<img src="' . $item['picture'] . '">';
}
Is there a way that I can have it only grab the last two pictures.
Yes, there is a way.
$result = array_slice($json['format'], -2);
Have a try.
Use this:
$numItems = count(foreach($json['format']);
$i = 0;
foreach($json['format'] as $item) {
if(++$i === $numItems-1) {
result1 = $json['format'][$i]
echo "first picture!";
} if(++$i === $numItems) {
result2 = $json['format'][$i]
echo "second picture!";
}
}
And result1 and result2 is your pictures
You can reverse the order of the array, run it backwards in your foreach loop, grab the first two then break.
$reversed = array_reverse($json);
$counter = 0;
foreach ($reversed['format'] as $item) {
if ($counter == 2) {
break;
}
//your echo image code
++$counter;
}
My version using array_pop:
$content = file_get_contents('URL');
$json = json_decode($content, true);
// take last element of array, array reduces by 1 element
$last = array_pop($json['format']);
print_r($last);
// take last element of array again, array reduces by 1 element
$last = array_pop($json['format']);
print_r($last);
// Beware - using `$json['format']` later means that
// you use array without two last elements

How to create an array and save them into database?

Based on the code below, how would I create an array and then save the arrays to the database?
<?php
$all_userid = '';
if (isset($_POST['userid'])) {
foreach($_POST['userid'] as $userid) {
// Add filtering here
$all_userid .= $userid;
}
}
echo $all_userid;
?>
Output from echo;
003210032100321
Use json_encode http://www.php.net/json_encode
$string = json_encode($array);
Then when retriving it from database, use json_decode http://php.net/json_decode
<?php
$all_statuses = '';
if (isset($_POST['status'])) {
foreach($_POST['status'] as $status) {
// Add filtering here
$all_statuses .= $status;
}
}
echo($all_statuses);
To Save in database serialize this array
serialize($all_statuses)
?>
you can implode them with an - or / or _ and save them.Like
$i = 0;
$cnt = count($_POST['status']);
foreach($_POST['status'] as $status) {
// Add filtering here
$all_statuses .= $status;
if($i++ < $cnt) {
$all_statuses .= '_';
}
}
echo $all_statuses;
Will gives you like
id1_id2_id3_id4....
And while retriving you simply explode them with the imploded string,you will get all the result in form of an array.

How to fetch URL variable array using $_REQUEST['variable name']

I am using a URL to fetch data stored/shown within URL. I get all the value of variable using $_REQUEST['v_name'] but if there is a array in URL how can i retrieve that value.
For Example:
WWW.example.com/rooms?&hid=213421&type=E
I got the value hid and type using
$hid=$_REQUEST['hid'];
but in URL like:
WWW.example.com/rooms?&rooms=2&rooms[0].adults=2&rooms[0].children=0&rooms[1].adults=2&rooms[1].children=0
how can i retrieve value of adults and children in each room.
please help.
Thanks in Advance
You could also try something like this, since most of your original $_REQUEST isn't really an array (because of the .s in between each key/value pair):
<?php
$original_string = rawurldecode($_SERVER["QUERY_STRING"]);
$original_string_split = preg_split('/&/', $original_string);
$rooms = array();
foreach ($original_string_split as $split_one) {
$splits_two[] = preg_split('/\./', $split_one);
}
foreach ($splits_two as $split_two) {
if (isset($split_two[0]) && isset($split_two[1])) {
$split_three = preg_split('/=/', $split_two[1]);
if (isset($split_three[0]) && isset($split_three[1])) {
$rooms[$split_two[0]][$split_three[0]] = $split_three[1];
}
}
}
// Print the output if you want:
print '<pre>' . print_r($rooms, 1) . '</pre>';
$valuse = $_GET;
foreach ($valuse as $key=>$value)
{
echo $key .'='. $value. '<br/>';
}

print name using for loop php

Using for loop in PHP can we have numbers associate with a variable name?
ex:
$name1="hi";
$name2="khj";
for($i=0;$i<=2;$i++)
{
echo ..
}
How can we print $name1 and $name2 using for loop?
Thanks!
Yes this is called variable interpolation.
$name1="hi";
$name2="khj";
for($i=1;$i<=2;$i++) {
$var = 'name' . $i;
echo $$var;
}
Note: There are multiple syntaxes for variable interpolation in PHP. Also, I modified your loop to start at 1.
for($i = 1; $i <= 2; $i++)
{
echo $name{$i};
}
It would be way easier to put it in an array though, that's what we have them for.
$names = array();
$names[1] = 'A';
$names[2] = 'B';
foreach($names as $name)
{
echo $name;
}
put this in the for loop:
echo ${'name'.$i}."\n";
Better to use something like:-
$names[] = $name1;
$names[] = $name2;
foreach($names as $name){
echo $name;
}
This print the name1 and name2 .And i value must be start from 1
for($i=1;$i<=2;$i++)
{
echo ${'name'.$i}."<br>";
}

Categories