作者 主題: Array新的顯示在最上面.  (閱讀 2292 次)

0 會員 與 1 訪客 正在閱讀本文。

supermico

  • 可愛的小學生
  • *
  • 文章數: 4
    • 檢視個人資料
Array新的顯示在最上面.
« 於: 2020-05-16 02:14 »
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>
<?              } ?>

twu2

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 5416
  • 性別: 男
    • 檢視個人資料
    • http://blog.teatime.com.tw/1
Re: Array新的顯示在最上面.
« 回覆 #1 於: 2020-05-18 16:44 »
前端怎麼顯示跟後端有什麼關係? 一般都是前面用 js 處理, 要放那邊是 js 的事.
還是你的程式是每次輸入後顯示是整頁都重新載入? 如果是這樣, 要產生什麼資料送到前端, 跟 array 也沒什麼關係.
順序不對就寫程式自己排序再送出啊...