0

I have a rebate system. The user will check to see if their product qualifies for a rebate by entering the model number, serial number, dealer name, and purchase date.

If the product qualifies, The values are added to the session array.

array(4) { 

[0]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "CP05G10" ["serial"]=> string(9) "ABCK50786" ["pdate"]=> string(10) "2013-05-18" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

[1]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "EP24G33" ["serial"]=> string(9) "AAMK01127" ["pdate"]=> string(10) "2013-05-18" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

[2]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "CP05G10" ["serial"]=> string(9) "ABCK50786" ["pdate"]=> string(10) "2013-05-17" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

[3]=> array(6) { ["id"]=> string(0) "" ["model"]=> string(7) "EP24G33" ["serial"]=> string(9) "AAMK01127" ["pdate"]=> string(10) "2013-05-17" ["amt"]=> string(2) "25" ["dealer"]=> string(8) "79163500" } 

}  

The problem I am having is users are adding the same model numbers and serial numbers but a different purchase date or dealer. Which creates duplicates in the system.

What I am needing is to remove duplicates based on the model numbers and serial numbers.

I have implemented the suggestion:

    //echo $amount;
        if ($amount != ''){
            $rebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date, "amt" => $amount, "dealer" => $dealerid);
        }else{
            $noRebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date);
        }

    if( isset($_SESSION['rebate']) ){
       foreach($_SESSION['rebate'] as $session )
        { 
           if($session['serial'] != $serial_number && $session['model'] != $model_number){
               $_SESSION['rebate'] = $rebate;
               $_SESSION['noRebate'] = $noRebate;
            }
        }
    }else{
        $_SESSION['rebate']   = $rebate;
        $_SESSION['noRebate'] = $noRebate;
    }

I re-implemented the suggested code and all is working.

        // check to see if model and serial already exist in Session
        if( isset($_SESSION['rebate'])){

           $match_rebate = 0;
           foreach($_SESSION['rebate'] as $session )
            {  
               if($session['serial'] == $serial_number && $session['model'] == $model_number){ // if session matches what is being submitted
                   $match_rebate = 1;
                }
            }
        }else{
            $_SESSION['rebate']   = $rebate;
            $_SESSION['noRebate'] = $noRebate;
        }

        if($match_rebate == 0)
        {
          $_SESSION['rebate']   = $rebate;
          $_SESSION['noRebate'] = $noRebate;
        } 
3
  • 3
    Mother of formatting. Commented May 18, 2013 at 10:18
  • @ChrisCooney: Editing instead of placing a witty comment would have been more helpful. Commented May 18, 2013 at 14:12
  • @MadaraUchiha It was edited soon after I posted. Commented May 18, 2013 at 17:12

1 Answer 1

1

Instead of removing Duplicates based on the model numbers and serial numbers, check whether the model number and serial number exists in the session array before creating the sessions. If found you can avoid creating the session or else create one. Tweaking your code

if ($amount != ''){
        $rebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date, "amt" => $amount, "dealer" => $dealerid);
    }else{
        $noRebate[] = $tmprebate = array("id" => '', "model" => $model_number, "serial" => $serial_number, "pdate" => $purchase_date);
    }

if( isset($_SESSION['rebate'])){

   $match_rebate = 0;
   foreach($_SESSION['rebate'] as $session )
    {  
       if($session['serial'] == $serial_number && $session['model'] == $model_number){
           $match_rebate = 1;
        }
    }
}else{
    $_SESSION['rebate']   = $rebate;
}
if( isset($_SESSION['norebate'])){

   $match_norebate = 0;
   foreach($_SESSION['norebate'] as $session2 )
    {  
       if($session2['serial'] == $serial_number && $session2['model'] == $model_number){
           $match_norebate = 1;
        }
    }
}else{
    $_SESSION['noRebate'] = $noRebate;
}

if($match_rebate == 0)
{
  array_push($_SESSION['rebate'],$rebate);
}    

if($match_norebate == 0)
{
  array_push($_SESSION['norebate'],$noRebate);
}
Sign up to request clarification or add additional context in comments.

15 Comments

@user2091928: Instead of editing my post, update your question with your solution or comment under my post. I reviewed your code and I know where the problem lies. I will be able to answer if you have it updated in your question or in comment.
I have implemented the above recommendation and it doesn't seem to be working.
Please post your implemented code again so that everyone can view.
Just remove else{ $_SESSION['rebate'] = $rebate; $_SESSION['noRebate'] = $noRebate; } suggested in your edit. That should do the trick
When after I removed the else if Session is NULL nothing gets added.
|

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.