Jump to content

Help with $_POST Array and iteration...


chito

Recommended Posts

Hello Im new to PHP and still trying to wrap my head around array's in particular how to iterate through them to get what I need. As an assignment I have to build a party invite  form with up to 10 inputs for a name and email for each individual that will mail each person an invite, and then the admin a list of who was mailed. I have tried a few ways to approach this but keep running into a dead end. Without giving me the complete solution can you help me with the concepts and how to structure and approach this from a newbie standpoint. Mostly the issue I have is how to get the values from each item and build a complete Name & Email for each person from 2 separate elements.

 

I have an input form with 10 inputs formated like this:

<form method="POST" action="invite_process.php">
<tr>
        <td align="right">Name:</td>
        <td align="left"><input type="text" name="name1" value="" size="25" /></td>
        <td align="right">Email:</td>
        <td align="left"><input type="text" name="email1" value="" size="25" /></td>
</tr>

My $_POST array ends up looking like this:

Array
(
    [name1] => myname1
    [email1] => email1@email.com
    [name2] => myname2
    [email2] => email2@email.com
    [name3] => myname3
    [email3] => email3@email.com
    [name4] => myname4
    [email4] => email4@email.com
    [name5] => 
    [email5] => 
    [name6] => 
    [email6] => 
    [name7] => 
    [email7] => 
    [name8] => 
    [email8] => 
    [name9] => 
    [email9] => 
    [name10] => 
    [email10] => 
)

This obviously gives me each element I need:

while(list($key, $value) = each($HTTP_POST_VARS))
{
echo "$key = $value<br/>";

}

name1 = myname1
email1 = email1@email.com
name2 = myname2
email2 = email2@email.com
name3 = myname3
email3 = email3@email.com
name4 = myname4
email4 = email4@email.com


also tried a foreach loop:

foreach ($_POST as $key => $value) {
   foreach ($value as $name => $x);
    echo $name."<br/>";
    echo $value;
}

myname1
email1@email.com
myname2
email2@email.com
myname3
email3@email.com
myname4
email4@email.com

My problem is stepping thru each element and grabbing the value I need to build the complete name and email address for the email. Any help and pointers would be greatly appreciated.. I REALLY want to get this down!

Link to comment
Share on other sites

Having looked at your code it seems all you're missing is abit of glue.  Using your convention of name# for names, and email# for email---

 

$list = array();
foreach ($_POST as $key => $value)  {
  // is this a name?
  if (substr($key, 0, 4) == 'name') {
     $index = (int)substr($key, 4);
     $list[$index]['name'] = $value;
  }
  
  // is this an email
  if (substr($key, 0, 5) == 'email') {
     $index = (int)substr($key, 5);
     $list[$index]['email'] = $value;
  } 
}

// Now foreach through $list, and you have an array of arrays, where the nested array has 'name' and 'email' keys for easy access.

Link to comment
Share on other sites

Do this. That way if you decide to change the names in your form widgets it will still work.

 

$i=1;

foreach($array as $key => $value)

{

if($i % 2 == 1){$name=$value; $show='';}

else{$show="$name $value";}

      if($show!=''){echo "$show<br>";}

$i++;

}

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Do this. That way if you decide to change the names in your form widgets it will still work.

 

$i=1;

foreach($array as $key => $value)

