Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

echo specific information from a POSTED array

Posted on 2012-03-13
11
Medium Priority
?
205 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 111

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 111

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1040 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 960 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 960 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 111

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

670 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