Well, this partial line:
<input name=“pcategories[]”
Shows that you are placing all of your inputs into an array. But, you are forcing them to be UN-INDEXED!
To explain… The pcategories[] array is loaded with values that use an index key. But, the index is just the
standard 0,1,2,3, etc… And, that means there is no way to sequence them to tell them if they are in the same
order that matches the database query. One simple way to fix that would be to place the category into your
input array. So, to fix that you would need to change the way that you are indexing the pcategories array.
Basically, you can not do it the way you are handling it now.
Currently, you have the test user inputs in this format:
Bicycles 320
Auto Parts 135
Bicycles 215
This is done on screen by using a drop-down to capture the first part and a number input. To read this, you
would need to read both the categories from the drop-down’s and the cost numbers. You currently get the
ucost array like this:
I am getting the $ucost array from the cost POST from the form.
$ucost=$_POST['cost'];
As you see, you are only getting numbers and not the matching categories. You would need to handle your
posting in a better way!
Normally, if you have an input form that possibly has duplicated categories or duplicated parts, you would have
a numbered row of input lines. Then, you would use that row number as the array you would pull from the
posted data. In that pulled array, you would have the category used in each row and the cost of it. Then,
you would run the foreach on the numbered rows of input not on the categories themselves. Hope that
makes sense. You can not use a numeric index key without being able to tell one row from another when
the categories are the same. You will need to create a unique row number when you create your inputs.
You currently have no indexing ( <input name=“pcategories[]” ) You would need something in the index and
then assign the value to include both the category and the costs. Then, read in the values into an indexed
ucost array instead of an unindexed ucost array. Then, it is easy to do your calculations. One possible fix for
this process is to number each row. So, you can have a row number, 1,2,3 and assign the categories and the
costs using the row number attached. row#_category, row#_cost. Then read both into your ucost array.
Study on this a bit and ask your next question(s) on it…