Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Can you interpret (break down) this line of code for me?


smartwork

Recommended Posts

Posted

From application_top.php (old version), there is this snippet of code used for updating the shopping cart quantities and deletions of items.

 

if (in_array($_POST['products_id'][$i], (is_array($_POST['cart_delete']) ? $_POST['cart_delete'] : array()))) 

{ $cart->remove($_POST['products_id'][$i]); }

 

I understand the purpose of in_array and is_array, but it's not clear to me how it's used here... mainly the is_array portion. I think the is_array is asking if $_POST['cart_delete'] is an array, and if it is, then the condition to the left of the colon, and if it's not, then the condition to the right of the colon. Is that right? If so, I'm not sure what those two "thens" are actually doing. Can anyone break that down for me? I'm trying to understand what exactly happens when the checkbox for deleting is used (because I have a scenario where the checkbox doesn't delete the item).

Posted

in_array() needs an array for its second argument. Thus, the check to see if $_POST['cart_delete'] is an array. If it is already an array, just use it. Otherwise, supply an empty array so at least the in_array() call doesn't crash and burn.

 

I'm not sure why you're asking about "two thens". It's actually an inline if-then-else: (cond) ? (then-clause) : (else-clause). It returns a value from either the then-clause or the else-clause; never both, never neither.

 

Now, I think you've come across a possible code flaw. What if $_POST['cart_delete'] exists (as a value), but is a scalar rather than an array? In that case, it would be better to turn it into an array: array($_POST['cart_delete']) and use that. If it's empty or not set, then use the empty array. I haven't tried it, but it might be something like

is_array($_POST['cart_delete']) ? $_POST['cart_delete'] : array($_POST['cart_delete'])

Posted

Thanks, Phil, for clarifying the "just use it" or "supply empty array" part of it. I wasn't sure how those were being treated. There's actually more to the code which is:

 

   case 'update_product' : for ($i=0, $n=sizeof($_POST['products_id']); $i<$n; $i++) {       

							if (in_array($_POST['products_id'][$i], (is_array($_POST['cart_delete']) ? $_POST['cart_delete'] : array()))) {
                                 $cart->remove($_POST['products_id'][$i]);

						    } else {

                                 if (PHP_VERSION < 4) {								  
                                   // if PHP3, make correction for lack of multidimensional array.
                                   reset($_POST);

                                   while (list($key, $value) = each($_POST)) {
                                     if (is_array($value)) {
                                       while (list($key2, $value2) = each($value)) {
                                         if (ereg ("(.*)\]\[(.*)", $key2, $var)) {
                                           $id2[$var[1]][$var[2]] = $value2;
                                         }
                                       }
                                     }
                                   }  // end of while loop

                                   $attributes = ($id2[$_POST['products_id'][$i]]) ? $id2[$_POST['products_id'][$i]] : '';
                                 } else {

                                   $attributes = ($_POST['id'][$_POST['products_id'][$i]]) ? $_POST['id'][$_POST['products_id'][$i]] : '';
                                 }

                                 $cart->add_cart($_POST['products_id'][$i], $_POST['cart_quantity'][$i], $attributes, false);
                               }
                             }
                             tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters)));
                             break;

 

I've got one scenario where an item won't delete when checked for removal in the shopping cart. I believe the entry of special characters (&, ", <, > ') being entered into a text option form is causing me a problem when they're converted to the code for the special symbol, so I'm trying to trace it and determining exactly how items are removed from the cart will hopefully help me trace it.

 

Edited to add: In this same scenario, if the item is not checked for removal and the update button on the cart is clicked, the item actually duplicates.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...