0
這裏是我的PHP和HTML代碼,我在最後的計算問題總 當任何項目存在在那個時候它不會影響在公斤唯一入口總數。不能設置默認值0的數組元素
例
如果kgqty 和kgpcs我有值比主總共是具有價值值顯示我但真正的計算是它顯示我在總
PHP代碼
$pendingArray = array();
$qty = 0;
$selectParty = "SELECT *,categorynm FROM item
JOIN category ON category.categoryId = item.categoryId
ORDER BY item.itemId";
$selectPartyRes = mysql_query($selectParty);
while($row = mysql_fetch_array($selectPartyRes))
{
$pendingArray[$row['itemId']]['itemId'] = $row['itemId'];
$pendingArray[$row['itemId']]['itemNm'] = $row['itemNm'];
$pendingArray[$row['itemId']]['qty'] = 0;
$pendingArray[$row['itemId']]['kgqty'] = 0;
$pendingArray[$row['itemId']]['kgpcs'] = 0;
$pendingArray[$row['itemId']]['ing'] = 0;
$slectIssue = "SELECT SUM(qty) AS kgqty
FROM tableorderdetail
JOIN item ON item.itemId = tableorderdetail.itemId
JOIN tableorder ON tableorder.tableorderId = tableorderdetail.tableorderId
WHERE unit = 'KG'
AND tableorderdetail.itemId = ".$row['itemId']."
AND categoryId = 1
AND (status = 'Y' OR status = 'N')
GROUP BY item.itemId";
$slectIssueRes = mysql_query($slectIssue);
while($irow = mysql_fetch_array($slectIssueRes))
{
$pendingArray[$row['itemId']]['tableorderdetailId'] = $irow['tableorderdetailId'];
$pendingArray[$row['itemId']]['kgqty'] += round($irow['kgqty'],2);
}
$slectIssue = "SELECT SUM(qty) AS kgpcs
FROM tableorderdetail
JOIN item ON item.itemId = tableorderdetail.itemId
JOIN tableorder ON tableorder.tableorderId = tableorderdetail.tableorderId
WHERE unit = 'PCS'
AND tableorderdetail.itemId = ".$row['itemId']."
AND categoryId = 1
AND (status = 'Y' OR status = 'N')
GROUP BY item.itemId";
$slectIssueResi = mysql_query($slectIssue);
while($irow = mysql_fetch_array($slectIssueResi))
{
$pendingArray[$row['itemId']]['kgpcs'] += $irow['kgpcs'];
$pendingArray[$row['itemId']]['inKg'] += $irow['kgpcs']/10;
$pendingArray[$row['itemId']]['ing'] = $pendingArray[$row['itemId']]['kgqty']+ $pendingArray[$row['itemId']]['inKg'];
}
$kgQtys += $pendingArray[$row['itemId']]['kgqty'];
$allQty += $pendingArray[$row['itemId']]['ing'];
$kgPcs += $pendingArray[$row['itemId']]['kgpcs'];
HTML代碼
<tr>
<th>Item Name</th>
<th>Qty</th>
<th>Pcs</th>
<th>Totoal </th>
</tr>
{foreach from=$pendingArray item=onerow}
{if $onerow.kgqty gt 0 || $onerow.kgpcs gt 0}
<tr>
<td align="center">{$onerow.itemNm}</td>
<td align="center">{$onerow.kgqty}</td>
<td align="center">{$onerow.kgpcs}</td>
<td align="center">{$onerow.ing}</td>
</tr>
{/if}
{/foreach}
[**請不要在新代碼**中使用'mysql_ *'函數](http://stackoverflow.com/q/12859942)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://php.net/manual/en/function.mysql-connect.php)?學習[*準備的語句*](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli)。 – SmokeDispenser
也,你真的不應該需要把一個查詢執行內部的while循環(非常糟糕的做法)。重新初始化您的初始查詢以獲取您需要的連接數據等。 – DevDonkey
您能否重新解釋問題以解釋您所談論的變量? – DanielM