Increment data in codeigniter

Here's my code

    $receipt = $_POST['receipt'];
    $invid = $_POST['invid'];
    // $receipt = intval($receipt);
    // $invid = intval($invid);

    $newreceipt = 0;
    $GTextension = 'GT-';
    $UTextension = 'UT-';
    $rec_type = array(1472,1416,9752,1417,1500);
    $last_receipt = $this->ion_auth->getLastReceipt($receipt);
    if($last_receipt->num_rows() > 0){
        if($receipt == 1){
            $newreceipt = $last_receipt->row()->inv_cno+1;
        }
        elseif($receipt == 2){
            $newreceipt = $last_receipt->row()->inv_cno+1;
        }
        elseif($receipt == 3){
            $newreceipt = $last_receipt->row()->inv_cno+1;
        }
        elseif($receipt == 4){
            $newreceipt = $UTextension.(++$last_receipt->row()->inv_cno);
        }
        else if($receipt == 5){
            $newreceipt = $GTextension.(++$last_receipt->row()->inv_cno);
        }
    }

    $this->ion_auth->update_receipt($invid,$receipt,$newreceipt);

    $return = array('dr_no' => $newreceipt);

    echo json_encode($return);

Now the output of this code is like this if i choose ==4 then it it is like this

UT-1418 it's correct.

then if i choose again ==4 the problem exist because it is now like this UT-UT-1419. It's incrementing also the extension. All i want to increment is the data which is -1419

Can someone help me please. Thank you.

2 answers

  • answered 2018-11-08 09:21 Lorenzo S

    If $receipt === 4 you are executing this:

    $newreceipt = $UTextension.(++$last_receipt->row()->inv_cno);

    and then you are saving the value in the DB with $this->ion_auth->update_receipt($invid,$receipt,$newreceipt);.

    So, now, the database contains "UT-something".

    Now, if you execute this simple code in PHP, you will probably get the point:

    $test = "UT-10";
    ++$test;
    echo "$test\n";
    

    It will output UT-11. You can take a look at some official documentation to understand why this works like this.

    So, what happens is that the next time that $receipt === 4, you perform the same code: $newreceipt = $UTextension.(++$last_receipt->row()->inv_cno);

    that is equivalent to "UT-" . "UT-*something*".

    And so the unwanted result.

    Now, the solution is up to your needs: If inv_cno has to be an integer, you just save the integer and you calculate back the "prefix-" . inv_cno in the code.

    Otherwise, you should just check if the $last_receipt->row()->inv_cno already has a prefix in it, in order to understand if you have to add/change it or not.

    EDIT --- after user comments.

    Now that we now it is a varchar, assuming that the prefix is always a 3 characters string ("UT-", "GT-" and so on), you can create something like this. According to your example, I assume that inv_cno can already have or not have a prefix. And I assume that the admissible prefixes are "UT-" and "GT-".

    I will write you an example here to make you understand my point, then you will have to integrate it in your code :)

    <?php
    
    function get_new_receipt($inv_cno, $extension) {
      $prefix_len = 3;
      $admissible_prefixes = array("UT-", "GT-");
      $inv_cno++;
      if (strlen($inv_cno) <= $prefix_len) {
        return $extension . $inv_cno;
      }
      $current_prefix = substr($inv_cno, 0, $prefix_len);
      if ($current_prefix === $extension) {
        return $inv_cno;
      }
    
      return $extension . substr($inv_cno, $prefix_len);
    }
    
    
    $test = "UT-10";
    
    $test = get_new_receipt($test, "UT-");
    echo "$test\n";
    
    $test = get_new_receipt($test, "UT-");
    echo "$test\n";
    
    $test = get_new_receipt($test, "GT-");
    echo "$test\n";
    

    P.S.: Anyway, as a side note, it's not so safe to perform this kind of math over strings. But that's another story.

  • answered 2018-11-08 10:25 BoonMingProg

    Done it like this

    $prefix = 'UT-'; 
    elseif($receipt == 4){
           if (substr($UTextension, 0, strlen($prefix)) == $prefix) {
                    $UTextension = substr($UTextension, strlen($prefix));
                    $newreceipt = $UTextension. (++$last_receipt->row()->inv_cno);
           }
     }
    

    Thanks Lorenzo S