Jump to content

Maybe a multidimensional array?


fezzik

Recommended Posts

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 = 0
CONST COL_QTY = 1

Dim products
Sub 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 array
End Sub
%>
[/code]


Cheers, Fezzik
Link to comment
Share on other sites

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
);
Link to comment
Share on other sites

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.