{

if($i % 2 == 1){$name=$value; $show='';}

else{$show="$name $value";}

      if($show!=''){echo "$show

";}

$i++;

}

 

 

HTH

Teamatomic

 

Huh?

Link to comment
Share on other sites

Mostly the issue I have is how to get the values from each item and build a complete Name & Email for each person from 2 separate elements.

 

Not huh. At least not for me. Run it and see if what you get is not EXACTLY what was asked for.

 


<?php
$array=array
(
    name1 => myname1,
    email1 => 'email1@email.com',
    name2 => myname2,
    email2 => 'email2@email.com',
    name3 => myname3,
    email3 => 'email3@email.com',
    name4 => myname4,
    email4 => 'email4@email.com'

);
  $i=1;
foreach($array as $key => $value)
{
if($i % 2 == 1){$name=$value; $show='';}
else{$show="$name $value";}
       if($show!=''){echo "$show<br>";}
$i++;
}
?>

 

if you need an explanation of modulus and whats happening with the code I'll be happy to explain it to you.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Thanks for the help, I'll give those both a try. Any good walk thru either book or tutorial on array's and iteration you can recommend? Most of the books (at least the ones I have) don't seem to get to clear into explaining the foreach loop.

Link to comment
Share on other sites

An array element is composed of two parts, a key and a value.

 

array(

'key'->'value',

key->value,

key->'this is value',

'this is value-the key is 3');

 

A foreach loop is exactly what it says, foreach element in the array. As of PHP5 you can either loop through the array iterating the values ($array as $value) or as ($array as $key=>$value) iterating both the key and value.

 

To see what you need to do to an array to get what you want out of it

<pre>

print_r($array);

</pre>

 

This will give you a pretty printed array vs the run-on one normally given, or you can look at the source or the file.

 

Most book dont really go into depth cause if you've seen one you've seen the basics of all of them. If you gogle you can find a few php book online, and many good tutorials.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Mostly the issue I have is how to get the values from each item and build a complete Name & Email for each person from 2 separate elements.

 

Not huh. At least not for me. Run it and see if what you get is not EXACTLY what was asked for.

 


$array=array
(
    name1 => myname1,
    email1 => 'email1@email.com',
    name2 => myname2,
    email2 => 'email2@email.com',
    name3 => myname3,
    email3 => 'email3@email.com',
    name4 => myname4,
    email4 => 'email4@email.com'

);
  $i=1;
foreach($array as $key => $value)
{
if($i % 2 == 1){$name=$value; $show='';}
else{$show="$name $value";}
       if($show!=''){echo "$show
";}
$i++;
}
?>

 

if you need an explanation of modulus and whats happening with the code I'll be happy to explain it to you.

HTH

Teamatomic

 

It's great that you're trying to be helpful, but in the future it would be much more helpful if you used the bbcode code or php around your code.

 

2nd, your code makes the assumption that there will be nothing else in the $_POST other than the columns presented in the example.  Then you simply echo it out, which in no way helps the OP solve his problem, which was how he would associate each name/email pair for further processing.  If you're going to throw something abstract out, I think you need to provide a bit of explanation. 

Link to comment
Share on other sites

for the record (and future form design), it would have been far more straightforward to process these values the way you wanted if you used an array key in the name, rather than an iterative variable tacked onto the end of your name:

 

<td align="right">Name:</td>
<td align="left"><input type="text" name="name[1]" value="" size="25" /></td>
<td align="right">Email:</td>
<td align="left"><input type="text" name="email[1]" value="" size="25" /></td>

 

what this allows you to do is iterate through one of the $_POST arrays, and use the key to identify its partner:

 

$paired_data = array();
foreach ($_POST['name'] AS $key => $this_name)
{
  $this_email = $_POST['email'][$key];
  $paired_data[$key] = array('name' => $this_name, 'email' => $this_email);
}
print_r($paired_data);

 

the php manual has some pretty good information on arrays here, if you're still quite new to them.

Link to comment
Share on other sites

gizmola:

 

Excuse me, but you didnt do anything more or less than I did, except in a bit more confusing manner for a newbie. I did exactly what he asked for, took his example, like you, and did  it in a way that was a bit more portable. I also feel it would be much easier for a newbie to work with what I did and use/alter the value of $show as he wants than to loop through another array that he does not understand.

 

The truth is; we are both wrong as the correct answer would be to tell him to delete his files and start over, then provide a few pointers.

 

Anyways, thanks for the advice.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

gizmola:

 

Excuse me, but you didnt do anything more or less than I did, except in a bit more confusing manner for a newbie. I did exactly what he asked for, took his example, like you, and did  it in a way that was a bit more portable. I also feel it would be much easier for a newbie to work with what I did and use/alter the value of $show as he wants than to loop through another array that he does not understand.

 

The truth is; we are both wrong as the correct answer would be to tell him to delete his files and start over, then provide a few pointers.

 

Anyways, thanks for the advice.

 

HTH

Teamatomic

 

I'm going to just let this pass, with the comment that it's a bit presumptive of you to make the statement that my example is "more confusing manner for a newbie" or that your code is more portable.  Let's say that the OP adds a single form element at the top of his form -- perhaps a text input with the name of "subject".  Does your code still work?  My code put the name/email combinations together into an array that could then be sent to a mail() function wrapper. Your example, if ran on the $_POST variable in the original post, would simply echo out the values.  Is that the same thing?  Is echoing it out helpful to his goal of emailing something to the people in the list?

 

So... again, I'm taking into account your overall contributions here, and willingness to help people.  If you will start using the php blocks around your code, I'm sure we'll end up fast friends.  ;)

 

As for being right or wrong, it's all in the eye of the beholder.  I think Atchkin probably provided the ultimate teachable moment for this topic, but with that said, I try not to completely throw out people's code, even if I wouldn't write things that way myself.  In my experience with this forum going on 8 years now, I've found that it doesn't really teach people anything to take something that is workable if not ideal,  rewrite it from scratch, and say: here's your solution.  That type of advice also tends to get ignored, so not only did you waste their time, but you wasted your own.

Link to comment
Share on other sites

Where do you come up with "if he adds another ". There is no other, there will be no other. Go back and read his original post. Its an assignment, he's in some kind of structured class. He stated exactly what the assignment is. He stated exactly what he wanted.

 

<?php
echo "plonk";
?>

 

 

 

Link to comment
Share on other sites

for the record (and future form design), it would have been far more straightforward to process these values the way you wanted if you used an array key in the name, rather than an iterative variable tacked onto the end of your name:

 

<td align="right">Name:</td>
<td align="left"><input type="text" name="name[1]" value="" size="25" /></td>
<td align="right">Email:</td>
<td align="left"><input type="text" name="email[1]" value="" size="25" /></td>

 

 

what this allows you to do is iterate through one of the $_POST arrays, and use the key to identify its partner:

 

$paired_data = array();
foreach ($_POST['name'] AS $key => $this_name)
{
  $this_email = $_POST['email'][$key];
  $paired_data[$key] = array('name' => $this_name, 'email' => $this_email);
}
print_r($paired_data);

 

the php manual has some pretty good information on arrays here, if you're still quite new to them.

 

This makes sense and I will keep for future reference. Thanks for all your replies Teamatomic and Gizmo as well. I went thru both your recommendations and I can understand the logic of both applications and I will be keeping all this for my references.. I have been referring to the PHP manual a lot but it seems to leave something to be desired when dealing with some particulars.. especially arrays for some reason. Books I've gone through also seem to just touch the surface and not get too in depth, maybe its just my thinking that needs to be adjusted as well as more familiarity over time with the concepts. Thanks for the examples, all of them give me an insight as to how people work with PHP and how its more a matter of personalizing how you get the job done.. Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.