dqz13288 2016-04-06 09:20
浏览 74
已采纳

无法在数组元素中设置默认值0

Here is my php and html code i have problem in final calculation of total When in any item there is only entry in Kg at that time it does not effect in main total.

Example

If Have value value in kgqty 1 and in kgpcs i have value 0 than in main total it's shows me 0 but the real calculation is it's shows me 1 in Total

Php Code

$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 Code

    <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}
  • 写回答

1条回答 默认 最新

  • douyiken0968 2016-04-06 09:57
    关注

    This isn't really an answer to your question as it's not completely clear as to what the question actually is, however, some points to raise:

    1. Your code is quite complicated and doing a lot of things. Consider breaking it down into functions or even class methods to reduce the complexity of each part, helping both grok and test it.
    2. Your variable names (and the use of arrays) obfuscates what's actually going. Making longer variable names isn't going to cost anything, but will help with maintenance.
    3. To help with both of the above, consider using objects defined by classes instead of arrays. They work well with PDO, they'll help segregate your code and help define what's actually happening ($pendingArray is somewhat meaningless at a glance).
    4. As pointed out be @SmokeDispenser, avoid mysql_* functions. See the link in his comment.
    5. As pointed out be @DevMonkey, avoid looping database requests. Requests are incredibly slow, you should be able to return all of the data you need in a single request. Make the DB do as much work for you as possible to save shifting too much data around.

    Regarding the question, you don't seem to have actually asked one (could be a failure to understand on my part). You've said that something is wrong, but it's hard for us to see what.

    Consider rephrasing the question and simplifying your example to the simplest possible thing that demonstrates the problem. This will help you and us understand what the problem is in the first place, avoid us moaning about things like mysql_*, and you might even solve the problem yourself while you do it. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?