Jump to content

Recommended Posts

Good afternoon all!!! Need some help with a function I thought I understood....guess not.

 

So I have a Javascript function that makes the following form every time the user hits the 'add item' button.

 

________  Amount ___

________  Amount ___

________  Amount ___

________  Amount ___

 

The question is, how do I get the item name and amount stored?? I would like to make it so I capture the item he typed in and the amount of that item. That information needs to be stored in one row of the database. So example:

 

Item          Amount 

Apple        10

Orange    15

Donut      12

Pizza        3

 

So the database should have the following:

 

Id, Item, and Amount (1, Apple, 10) (2, Orange, 15) (3, Donut, 12) (4, Pizza, 3)

 

The problem is I don't understand how to set up my foreach loop so each item knows which amount to reference. This is where I am so far

 

foreach ($_POST['items'] as $item) {
        
}

 

Well thats not where I am lol but thats the only part I think I have correct. The rest was just foolishness that I have been playing around with....none of it has worked though. The real issue is the fact that these fields are dynamic. If I could give my Amount array individual keys, I could just set up my foreach like:

 

foreach ($_POST['items'] as $item => $amount) {
        
}

 

And this would tell me which 'amount' is referencing which item because the item name would be the key. But how can I get the item name that the user just typed in?? This is driving me crazy!!! I keep trying different stuff but nothing seems to work. Here is the Javascript and HTML just in case you need it to fool around with:

 

New Form Fields Javascript

<script type="text/javascript">
function add_field(){
var max = 24; // total number of fields, adjust value as needed or for an unlimited number, just remove the test from the logic
var cont = document.getElementById('add_here'); // refer to the div
var numfields = cont.getElementsByTagName("input").length; // get number of input fields in the div
var divNum = 0;
if(numfields < max){
	// create a div element
	var div1 = document.createElement('div');
	// Get template data
	div1.innerHTML = document.getElementById('fieldtpl').innerHTML;
	// append to div, so that template data becomes part of document
	document.getElementById('add_here').appendChild(div1);
	divNum = divNum++;
} else {
	alert("You have reached the maximum number of fields\n that can be added at one time!");
}

}
</script>

New Form Fields HTML

<div id="fieldtpl" style="display:none">
    <div class="resource_form_label">
        <input type="text" name="resource_form_label[]" value="" />
    </div>
   
    <div class="resource_form_quantity">
        Quantity
    </div>
   
    <div class="resource_form_quantity_textfield">
        <input type="text" name="quantity[]" value="" size="3" />
    </div>

</div>

 

Any help with this issue will be greatly appreciated!! Thanks in advance!

 

Bl4ck Maj1k

 

your final form will be POSTing at least 2 arrays:

- resource_form_label[]  .... most likely corresponding to the "item" name  and

- quantity[]

 

in your processing form script just var_dump($_POST) and you will see the posted values... the rest is just a matter of process those arrays to construct your INSERT

your final form will be POSTing at least 2 arrays:

- resource_form_label[]  .... most likely corresponding to the "item" name  and

- quantity[]

 

in your processing form script just var_dump($_POST) and you will see the posted values... the rest is just a matter of process those arrays to construct your INSERT

 

Mikosiko, I think my problem is the syntax. I understand what I need to do in the English language lol....I just can't seem to convert it to PHP. The last thing you say, the "matter of process[ing] those arrays to construct my insert", thats where my issue lies. I cannot seem to grasp my mind around how I will manage to make each item associated with the corresponding amount. I feel so Noob lol.

Code example:

foreach($_POST['resource_from_label'] as $key => $value) {
  $quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0;
if($quantity > 0) {
$arr[] = "('$value','$quantity')";
}
}

$arr_to_string = 'Item, quantity ' . implode(' , ',$arr);

echo $arr_to_string;

Code example:

foreach($_POST['resource_from_label'] as $key => $value) {
  $quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0;
if($quantity > 0) {
$arr[] = "('$value','$quantity')";
}
}

$arr_to_string = 'Item, quantity ' . implode(' , ',$arr);

echo $arr_to_string;

 

I dont mean to be a bother, but do u mind explaining ur code in the english language?? lol I kinda get wat u did but....yea not really. I get it to a certain extent.

I just would like to clarify so I don't look like a leecher or whatever people are calling lazy people these days LOL. Its not that I haven't tested this code. In fact I have played around with several different variations of it just to see its extent. Although I have seen it in action (thanks by the way, works like a charm), I would like to understand what's actually happening here. For example:

 

$quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0;

 

I know this is a true/false statement. But I didnt know you can put this inside of a variable. Also, what is the value of $quantity when you do this sort of statement? And I have never seen $_POST['someValue'][$someVariable] before. I don't understand what thats doing at all. And the other confusion:

 

$arr_to_string = 'Item, quantity ' . implode(' , ',$arr);

 

Is here. The variable arr_to_string is an array obviously with all of the $arr values stored in them. What I don't understand is the 2 single quotes inside the implode statement with the comma separating them. What does that mean? Why is that necessary?

 

So many questions!! I wish I knew one of you PHP gurus personally LOL. Thanks again for the code and assistance guys. It's all really good stuff. Please help me understand it as you guys do.

 

Bl4ck Maj1k

this:

$quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0;

is shorthand for this:

if( !empty($_POST['quantity'][$key]) ){
   $quantity = intval($_POST['quantity'][$key]);
}else{
   $quantity = 0;
}

this:

$quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0;

is shorthand for this:

if( !empty($_POST['quantity'][$key]) ){
   $quantity = intval($_POST['quantity'][$key]);
}else{
   $quantity = 0;
}

 

Thanks WebStyles!!! I totally understand that now...awesome. So now, what does it mean when you do a post function with a bracketed variable next to it??

 

$_POST['someValue'][$someVariable]

Thats the only thing left that I don't quite understand.

$arr_to_string = 'Item, quantity ' . implode(' , ',$arr);

implode basically takes all values of an array and glues them together with thatever text/simbols you want. in this case, they're being joined by [space][comma][space] defined in single quotes, thus: ' , '

 

ok...

this:

$_POST['someValue'][$someVariable];

is basically an array called 'someValue' that was sent over POST method.

 

because POST is an array itself, what you have is an array inside an array.

the [$someVariable] at the end is just the index you're using to access one of the arrays values.

this:

$quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0;

is shorthand for this:

if( !empty($_POST['quantity'][$key]) ){
   $quantity = intval($_POST['quantity'][$key]);
}else{
   $quantity = 0;
}

 

just as complementary information for the OP... this '?:'

$quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0;

is called ternary operator , more info is here

Got it and got it!!!! Thanks again fellas. Whenever I am confused I can ALWAYS depend on this place to keep me on track! You guys are incredibly smart. I can't wait for my PHP fluency to become as good as you all's. Another one bites the dust....SOLVED!

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.