Question

In a user system we are building, a client is able to sell a number of products that we offer. The number of these products is not too extensive but we may (and plan to add new products often). When a user logs into their administration portal, they have the ability to enable or disable the products they would like to sell from a list of all available products offered.

We have the form built to accept values for 'enabled' products using checkboxes and a checkboxed name="products[]" array. This part all works fine. The issue comes into play when we are trying to build out the best INSERT / UPDATE methods for the data entered. Values would be INSERTED in the event a customer is offered a new product (by us) at a later time, and choose to enable that product. An update would occur by simply checking or unchecking the corresponding product checkbox, and thus enabled or disabling that product.

The INSERT and UPDATE if a product is currently disabled but I cannot come up with an array compare to properly UPDATE the product to 'disabled' if it is currently set to enabled in the DB.

 foreach($_REQUEST['products'] as $productID) {
    if ( array_key_exists($productID, $_SESSION['productsOffered']) && $_SESSION['productsOffered'][$productID]['status'] == 0) {
        echo 'this product would be turned active.';
        echo "<br>";
    }
    elseif ( array_key_exists($productID, $_SESSION['productsOffered']) && $_SESSION['MYADMIN']['products'][$productID]['status'] == 1) {
        echo 'this product would be turned inactive.';
        echo "<br>";
        <b>THIS IS THE LOGIC WHICH DOES NOT WORK</b>
    }
    elseif ( !array_key_exists($productID, $_SESSION['MYADMIN']['products']) ) {
        echo 'insert';
        echo "<br>";
    }
}

As mentioned, the IF and final elseif here work fine but the middle statement, in english: "If the user unchecked a product which was originally set to active, disable that specific productID", is not right.

Any advice given is appreciated.

Update 1: Initial Thought

I believe a method which may work would be to build a new array of all current $_SESSION['productsOffered'] and find out which current products (at time of update) are currently set to active. Then I would be able to compare the new array with the $_REQUEST['products'] array and determine which products had been left out (or essentially unchecked) and then could update the appropriate product.

While I believe this method may work, it seems very sloppy. Update2: This also would have to be done outside of the current foreach request because obviously the deactivated product would not exist in the REQUEST.

Update 3

By request, some sample data from the arrays is as follows (it is very simple data and near 'real'):

$_SESSION['productsOffered'] = array(
    [1] => Array
        (
          [ID] => 1,
          [status] => 1,
          [name] => Product 1,
        )

    [2] => Array
        (
          [ID] => 2,
          [status] => 1,
          [name] => Product 2,
        )

);

$_REQUEST['products'] = array('1','2','3','4','5','6','7');
Was it helpful?

Solution

$_REQUEST['products'] will only contain products that have been marked as checked by the user. So it will not contain products that have not been checked. Which means in the code above, the control will never go to the 2nd part of the code which is what you are trying to do. One approach that you can take here would be to initially disable all the products when the user submits the form. A sample SQL would be:

$sql = "update product_table set disabled='true' where user_id = 'this_user_id'";

then you can iterate through your logic to mark the products that the user did select:

foreach($_REQUEST['products'] as $productID) {
    if ( array_key_exists($productID, $_SESSION['productsOffered']) && $_SESSION['productsOffered'][$productID]['status'] == 0) {
        echo 'this product would be turned active.';
        echo "<br>";
    }
    elseif ( !array_key_exists($productID, $_SESSION['MYADMIN']['products']) ) {
        echo 'insert';
        echo "<br>";
    }
}

This is somewhat similar to the logic that you posted in Update 1: Initial Thought. Some more information like what are your $_SESSION['productsOffered'] array and sample $_REQUEST['products'] array would be helpful.

OTHER TIPS

Do you know about the REPLACE verb in mysql?

Add new if does not exists, update if it does exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top