View Single Post
Old 02-20-2013, 10:52 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You actually want to use arrays for these. You can name them if you want, but if the name's have no actual value by themselves, than a list is what you'll get.
PHP retrieves arrays from html with the following conventions: <input type="text" name="yourname[specificoffset]" /> where you can now find the value in $_POST['yourname']['specificoffset']. If no specific is required, than a simple [] will suffice. 'yourname' is whatever you want to call it.

Then in processing you simply sum them up:
PHP Code:
if (isset($_POST['yourname']))
{
    
$total array_sum($_POST['yourname']);

Or iteratively with a for or foreach loop.
Naming them is useful if you want to display some info for them. You can retrieve that from the key() of the array during iteration, or $key from the foreach:
PHP Code:
foreach ($_POST['yourname'] AS $k => $v)
{
    
printf('Value for %s is %s' PHP_EOL$k$v);

Like that.

This works well with JS as well as you can create the item with the same naming convention and append it to the dom forms.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote