-3

First off, I am a PHP NOOB. I am currently trying to develop a PHP page that is suppose to calculate a monthly budget for you and I am having problem with one section of it.

I am having problem with my food variables. I can't give you the syntax I have for the "foodPerMonth" array because I simply don't know how to write it. Everything I've written so far is wrong.

Anyhow. This is how I want it to work.

I have my html form. I'm making it shorter just for the food part. You are suppose to type in how many i.e "Adult Woman" you want in your budget. And I will have a variable for each of those that will contain the cost per month for each person.

<form action="function.php" method="post">
<label>Adult Woman: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Adult Man: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Juvenile Girl: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Juvenile Boy: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Child Girl: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Child Boy: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Baby Girl: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Baby Boy: </label><input type="text" name="foodPerMonth[]" /><br />
<input type="submit" value="Submit" />
</form>

What I want to know is how do I type this kind of array that will go with the form and etc.

I would also appreciate if someone could thorougly explain how arrays and keys work and how they are used and what they are used for. Doesn't matter how much I read the fact in the manual. It wont stuck.

Thank you in advance for answering, all help will be greatly appreciated.

  • Don't use php.net to learn how to code. Use it for a resource. Search Google for php tutorials, there are a TON available. – Paul Dessert Sep 10 '13 at 23:22
  • 1
    Why an array? This isn't the correct plan for an array really. That `foodPerMonth[]` array syntax is primarily meant for multi-select list boxes. Where you have many items, in a list box, and you want the array to be populated. by selecting items from the list. Also a set of checkbox, might be useful with that syntax. – Zoredache Sep 10 '13 at 23:22
  • Examples: Checkboxes http://stackoverflow.com/questions/14026361/php-multiple-checkbox-array - Multi-select list box. http://stackoverflow.com/questions/4183015/get-all-multiple-listbox-values – Zoredache Sep 10 '13 at 23:27
  • It's suppose to be checkboxes Zoredache. That's why I have foodPerMonth[] And Relentless, I've searched all around to try to find the way I wan't to do this script. I just can't find it. I keep getting directed to stackoverflow. Btw why vote down the question? I mean, it's not a stupid question or anything. – Christoffer Zebran Vik Sep 10 '13 at 23:28
  • That's a fine answer Zoredache, but how does the PHP code look like if I would use that multiple checkbox? – Christoffer Zebran Vik Sep 10 '13 at 23:29
  • @ChristofferZebranVik You could set it up like so `foreach($_POST["foodPerMonth"] as $value) { $foodPerMonth .= "$value\n";}` that will echo each value for your checkboxes without making any changes to your present code. – Funk Forty Niner Sep 10 '13 at 23:37

1 Answers1

0

You're going to want:

<label>Adult Woman: <input type="text" name="foodPerMonth[adultwoman]" /></label><br />
<label>Adult Man: <input type="text" name="foodPerMonth[adultman]" /></label><br />

[note: also wrap the labels around the inputs]

Which will be accessible via:

$_POST['foodPerMonth']['adultman'];
$_POST['foodPerMonth']['adultwomman'];

Just the blank foodPerMonth[] syntax crams everything into a numbered array which skips any fields left blank making the numbering unreliable.

Sammitch
  • 30,782
  • 7
  • 50
  • 77