0

I am trying to add a promotion section in a shopping cart. It is a temp hack but the code is not working.

/cart.php?promocode=1&productcode=434_red
// WILL ECHO NO


/cart.php?promocode=sale20&productcode=555_red
// WILL ECHO YES BUT NOT WORKING

This is the PHP code, I have broken it all down and I am thinking something to do with my && or in_array ?? Appreciate help.

<?
// PROMO CODE
$promocode=$_GET["promocode"];
$productcode=$_GET["productcode"];

// SALE LIST (ONLY SALE20 WORKS ON THIS)
$saleitemlist=array("555_red, 305_black, 582_elecblue, 593_black");

// PROMO CODE VALIDATION CHECK

// ALLOW EXISTING CODES TO WORK
if($promocode=="chocolate2" || $promocode=="post") {
        $promocode_validate_check = $_POST['promocode']; // APPLY CART FUNCTION
    } else {
        // NOT AN EXISTING CODE - IF SALE20, APPLY THE CODE OTHERWISE DO NOTHING
        if($promocode=="sale20" && in_array($productcode, $saleitemlist)) {
            $promocode_validate_check = $_POST['promocode']; // APPLY CART FUNCTION
            echo "YES"; 
        } else {
            echo "NO";
        }
    }
?>

3 Answers 3

3

it isn't array you want

  $saleitemlist=array("555_red, 305_black, 582_elecblue, 593_black");

try

 $saleitemlist=array("555_red", "305_black", "582_elecblue", "593_black");

its working fine with in_array() check codepad

Sign up to request clarification or add additional context in comments.

5 Comments

How trivial? Let me try that. I thought PHP uses , for Array
yes but you have put all in " so it will behave like string
I get this error now Warning: in_array() expects parameter 2 to be array, null given
@TheBlackBenzKid could you tell me what about you talking its looks fine codepad.org/u7Is4U2s
It is working now. My array was setup with a wrong variable. Thank you so much, repped and marked as an answer friend. Appreciate up reps too.
3
$saleitemlist=array("555_red, 305_black, 582_elecblue, 593_black");

This line means that $saleitemlist is an array that contains just one elements which is "555_red, 305_black, 582_elecblue, 593_black".

If you want that 555_red was one elements, 305_black another and so on. you have to do following:

$saleitemlist=array("555_red", "305_black", "582_elecblue", "593_black");

Comments

0

This one is wrong:

 $saleitemlist=array("555_red, 305_black, 582_elecblue, 593_black");

Right one should be this:

 $saleitemlist=array("555_red", "305_black", "582_elecblue", "593_black");

Each element of array should be wrapped with qutes indivudally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.