How to get value of variable from array?

Bill_B
Bill_B used Ask the Experts™
on
Help please! Here is a small snippet of a shopping cart script that I am trying to add a coupon module to. I am only having one problem with this portion of the script, and the problem is getting the value of    $individualOrders[2] - which is equal to the quantity ordered - so that I can use it to calculate the amount of the coupon. For some reason, the value always comes back as zero, which makes me think that I am not referencing $individualOrders[2] correctly. If I change the line where I set $current_quantity (by doing $current_quantity = $individualOrders[2];) to a constant value, the coupon code multiplies that constant value by $AMOUNT properly and returns a value. So, how do I pull the value of $individualOrders[2] so I can use it?

Thanks in advance! Here's the snippet of code:

 sub check_coupon_validity
{

      
      #######################################################################
      #
      # Compare coupon code and e-mail to valid coupons in database: coupon_tbl
      #
      #######################################################################



      use DBI;
      my $dbh;
      my $EMAIL;      
      my $CODE;
      my $AMOUNT;
      my $CHECK_1;
      my $CHECK_2;
      my $CHECK_3;
      my $CHECK_4;                        
      my $CHECK_5;                        
      my $CHECK_6;                        
      my $COUPON_TYPE;      
      my $MINIMUM_AMOUNT;      
      my $QUANTITY;      
      my $coupon_amount = "0";
    my $valid_coupon_flag = "0";
         my $required_items_flag = "0";
      my $subtotal = param("hiddenSubTotal");      


###################################################
#
# Pull data based on entered coupon code to be used
# in all calculations.
#
###################################################

      my $db = DBI->connect('DBI:mysql:coupon','xxxxx','xxxxx') or bail_out ("Cannot connect to database");

      $dbh = $db->prepare ("SELECT * from coupon_tbl where CODE = '$coupon' LIMIT 1") or bail_out("Cannot prepare query");

      $dbh->execute () or bail_out ("Cannot execute query");


      while(($EMAIL,$CODE,$AMOUNT,$CHECK_1,$CHECK_2,$CHECK_3,$CHECK_4,$CHECK_5,$CHECK_6,$COUPON_TYPE,$MINIMUM_AMOUNT) = $dbh->fetchrow_array)

             {

###################################################
#
# If we're inside of the while loop, we have a set
# of results. Check first to see if coupon type is
# percentage. If so, do a percentage calculation.
#
###################################################
         
        if ($COUPON_TYPE eq "PERCENT") {

                         $coupon_amount = $coupon_amount + (($AMOUNT * $subtotal)/100);
                       $valid_coupon_flag = "1";

            }


###################################################
#
# Next, check to see if discount is based on a
# minimum dollar amount purchase.
#
###################################################

            elsif ($COUPON_TYPE eq "AMOUNT") {

                   my @groupOrder = split /~/, $itemsOrdered; #split up the ordered items
                  my $lengthOfArray = scalar(@groupOrder);
         
                        if ($lengthOfArray != "0") {
                            my $counter = 0;
                                   
                              while ($counter < $lengthOfArray) {        
      
                                    my @individualOrders = split /\|/, @groupOrder[$counter];              

                                    if (uc(@individualOrders[3]) eq uc($CHECK_1)) {

                                         $required_items_flag = "1";

                                  }

                             $counter ++;

                              }

                   
                         }      
 
                   
                   if (($required_items_flag > 0)  && ($subtotal >= $MINIMUM_AMOUNT)) {

                        $coupon_amount = $AMOUNT;
                       $valid_coupon_flag = "1";

                  }

                              
            }


###################################################
#
# Next, check to see if discount is associated  
# with a specific e-mail address.
#
###################################################

            elsif ($COUPON_TYPE eq "EMAIL") {

                   my @groupOrder = split /~/, $itemsOrdered; #split up the ordered items
                  my $lengthOfArray = scalar(@groupOrder);
         
                  $QUANTITY=0;
                                    
                        if ($lengthOfArray != "0") {
                            my $counter = 0;

                              while ($counter < $lengthOfArray) {        
      
                                    my @individualOrders = split /\|/, @groupOrder[$counter];              

                                    my $order_item_quantity = @individualOrders[2];

                                    if (uc(@individualOrders[3]) eq uc($CHECK_1)) {


                                         $required_items_flag = "1";
                                         $QUANTITY = $QUANTITY + $order_item_quantity;
                                       }

                             $counter ++;

                              }
                   
                         }      

###################################################
#
# NOW WE MUST CHECK TO SEE IF THE E-MAIL ADDRESS
# IS VALID
#
###################################################
 
                   
                   if (($required_items_flag > 0)  && ($subtotal >= $MINIMUM_AMOUNT)) {

                        my $count_result;

                        my $dbi = DBI->connect('DBI:mysql:coupon','xxxxx','xxxxx') or bail_out ("Cannot connect to database");
                        $dbi = $dbi->prepare ("SELECT count(*) FROM coupon_tbl where EMAIL = '$clientEmail' and CODE = '$coupon'");
                        $dbi->execute () or bail_out ("Cannot execute query");

                        while ($count_result = $dbi->fetchrow_array) {

                              if ($count_result > 0) {                              

                                    $valid_coupon_flag = "1";
                                    $coupon_amount = $QUANTITY*$AMOUNT;

                             }
                             
                             else {

                                    $coupon_amount = -44;
                                                          
                             }
                        }
                  
                        $dbi->finish();
                                    
                  }



            }

      }

      $dbh->finish();
      $db->disconnect;

###################################################
#
# Check if we've got a valid coupon. If so,
# return the coupon amount and exit.
#
###################################################

      if ($valid_coupon_flag > 0) {

            return $coupon_amount;
                        
      }


###################################################
#
# If we have made it to this point, it means that
# no search results found for this coupon code.
# Therefore, coupon is invalid. Print error page.
#
###################################################

      else {


      ... PRINT ERROR CODE ...


      exit;
      }
      
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
 if (uc(@individualOrders[3]) == uc($CHECK_1)) {
should probably be
  if (uc($individualOrders[3]) eq uc($CHECK_1)) {

Author

Commented:
I thought that as well, but when I change it to eq from ==, everything breaks and it doesn't raise the flag for required items or valid coupon. I'm wondering why that would break everything. It might be indicative of a problem with my @individualOrders or my $CHECK_1 variable. Maybe they were equal on a numeric level, but not on a alphanumeric?
Most Valuable Expert 2014
Top Expert 2015
Commented:
if $individualOrders[3] and $CHECK_1 are ment to be numeric,
why are you doing uc on them?
what kind of values do you have in $groupOrder[$counter]
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
No, you're right about them being alphanumeric. They are in the form of something like this: "AB123" and they check whether a required item is in the cart in order to receive the discount. So, I think you're right about the eq versus == thing. What I don't understand is that it works now, but if I change it to the eq version, everything goes haywire and stops working.

I'm getting the feeling that this has something to do with the way I declare my variables, so I'm going to go back and edit the first message to show the whole subroutine. Maybe that will help.

Thanks!

Author

Commented:
Ok, thanks to your last comment, I realized I had to take a close look at what was being passed to the subroutines. Upon further inspection, I realized that I was not receiving ANY information due to the fact that I wasn't receiving the information for the variable $itemsOrdered at all. So, I've now changed the call for the subroutine to pass that info:


my $coupon_amount = &check_coupon_validity($itemsOrdered,$hiddenSubTotal);
     
and I've then declared $itemsOrdered and $subtotal like this:

$itemsOrdered = $_[0];
$subtotal = $_[1];

Things are working pretty well now. I made a couple of other changes, so if you'd like to see those, let me know. Not sure what the protocol is for posting a whole subroutine again just to show minor changes.

Now if I could just get rid of these apostrophes in the titles of some of the items, I'd be in good shape. For some reason a simple string replace won't work.

I would love to award ozo all of the points for steering me in the correct direction. Thanks ozo!

Author

Commented:
This wasn't quite the final answer, but it steered me in the right direction and was good enough to help me solve it. Thanks ozo!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial