What would be the best way? Array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bordman
Forum Newbie
Posts: 16
Joined: Thu Jan 26, 2006 8:20 am
Location: RI

What would be the best way? Array?

Post by bordman »

Hi,
I'm new to PHP and I'm not sure of the best way to do this.

Here's what I need to do. I need a quote form (name,state, phone, email) that will use php to send an email.

The form will start with a question: "Number of people?" with text field for the user to enter a number. I will probably limit it to 10.

So, for example if the user enters 3 people and clicks submit, I would basically like to produce this code:

Code: Select all

<form name="form1" method="post" action="">
<p><hr><strong>Person 1</strong><br>
  Name: 
    <input name="name" type="text" id="name">
    <br>
    <br>
  State: 
  <input name="address" type="text" id="address">
  <br>
  <br>
  Phone: 
  <input name="phone" type="text" id="phone">
<br>
<br>
Email: 
  <input name="email" type="text" id="email">
<hr>
</p>
<strong>Person 2</strong><br>
  Name: 
    <input name="name2" type="text" id="name2">
    <br>
    <br>
  State: 
  <input name="address2" type="text" id="address2">
  <br>
  <br>
  Phone: 
  <input name="phone2" type="text" id="phone2">
<br>
<br>
Email: 
  <input name="email2" type="text" id="email2">
<hr>
</p>
<strong>Person 3</strong><br>
  Name: 
    <input name="name3" type="text" id="name3">
    <br>
    <br>
  State: 
  <input name="address3" type="text" id="address3">
  <br>
  <br>
  Phone: 
  <input name="phone3" type="text" id="phone3">
<br>
<br>
Email: 
  <input name="email3" type="text" id="email3">
<hr>
</p>
<input type="submit" name="Submit" value="Submit">
<p>&nbsp; </p>
</form>
And put all 3 people's info in the email as well.(or how ever many the user enters)

Any ideas on the best way to handle this?

Thanks for any Help!!
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Array.

Have the HTML look like this

Code: Select all

<input name="name[]" type="text">
"name" will show up in $_POST as an array and so on.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

To lighten the load of the code later on, you may want to pass the number specified by the user to the form as a hidden field. Then you can use that value instead of counting the resulting post array.
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Everah wrote:To lighten the load of the code later on, you may want to pass the number specified by the user to the form as a hidden field. Then you can use that value instead of counting the resulting post array.
What's there to "lighten the load" ... just foreach
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

EDIT | My last answer was in the wrong thread. I amazed at how stupid I can be sometimes...

What I meant by lightening the load of the code is that since the form will be drawn using a know number passed by the user, it makes sense to pass that number to the form so when the form is posted, it has an idea of how many of each form field was posted. It just made sense to me when it came out of my fingertips. If it is confusing, please disregard.
Last edited by RobertGonzalez on Tue Jun 27, 2006 6:15 pm, edited 1 time in total.
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

edited
Last edited by Robert Plank on Tue Jun 27, 2006 6:16 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

HAH! You're right. I choked on that one. Sorry. I'll edit mine if you edit yours.
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Everah wrote:EDIT | My last answer was in the wrong thread. I amazed at how stupid I can be sometimes...

What I meant by lightening the load of the code is that since the form will be drawn using a know number passed by the user, it makes sense to pass that number to the form so when the form is posted, it has an idea of how many of each form field was posted. It just made sense to me when it came out of my fingertips. If it is confusing, please disregard.
I still don't understand the lighten the load on the code metaphor... are you trying to say for is faster than foreach?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No. What I am saying is that instead of writing code to guess the number of form elements sent, you tell the form. It reduces the amount of code need to process the sent data because you are actually giving the script the same fixed number of fields the user told the script to generate, instead of having the script try to determine the number of posted elements. This is a trivial amount of code savings anyway, but if I can find a savings somewhere, I'll at least at it.
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

I have an idea, if not a great one or perhaps a very simple one.

1. Let the user enter the number of people in a form.

2. Use a for loop, print the 'person n' form n number of times, where n is the number fo people obtained from the previous step. You can use the same 'n' to print 'person 1, person 2...etc' as well as form name[1], name[2]... ie the name and also the corresponding email arrays.

Thus you need not print the form manually in your code for number of times, the for loop will take care of that. But remeber to put the form code (ie the form for person1, etc..) as a function and call it within the for loop to print it.

hope you got our ideas right ?!
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Everah wrote:No. What I am saying is that instead of writing code to guess the number of form elements sent, you tell the form. It reduces the amount of code need to process the sent data because you are actually giving the script the same fixed number of fields the user told the script to generate, instead of having the script try to determine the number of posted elements. This is a trivial amount of code savings anyway, but if I can find a savings somewhere, I'll at least at it.
There is nothing to guess. If you use foreach you don't even have to know the number of elements in the form. Using foreach is more of a savings than using for because foreach uses iterators instead of random-access.

http://www.robertplank.com/foreach-benchmark.php
Source: http://www.robertplank.com/foreach-benchmark.phps

foreach was 20 times faster than for when I ran that.
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

Sorry if this is offtopic :

Have you tried Javascript to do this? I think it is much easier and more user friendly.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Robert Plank wrote:
Everah wrote:No. What I am saying is that instead of writing code to guess the number of form elements sent, you tell the form. It reduces the amount of code need to process the sent data because you are actually giving the script the same fixed number of fields the user told the script to generate, instead of having the script try to determine the number of posted elements. This is a trivial amount of code savings anyway, but if I can find a savings somewhere, I'll at least at it.
There is nothing to guess. If you use foreach you don't even have to know the number of elements in the form. Using foreach is more of a savings than using for because foreach uses iterators instead of random-access.

http://www.robertplank.com/foreach-benchmark.php
Source: http://www.robertplank.com/foreach-benchmark.phps

foreach was 20 times faster than for when I ran that.
OK.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I didn't read the thread all of the way through, but I saw 'arrays' and 'send the number of indicies' or something.

You should write code that can support a dynamic number of indices just so you can expand your system later. (if you are using a static number of indices)
bordman
Forum Newbie
Posts: 16
Joined: Thu Jan 26, 2006 8:20 am
Location: RI

Thanks Everyone!

Post by bordman »

This is what I came up with, which seems to do just fine, except for the email part which I haven't done yet.

Code: Select all

<? 
$numowner = $_POST['numowner'];
$counter = 1 ;

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form2" method="post" action="">
Select # of Business Owners: 
  <select name="numowner" id="numowner">
  <option value="1" selected>1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  </select>
  <input type="submit" name="Submit2" value="Submit">
</form>
<form name="form1" method="post" action="">
<? 
if ($numowner <> "") {
while ( $counter <= $numowner ) { ?>

<p><hr>
<strong>Owner <? echo $counter ; ?></strong><br>
  Name: 
    <input name="name<? echo $counter ; ?>" type="text" id="name<? echo $counter ; ?>">
    <br>
    <br>
  State: 
  <input name="address<? echo $counter ; ?>" type="text" id="address<? echo $counter ; ?>">
  <br>
  <br>
  Phone: 
  <input name="phone<? echo $counter ; ?>" type="text" id="phone<? echo $counter ; ?>">
<br>
<br>
Email: 
  <input name="email<? echo $counter ; ?>" type="text" id="email<? echo $counter ; ?>">
  <br>
  <br>
  
<p>&nbsp;</p>
<?
$counter = $counter + 1;
}
}
?>
<? 
if ($numowner <> "") {
?>
<hr>
<input type="submit" name="Submit" value="Submit">
<?
}
?>
</form>
</body>
</html>
Thanks for all your help!
Post Reply