barcode查價程式, 每次掃完條碼, 新輸入的商品都是顯示在最後1行, 量多的話在最下面很難看到. 請問要怎樣改為新的顯示在最上面一筆. 謝謝!
程式碼如下 :
<?
// 若barcode有問題,則跳警告訊息
// 邏輯:剛進來,不秀錯誤訊息
// 在原本的form按enter,且 barcode變數為空 (可能為誤按enter) 則秀錯誤訊息
// 在原本的form按enter,且 barcode變數無法找到對應商品(可能為掃錯條碼) 則秀錯誤訊息
if (
(
(
$reenter
&&
$barcode == ''
// 從原程式按 enter,且為空白
)
)
||
(
(
$reenter
&&
count(get_sql("SELECT * FROM Products WHERE Barcode='$barcode'")) == 0
) // 找不到東西
)
) {
if ( ! isset($_REQUEST['cheat'])) {
show_alert('No item found! Please ask store manager or check again. 條碼掃描錯誤,可能掃到非屬系統條碼,或為空白,請重新掃描');
}
}
// 將 barcodes 統計成一個 'barcode' => 'amount' 的陣列
// 例:
// "4712257800116,4712257800116, 4712257800222"
// 轉換成
// Array (
// 4712257800116,2
// 4712257800222,1
// )
// 目的:計算每個商品的個數 (amount)
//
//
$barcodes_ary = explode(',', $barcodes);
$barcodes_hash = array ();
foreach ($barcodes_ary as $b) {
if (array_key_exists($b, $barcodes_hash)) {
$barcodes_hash[$b] += 1;
} else {
$barcodes_hash[$b] = 1;
}
}
// print_r($barcodes_hash);
//
//
//
if ($reenter == 0) {
// 清掉 session 當中 amount 開頭的變數
foreach ($_SESSION as $k => $v) {
if (substr($k,0,6) == 'amount') {
unset($_SESSION[$k]);
}
}
}
foreach ($barcodes_hash as $b => $amount) {
if ($barcodes_ary == array(''))continue; // 第一次進入此程式:不理
$ary = get_sql("SELECT * FROM Products WHERE Barcode='$b' Order by ProductName");
// print_r($ary);
if ( count($ary) == 0 ) {
continue;
} // 找不到對應的資料庫:不理
$pid = $ary[0]['ProductID'];
$pn = $ary[0]['ProductName'];
$unit = $ary[0]['UnitID'];
// 取得單位
$ary2 = get_sql("SELECT UnitName FROM Units WHERE UnitID = '$unit'");
$unit = $ary2[0]['UnitName'];
// $price = $ary[0]['Price'];
?>
<TR>
<TD><? echo "$pn"; ?></TD>
<TD>
<INPUT TYPE=hidden NAME=productid<?=$pid?> VALUE='<?=$pid?>'>
<!-- <INPUT TYPE=text NAME=amount<?=$pid?> SIZE=3 VALUE='<?=$amount?>'> -->
<?
// 製作下拉式選單。依據 config.inc 裡頭設定的變數決定下拉式的內容是一到多少。
$candidates = array();
for($i=1; $i <= DropDownMaxValue; $i++) {
$candidates[]=$i;
}
// echo get_select("amount$pid", $candidates, $amount);
$str = "<SELECT NAME='amount$pid' onchange='transfer(\"amount$pid\", document.form1.amount$pid.value)'>\n";
$has_requested_option = false;
$str2 = '';
foreach ($candidates as $c) {
if (isset($_REQUEST["amount${pid}option$c"])) {
$if_selected = 'SELECTED';
$has_requested_option = true;
$_SESSION["amount$pid"] = $c;
$price = get_range_price($pid, $c);// 既然取得了 amount ,就可以推出 price 來
} else {
$if_selected = '';
}
$str2 .= "<OPTION VALUE='$c' $if_selected> $c\n";
}
if (! $has_requested_option) {
// 再繞一遍,看能不能對到先前留下來的 session 值
$str2 = '';
foreach ($candidates as $c) {
if (isset($_SESSION["amount$pid"]) && $_SESSION["amount$pid"] == $c) {
$if_selected = 'SELECTED';
$_SESSION["amount$pid"] = $c;
$price = get_range_price($pid, $c);// 既然取得了 amount ,就可以推出 price 來
} else {
$if_selected = '';
}
$str2 .= "<OPTION VALUE='$c' $if_selected> $c\n";
}
}
$price = isset($price) ? $price : get_range_price($pid, 1); // 剛近來時
$str2 .= "</SELECT>\n";
$str .= $str2;
echo $str;
?>
</TD>
<!-- <TD><?=$amount?></TD> -->
<TD><?=$unit ?></TD>
<TD><?=$price; ?></TD>
<? unset($price); ?>
</TR>
<? } ?>