0

I am doing sum with the below query but it is not giving result properly.

If there are four items and it is showing the result like: 1.000 2.000 3.000 4.000 and it should be like 10.000

![enter image description here

I don't know where I am mistaken please help.

<?php
$order_temp =   mysql_query("select * from temp_cart
where item_id = '".$mitem_idC."'  and ses_mem=113 order by id");

while ($torder = mysql_fetch_array($order_temp)) {
    $prITTC     =   $torder['item_id'];
    $qtyT       =   $torder['qty']; 

    $chTP   =   mysql_query("
    select * from temp_choices
    where item_id = '".$prITTC."'
    AND ses_mem = 113
    AND status = 1
    ");
    while($chGET    =   mysql_fetch_array($chTP)){
    $fID    =   $chGET['id'];
    $field  =   $chGET['choice_id'];

    $order_tempCHP      =   mysql_query("
    select sum(price) as total, id, ename, choice_id, item_id, price
    from choice_price
    WHERE
     id IN('$field')
    ");
    while ($torderCP    = mysql_fetch_assoc($order_tempCHP)){

    $totalCH    =   $torderCP['total'];

    $tsl    = $totalCH+($qtyT*$prIDTC);

$altsl  = number_format($tsl, 3, '.', '');
echo $altsl;
    } }

}
?>
7
  • Can you show the data and the expected result? Also only the query without the php around it would be nice Commented Jul 18, 2016 at 17:50
  • I only read this for like 10 seconds. Try to let the db engine do the work for you. You are making PHP part of the db engine with the loop. (Read: 100 times slower) Commented Jul 18, 2016 at 17:50
  • @Philipp please check i have added image Commented Jul 18, 2016 at 17:56
  • @Drew can you help me and give me the edited code? Commented Jul 18, 2016 at 17:56
  • 1
    Yes I can but I always try to let someone else do it first. Do what @Philipp was talking about (tabular layout with in and out data). And do a show create table xyz output for each table xyz . Usually the only standing between and an answer is what you are not nicely providing. Commented Jul 18, 2016 at 17:57

1 Answer 1

0

according to my question above after trying and trying i found the solution and resolved my problem according to below code:

Thanks to @John Kugelman at MySQL query using an array

    $order_temp =   mysql_query("select * from temp_cart
    where item_id = '".$mitem_idC."'  and ses_mem=113 order by id");
    while ($torder = mysql_fetch_array($order_temp)) {
        $prITTD     =   $torder['id'];
        $prITTC     =   $torder['item_id'];

$chTPaa =   mysql_query("
select choice_id
FROM temp_choices
WHERE item_id = '$prITTC'
AND ses_mem = 113
AND status = 1
group by choice_id
");
while ($chGETaa =   mysql_fetch_assoc($chTPaa)){
$temp[] = $chGETaa['choice_id'];
}
$thelist    =   implode(",",$temp);

    $order_tempCHP      =   mysql_query("
    select sum(price) as total
    from choice_price
    WHERE
     id IN ($thelist)
    AND
        item_id = '".$prITTC."'
    ");
    while($torderCP     = mysql_fetch_assoc($order_tempCHP)){
    $totalCH    =   $torderCP['total'];

    $tsl    = $totalCH+($qtyT*$prIDTC);

$altsl  = number_format($tsl, 3, '.', '');
echo $altsl;
    } }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.