fezzik Posted November 10, 2006 Share Posted November 10, 2006 I need some help with creating a method to manage information from a form. The form has 2 input fields. The 1st field accepts a number similar to a sku or product code. The 2nd field accepts a number as a quantity.I think using a multidimensional array is a possible solution to store this data but I'm uncertain as to how to create the array and give it the desired functionality.One of the issues I'm trying to work out is that the form needs to allow the user to input the same product code multiple times. Since there is a count associated with each product code I would like matching product codes to have their count updated rather than being recorded as new entries. So there needs to be some sort of validation to see if the product code has already been entered.Sample data is as follows:Product Code(count)-------------------111222333(2)444555666(1)777888999(3)111222333(2)I would like to store the data in the array as:Product Code(count)-------------------111222333(4)444555666(1)777888999(3)Any help is appreciated. If you need more information please let me know.A friend has helped by creating a untested ASP script that I'm trying to convert to PHP and test but I have been unsuccessful. [code]<%CONST COL_PNUM = 0CONST COL_QTY = 1Dim productsSub AddProduct( prodnum ) Dim row products = Session("Products") If Not IsArray(products) Then ReDim products( 1, 10 ) ' arbitrary size For row = 0 To 10 : products(COL_QTY,row) = 0 : Next End If ' See if we already have a product by that number For row = 0 To UBound(products,2) If products( COL_PNUM, row ) = prodnum Then ' found match...bump quantity products( COL_QTY, row ) = products( COL_QTY, row ) + 1 Session("products") = products ' save updated array Exit Sub ' and we are done End If Next ' no match found, so find an empty slot in array For row = 0 To UBound(products,2) If products( COL_QTY, row ) = 0 Then products( COL_PNUM, row ) = prodnum products( COL_QTY, row ) = 1 Session("products") = products ' save updated array Exit Sub ' and we are done End If Next ' no empty slot found...expand array newslot = UBDOUND(products,2) + 1 ReDim Preserve products( 1, newslot + 9 ) ' allow extra space ' zap the quantities in the new elements For row = newslot To UBOUND(products,2) : products(COL_QTY,row) = 0 : Next ' use the first slot for our new product products( COL_PNUM, newslot ) = prodnum products( COL_QTY, newslot ) = 1 Session("products") = products ' save updated arrayEnd Sub%> [/code]Cheers, Fezzik Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 10, 2006 Share Posted November 10, 2006 Take a look at [url=http://www.php.net/array_unique]array_unique()[/url] Quote Link to comment Share on other sites More sharing options...
btherl Posted November 10, 2006 Share Posted November 10, 2006 I don't see how array_unique() can be used here..You can structure it like this:$products[$id] = $count;if (isset($products[$id])) { $products[$id] += $another_count;} else { $products[$id] = $another_count;}Then your final array will look like:array( 111222333 => 4, 444555666 => 1, 777888999 => 3); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.