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}
Prashant Bhatt
  • 507
  • 1
  • 6
  • 19
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – Tobi Nary Apr 06 '16 at 09:39
  • 1
    also, you really shouldnt need to put a query execution inside a while loop (horribly bad practice). Reengineer your initial query to get the data you need with joins etc. – DevDonkey Apr 06 '16 at 09:40
  • Could you rephrase the question to explain what variables you're talking about? – DanielM Apr 06 '16 at 09:41
  • what is perfect way for that @DevDonkey – Prashant Bhatt Apr 06 '16 at 09:41
  • `$qty` is never used – DanielM Apr 06 '16 at 09:43
  • Me Edit My Variable @DanielM – Prashant Bhatt Apr 06 '16 at 09:48

1 Answers1

0

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. :)

DanielM
  • 6,380
  • 2
  • 38
  • 57