Search code examples
phpexplode

explode comma from textbox


Can any one help me to explode last comma?

column name = used_receipt

values comes in column like below...

301,302,303,

Now the problem is when I try to get min and max value from this textbox and click save, the for loop goes into an infinite loop because of last comma.

if(isset($_POST['save'])){
    $used_receipt = $_POST['used_receipt']; 
    $arr = array_filter(explode(",", $used_receipt));

    $min = min($arr);
    $max = max($arr);
    for ($i = $min; $i <= $max; $i++){
        $insertrow = $database->insertRow("INSERT INTO book_return (used_receipt) VALUES (:used_receipt)", 
                array(':used_receipt'=>$i));                
    }
}

Solution

  • rtrim($used_receipt, ',');
    

    This will trim out suffixing comma's.