Jump to content

Add New Item Button... Opens underneath and counts... blah.


Recommended Posts

Ok... this is going to be a hairy explanation and I'll apologize for that before I begin. Ok, so what I have: Not much... I have a simple form, in the first td I have an input box asking for the number of items. In the second td I have four input boxes; brand, model, price, quantity. And in the third td I have a submit button. Once the information is submitted it jumps to a php script that processes the number of items, the pricing, and the quantity and echoes a nice little summary of pricing and fees. Ok, not so complex right? Here's where it gets nasty... I would like another button placed somewhere in the form which will add another item. When it adds the next item, I need all the same information as the first item, but with each input having a unique name (so I can pop item #2's information into their own variables and do some math with item #1's data. Additionally, I would like to have a maximum of 10 items total and for the submit/clear buttons to be moved to the bottom. For better understanding, here is the exact coding I am currently using:

 

Form Code:

<table border="0" cellpadding="0" cellspacing="0">
<td>
<tr>

<form name="individualform" method="post" action="price.php">
</tr></td>
<tr>
<td>
<font size="4">Item #1</font><br>
Brand:<br>
<input name="brand1" type="int" id="brand1" size="26"><p>
Model:<br>
<input name="model1" type="int" id="model1" size="26"><p>
Price:<br>
<input name="price1" type="int" id="price1" size="5"><p>
Quantity:<br>

<input name="quantity1" type="int" id="quantity1" size="5"><p>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="submit" />
<input type="reset" name="submit2" value="reset" />
</tr></td>
</table>

So an example of the add new item maybe change the values to Item#2, brand2, model2, ect...

 

And here's the php it references:

<?php
$items =$_POST['items'];
$price1 =$_POST['price1'];
$quantity1 = $_POST['quantity1'];
$price2 =$_POST['price2'];
$quantity2 = $_POST['quantity2'];
$price3 =$_POST['price3'];
$quantity3 = $_POST['quantity3'];
$price4 =$_POST['price4'];
$quantity4 = $_POST['quantity4'];
$price5 =$_POST['price5'];
$quantity5 = $_POST['quantity5'];
$price6 =$_POST['price6'];
$quantity6 = $_POST['quantity6'];
$price7 =$_POST['price7'];
$quantity7 = $_POST['quantity7'];
$price8 =$_POST['price8'];
$quantity8 = $_POST['quantity8'];
$price9 =$_POST['price9'];
$quantity9 = $_POST['quantity9'];
$price10 =$_POST['price10'];
$quantity10 = $_POST['quantity10'];


/*POST PRICE BY NUMBER OF INDIVIDUAL ITEMS*/
if (($items >= 1)&&($items <= 4)){
$posting=.75;
}elseif (($items > 4)&&($items < 10)){
$posting=.50;
}elseif ($items ==10){
$posting=.25;
}

/*SETS DISCOUNT FOR POSTING*/
$postprice = $posting * $items;


/*REST*/
$totalrequestedprice1 = $price1 * $quantity1;
$totalrequestedprice2 = $price2 * $quantity2;
$totalrequestedprice3 = $price3 * $quantity3;
$totalrequestedprice4 = $price4 * $quantity4;
$totalrequestedprice5 = $price5 * $quantity5;
$totalrequestedprice6 = $price6 * $quantity6;
$totalrequestedprice7 = $price7 * $quantity7;
$totalrequestedprice8 = $price8 * $quantity8;
$totalrequestedprice9 = $price9 * $quantity9;
$totalrequestedprice10 = $price10 * $quantity10;
$totalprice = $totalrequestedprice1 + $totalrequestedprice2 + $totalrequestedprice3 + $totalrequestedprice4 + $totalrequestedprice5 + $totalrequestedprice6 + $totalrequestedprice7 + $totalrequestedprice8 + $totalrequestedprice9 + $totalrequestedprice10;

$fee = .1;
$totalcommission = $totalprice * $fee;

$totalprofit =$totalprice - $postprice - $totalcommission;
$totalpostfee = money_format("$%i USD", $postprice);
$yourtotalprofit = money_format("$%i USD", $totalprofit);
$ourcommission = money_format("$%i USD", $totalcommission);
$yourtotalprice = money_format("$%i USD", $totalprice);
echo "Your profit before fees: $yourtotalprice.<br>Our posting fees: $totalpostfee.<br>Our services commission: $ourcommission.<br>Your total profit: $yourtotalprofit.";

?>

Before I get yelled at, let me just say that I am a php nublet. I had visual basic maybe two years ago... so it's kinda similar... I know just enough to get me in trouble. If there is a better way of doing this (one I can understand), please let me know.

There certainly is. Have you worked with arrays before? They're basically variables that can hold multiple bits of data, in a set structure. You can use loops to loop through each item in the array and, in your case, works out the pricing totals. You've probably worked with them without realising it, as $_POST is an (associative) array. You access differents bits of data in the structure by specifing the key:

 

$_POST['name_of_key'];

 

You can define a HTML input to post the data as an array, by adding "[]" after the name:

 

Brand:<br>
<input name="brands[]" type="int" id="brand1" size="26"><p>

 

So a quick example of how you can loop through each bit of data in the array:

 

$brands = $_POST['brands'];

foreach ($brands as $brand)
{
    // Do something with $brand
}

 

As you have multiple inputs though you'll need to do it slightly different. Fortunately browsers post input values even if they're blank, so that your structure of post data doesn't alter from what you're expecting. That means you can reliably use the key from one array, to access the same item's value within another array:

 

$brands = $_POST['brands'];
$models = $_POST['models'];
$prices = $_POST['prices'];
$quantities = $_POST['quantities'];

foreach ($brands as $key => $brand)
{
    // Do something with $brand
    // Or $models[$key]
    // Or $prcies[$key]
    // Or $quantities[$key]
}

 

It also removes the need to manually track the number of items, as you can just use count on one of the arrays.

 

One last thing; you should always check first that a key exists within an array before you try to access it (otherwise you will get a PHP notice). For this you have isset and empty, which are extremely useful functions. Combined with the ternary syntax, you can easily check that an input exists in one line:

 

$brands = !empty($_POST['brands']) ? $_POST['brands'] : array();

Ok, I kind of understand, but that only serves to clean up my math right? And also to add a count function? But, one of the things I am really after is a way to only show one item form on my page and then allow the user to add an item as they need it. Let me see if I can make this more clear.

 

What the website is: It's like a giant yardsale. If someone has something they want to sell they can come to us and we will post it on our website (or on eBay) for them. It's a safer alternative to Craigslist or other classifieds because they will not need to put their personal information. It also allows them to reach a broader range of buyers because it is online and will allow buyers to pay with more than just cash.

 

So when someone wants to sell something of theirs they can come to the website and use this form to enter in the item they want to sell. I want them to be able to put in up to 10 items and once they submit the form echo a print page with all the information. They can then bring this to me along with their items as a receipt.

 

On my html page I have just one form for 1 Item's information: Item #1, Brand, Model, Price, and Quantity.

I want the user to be able to click a button which will dynamically add a second form with item 2's information: Item#2 Brand, Model, Price, and Quantity.

 

Since I charge for every item I post on my website, and give discounts after 5 and 10 items I will need to keep track of how many unique items they are posting.

 

As well, since I would like the form to echo each individual item in a new page, each dynamically created form will need the inputs to be unique.

Ok, nevermind that last post. I found a javascript that allows me to do exactly what I want. But now I have a math question for php. Ok, so my price and quantity fields are stored into their own array.

 

Example would be: Price Array (3, 4, 5, 6) and Quantity Array (1, 2, 3, 4).

 

What I need to have happen here is the first numbers in each array to multiply, then the second, then the third, then the fourth. The results from each need to then add together to get the total.

 

Example: Price Array (3, 4, 5, 6) - Quantity Array (1, 2, 3, 4)

So then: 3*1=3, 4*2=8, 5*3=15, 4*6=24. Then 3+8+15+24= 50 So then the total equals 50.

 

:wtf: How do I do this?

You should store them as a multi-dimensional array:

 

$items = array(
    array(
        'price' => 3,
        'quantity' => 1,
    ),
    array(
        'price' => 4,
        'quantity' => 2,
    ),
    array(
        'price' => 5,
        'quantity' => 3,
    ),
    array(
        'price' => 6,
        'quantity' => 4,
    )
);

$total = 0;

foreach ($items as $item)
{
    $total += $item['price'] * $item['quantity'];
}

echo $total;

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.