Display contents of $_POST one bye one - php

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

Related

Variables only printable from foreach loop

I want to get the variables within a foreach loop in PHP, when it is within the while loop I can print all the object data but when I echo the variables outside of the while loop it prints only a value of the variable, how do I declare the variables global and access their values anywhere?
<?php
global $answerList, $countList;
foreach( $finalAnswer as $answerID ) {
foreach ($answers as $ans => $answer_list) {
foreach ($answer_list->unique('AnswerShortID') as $answer) {
if ($answer->AnswerShortID === $answerID){
$answerList = $answer->Text;
$countList = $answer_list->count();
echo $answerList;
}
}
}
}
echo $answerList;
?>
You don't need the global, you are overwriting the variables each time through the loop so the variable only contains the last one. Just save to an array, then you can loop it later or implode it to display:
// you don't need to do this in the loop
$countList = $answer_list->count();
foreach ($answer_list->unique('AnswerShortID') as $answer) {
if ($answer->AnswerShortID === $answerID){
$answerList[] = $answer->Text;
}
}
echo implode(',', $answerList);
//or
foreach($answerList as $value) {
echo "<something>$value</something>other stuff";
}
The below will store all values in a string, which you can then echo out later. By adding a period before the equals sign you are adding new values to the end of the string with each iteration of the loop. Your code currently deletes the last value and replaces it with a new one every time you complete the loop.
<?php
global $answerList, $countList;
foreach( $finalAnswer as $answerID ) {
foreach ($answers as $ans => $answer_list) {
foreach ($answer_list->unique('AnswerShortID') as $answer) {
if ($answer->AnswerShortID === $answerID){
$answerList .= $answer->Text;
$countList .= $answer_list->count();
echo $answerList;
}
}
}
}
echo $answerList;
?>

modify key in a foreach loop php

I want to change the value of the $key because I have array_splice inside the loop which change the position of my values so - it mess up the value I need in a specific place.
I tried $key-- but it doesn't work.
for example when I print the $key after I do echo $key it's fine but when I echo $key just after the foreach loop I get the worng value.
Any ideas?
foreach ($cut as $key => $value) {
echo "foreach key:".$key."<br>";
if(in_array($value,$operators))
{
if($value == '||')
{
echo "found || in position:".$key."<br>";
if(($key+1<sizeof($cut)))
{
$multi = new multi;
echo "<br>"."key-1: ";
print_r($cut[$key-1]);
echo"<br>";
echo "<br>"."key+1: ";
print_r($cut[$key+1]);
echo"<br>";
$res = $multi->orex($cut[$key-1],$cut[$key+1],$numString);
$cut[$key-1]= $res;
array_splice($cut,$key,1);
array_splice($cut,$key,1);
$key--; //here trying to change the key
echo "new string:";
print_r($cut);
echo "<br>";
echo "key:".$key."<br>";
}
}
}
}
Updated
I don't think it is a good idea to change the array itself inside the foreach loop. So please crete another array and fill data into it, which will be your result array. This method works well when your array data is not big, in other words, most situations.
Origin
I don't know what do you mean. Let me give it a guess...
You want:
foreach($arr as $key=>$val){
$newkey = /* what new key do you want? */
$arr[$newkey] = $arr[$key];
unset($arr[$key]);
}

for loop append variable php

I have a xml file that i want to echo to the browser
echo $rule = $info->rule1
echo $rule = $info->rule2
Result:
example 1
example 2
Because the rule in the xml is dynamic i want to count how much rules there are and pass that variable behind the "rule"
$xml = simplexml_load_file('file.xml');
$xml_nested = $xml->monthlegenda;
echo "<ul>";
foreach ($xml_nested as $info):
for($i=1; $i < count($info); $i++){
$rule = $info->rule;
$rule .= $i;
echo $rule;
};
endforeach;
echo "</ul>";
As a result i expect:
example 1
example 2
but i get
12
You're probably looking for a way to get the property of an object from a string, which is done like so:
$instance->{$var.'string-part'.$otherVar};
In your case, that'd be:
echo $info->{'rule'.$i};
For more details, refer to the man pages on variable variables. Especially the section entitled " #1 Variable property" should be of interest to you...
But since you're echoing <ul> tags before and after the loop, I'm assuming you're trying to create a list, in which case, your echo statement should look like this:
echo '<li>', $info->{'rule'.$i}, '</li>';//comma's not dots!
Note that you'll never loop through the entire $info object, because of your for loop. You should either write:
for ($i=1,$j = count($info);$i<=$j;$i++)
{
echo $info->{'rule'.$i};
}
Note that I'm assigning count($info) to a variable, to avoid calling count on each iteration of the loop. You're not changing the object, so the count value will be constant anyway... or simply use foreach:
foreach($info as $property => $val)
{//val is probably still an object, so use this:
echo $info->{$property};
}
In the last case, you could omit the curlies around $property, but that's not recommended, but what you can do here, is check if the property concerned is a rule property:
foreach($info as $property => $val)
{
if (substr($property, 0, 4) === 'rule')
{//optionally strtolower(substr($property, 0, 4))
echo '<li>', $info->{$property}, '<li>';
}
}
That's the easiest way of doing what you're doing, given the information you've provided...
Try this code for "for":
for($i=1; $i < count($info); $i++)
{
$rule = $info->{'rule'.$i};
echo $rule;
}
Try this one:
$rule = $info->{rule.$i};
Check this
$xml = simplexml_load_file('file.xml');
$xml_nested = $xml->monthlegenda;
echo "<ul>";
foreach ($xml_nested as $info)
for($i=1; $i < count($info); $i++){
$rule = "";
$rule = $info->rule;
$rules = $rule.$i;
echo $rules;
}
echo "</ul>";

how do i create post values as a session array?

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']);
}

php foreach echo prints "Array" as value

Perhaps I'm simply having trouble understanding how php handles arrays.
I'm trying to print out an array using a foreach loop. All I can seem to get out of it is the word "Array".
<?php
$someArray[]=array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value;
?>
<br />
<?php
}
?>
This prints out this:
Array
I'm having trouble understanding why this would be the case. If I define an array up front like above, it'll print "Array". It almost seems like I have to manually define everything... which means I must be doing something wrong.
This works:
<?php
$someArray[0] = '1';
$someArray[1] = '2';
$someArray[2] = '3';
$someArray[3] = '4';
$someArray[4] = '5';
$someArray[5] = '6';
$someArray[6] = '7';
for($i=0; $i<7; $i++){
echo $someArray[$i]."<br />";
}
?>
Why won't the foreach work?
here's a link to see it in action >> http://phpclass.hylianux.com/test.php
You haven't declared the array properly.
You have to remove the square brackets: [].
<?php
$someArray=array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value;
?> <br />
<?php
}
?>
Try:
<?php
$someArray = array('1','2','3','4','5','6','7'); // size 7
foreach($someArray as $value){
echo $value . "<br />\n";
}
?>
Or:
<?php
$someArray = array(
0 => '1',
'a' => '2',
2 => '3'
);
foreach($someArray as $key => $val){
echo "Key: $key, Value: $val<br/>\n";
}
?>
actually, you're adding an array into another array.
$someArray[]=array('1','2','3','4','5','6','7');
the right way would be
$someArray=array('1','2','3','4','5','6','7');

Categories