Solved

echo specific information from a POSTED array

Posted on 2012-03-13
11
199 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
  • 4
  • 4
  • 3
11 Comments
 
LVL 30

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 108

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 108

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
 
LVL 108

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

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 30

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 30

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 108

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now