Solved

echo specific information from a POSTED array

Posted on 2012-03-13
11
203 Views
Last Modified: 2012-03-15
I have a form with input fields as follows:
<input name="fp[Title 1][]" type="text" id="test"  value=""/>
<input name="fp[Title 2][]" type="text" id="test"  value=""/>

I would like to output the form input like so:
<?php echo $fp[0][0]." ".$fp[0][1]; ?>
<?php echo $fp[1][0]." ".$fp[1][1]; ?>

I had though that the code might look something like this however this does not work.
<?php echo $_POST['fp'][0][0]." ".$fp[0][1]; ?>
<?php echo ['fp'][1][0]." ".$fp[1][1]; ?>
Can anyone show me best practice?
Cheers,
N
0
Comment
Question by:Neil_Bradley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37718531
A question first: in your code I see

<input name="fp[Title 1][]"

Title 1 hasn't quotes, so it seems to be a constant, but what is its value? To be sure on how your fields are posted by your form, print the $_POST array content

if (isset($_POST['submit'])){

echo "<pre>"; // more readable output
var_dump($_POST); //print out the whole content of the $_POST array

Here you'll see the exact names of your fields. If you need help about, post here the output of the var_dump so I'll can help you better

Cheers
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37719542
Agree with marqusG: var-dump() is  your friend.  But use it unconditionally.  In other words, do not test anything before calling it.  Just call it this way...
<?php // action script
error_reporting(E_ALL);
session_start(); // if needed
echo "<pre>";
var_dump($_POST);
echo "</pre>";
// REST OF PAGE GOES HERE

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37719546
Also, you may want to learn about the nature of the "id" field in HTML.  I do not think you want to have two id's that are the same.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 260 total points
ID: 37719584
Install this and run it, or just test on my server, here.  Note there is no use of the id= attribute!
http://www.laprbass.com/RAY_temp_neil_bradley.php
<?php // RAY_temp_neil_bradley.php
error_reporting(E_ALL);

// SHOW THE POSTED FORM
echo "<pre>";
var_dump($_POST);

// SHOW HOW TO USE THE CONTENTS OF THE POSTED FORM
foreach ($_POST["fp"] as $key => $sub_array)
{
    foreach ($sub_array as $sub_sub_array)
    {
        echo PHP_EOL . $key . ' POINTS TO A SUB-SUB-ARRAY CONTAINING ';
        var_dump($sub_sub_array);
    }
}

// CREATE THE FORM USING THE INPUTS FROM THE POST AT EE
$form = <<<FORM
<form method="post">
<input name="fp[Title 1][]" type="text" id="test"  value=""/>
<input name="fp[Title 2][]" type="text" id="test"  value=""/>
<input type="submit" value="GO" />
</form>
FORM;

echo $form;

Open in new window

0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 37722234
Thank you for you comments. I have re written the code to better illustrate my question
<?php error_reporting(E_ALL);
		 if (isset($_POST['fp'])){
$arrValues = array();
foreach( $_POST['fp'] as $list_id => $values ){
  foreach( $values as $k => $v ){
    $arrValues[] = $list_id.', ' .$v;
	echo '<li>'. $list_id .': '.$v.'</li><br>';
  }
}
		 }

?>
<form action='array.php' method='post' enctype="multipart/form-data">
<label for="fullName">title 1</label><input name="fp[Title 1 is][]" type="text" id="1"  value=""/><br />
<label for="email">title 2</label><input type="text" name="fp[Title 2 is][]" id="2"  value=""/><br />
<label for="email">title 3</label><input type="text" name="fp[Title 3 is][]" id="3"  value=""/><br />
<input type="submit" value="Register" id="submit" />
</form>

Open in new window

.

If you run this code (live view - http://www.beaconhilldesign.co.nz/sandbox/array.php) the entire contents of the array is successfully outputted using foreach.
My question is: If I wanted to output a specific part of the array as opposed to all of it , then how would this be achieved?
For example, if I wanted to output only the second array item (<?php echo $fp[0][0]." : ".$fp[0][1]; ?>) as opposed to listing the entire contents of the array what is the best way to achieve this?
Cheers,
N
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 240 total points
ID: 37723524
Well, you can do this;

echo $_POST['fp']['Title 1 is'][0];
echo $_POST['fp']['Title 2 is'][0];
echo $_POST['fp']['Title 3 is'][0];

Cheers
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 37723536
Let me say that I used

echo "<pre>";
var_dump($_POST);

If you do it, as me and Ray said, you see how info are formatted within the $_POST array:

array(1) {
  ["fp"]=>
  array(3) {
    ["Title 1 is"]=>
    array(1) {
      [0]=>
      string(4) "sasa"
    }
    ["Title 2 is"]=>
    array(1) {
      [0]=>
      string(4) "dada"
    }
    ["Title 3 is"]=>
    array(1) {
      [0]=>
      string(4) "caca"
    }
  }
}

This output leads you to the right way to output specific elements.
I said this for two reason: first, to allow you to understand better what we meant suggesting to use var_dump; second, to help you to correctly split points (if answer will be a good solution for you)

Cheers
0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 37726520
Both yours and Rays comments are helping me to not only answer this question but also to comprehend the working of an array..
echo $_POST['fp']['Title 1 is'][0]; is close to answering  my question.

The above code will work to echo the value of the 0 however it relies on adding 'Title 1 is' in the square brackets before it.  I also want to echo the value of 'Title 1 is' without actually writing 'title 1 is' (as is show above) how would this be done? In the foreach code I assign the value of the field name to $list and output it that way..

My reasons for needing  to pass along the "title 1 is" text is that this will be a variable so I cant always bank of that text being the same.
Updated code here:

Open in new window

<?php error_reporting(E_ALL);
		 if (isset($_POST['fp'])){
$arrValues = array();
foreach( $_POST['fp'] as $list_id => $values ){
  foreach( $values as $k => $v ){
    $arrValues[] = $list_id.', ' .$v;
	echo '<li>'. $list_id .': '.$v.'</li><br>';
  }
} echo "The name and value of the form input field is:";
// this should echo Title 1 is : the value of the fleid

echo "<pre>";
var_dump($_POST);
		 }

?>
<form action='array.php' method='post' enctype="multipart/form-data">
<label for="fullName">title 1</label><input name="fp[Title 1 is][]" type="text" id="1"  value=""/><br />
<label for="email">title 2</label><input type="text" name="fp[Title 2 is][]" id="2"  value=""/><br />
<label for="email">title 3</label><input type="text" name="fp[Title 3 is][]" id="3"  value=""/><br />
<input type="submit" value="Register" id="submit" />
</form>

Open in new window

.
Cheers,
N
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 240 total points
ID: 37726756
The problem is that you're using a variable value as an array index. To gain direct access an element of an array whose index is not numeric but a variable string you must to know what is the value. Otherwise you have to iterate through the array - as you do - to retrieve all elements.
Summarizing, AFAIK you can't access directly an element of an array without referring to its index: here my knowledge stops but Ray's one goes far and maybe he can teach something about this.

@Ray: I'm reading your articles about registration and login: they're really wonderful! I'm trying to make a login system oop and maybe I'll post some question here at EE: hope you'll wnat help me, eventually.

Cheers
0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 37726833
Big thanks you to both Ray and marqusG for participating in my question..
marqusG, your last post has helped to clarify what was before missing from my comprehension of multi dimensional arrays. Ray, I will be sure to use the info from your second post to help properly understand the anatomy of an array.
Thanks again.
Neil
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37727340
Thanks for the points.  Glad we could help -- it's a great question! ~Ray
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question