从文本框中分解逗号
explode comma from textbox
谁能帮我把最后一个逗号炸开?
column name = used_receipt
值出现在如下列中...
301,302,303,
现在的问题是,当我尝试从此文本框中获取最小值和最大值并单击“保存”时,由于最后一个逗号,for 循环进入无限循环。
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));
}
}
你犯了两个错误:
1。你的 explode 语句中有一个 space
2。您不从数组中删除空值
因此,要消除这些错误,您可以这样做:
$arr = array_filter(explode(",", $used_receipt));
//^^^^^^^^^^^^ ^ See only a comma without a space
//| To remove empty values from your array
rtrim($used_receipt, ',');
这将 trim 去掉后缀逗号。
谁能帮我把最后一个逗号炸开?
column name = used_receipt
值出现在如下列中...
301,302,303,
现在的问题是,当我尝试从此文本框中获取最小值和最大值并单击“保存”时,由于最后一个逗号,for 循环进入无限循环。
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));
}
}
你犯了两个错误:
1。你的 explode 语句中有一个 space
2。您不从数组中删除空值
因此,要消除这些错误,您可以这样做:
$arr = array_filter(explode(",", $used_receipt));
//^^^^^^^^^^^^ ^ See only a comma without a space
//| To remove empty values from your array
rtrim($used_receipt, ',');
这将 trim 去掉后缀逗号。