skip to Main Content

I’ve spent HOURS on this and I can’t figure it out. My while loop below returns the same record (Sub-Total:) three times. Please see the image below for my table structure. Also, this is in an OsCommerce installation, so the “tep_…..” functions are an OsCommerce thing. I don’t believe they’re the problem though because I’ve tried a few other non-OsCommerce solutions here.

$ot_query = tep_db_query("select * from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int) $order_id . "'");
if (tep_db_num_rows($ot_query)) {
    while ($ot = tep_db_fetch_array($ot_query)) {
        $order_total_sql = tep_db_query("select * from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int) $order_id . "'");
        $order_total_result = tep_db_fetch_array($order_total_sql);
        $email_order .= strip_tags($order_total_result['title']) . ' ' . strip_tags($order_total_result['text']) . "n";
    }
}

Table Structure…

orders_total_id   |   orders_id   |   title   |   text   |   value   |   class   |   sort_order

There are three different rows that have the same orders_id, but different orders_total_id’s and of course the other data is different as well.

— UPDATE —

Adding

echo "<pre>".print_r($ot, 1)."</pre>n"; 

under the while ($ot = tep_db_fetch_array($ot_query)) { produced the following…

Array
(
    [orders_total_id] => 4149
    [orders_id] => 1280
    [title] => Sub-Total:
    [text] => $49.99
    [value] => 49.9900
    [class] => ot_subtotal
    [sort_order] => 1
)
Array
(
    [orders_total_id] => 4150
    [orders_id] => 1280
    [title] => Gift Certificates (-) :
    [text] => $37.00
    [value] => 37.0000
    [class] => ot_gv
    [sort_order] => 3
)
Array
(
    [orders_total_id] => 4151
    [orders_id] => 1280
    [title] => Total:
    [text] => $12.99
    [value] => 12.9900
    [class] => ot_total
    [sort_order] => 12
)

3

Answers


  1. You’re not changing $order_id inside the main loop, so your inner query is never changing – you always fetch the same record(s) each time. Shouldn’t you have something like…

    while ($ot = tep_db_fetch_array($ot_query)) {
       $order_id = $od['order_id'];
       $order_total_sql = etc...
       etc...
    }
    

    of course, that doesn’t answer why you’re running the same query twice… You’re selecting the exact same construct ‘outside’ as you are ‘indside’…

    Login or Signup to reply.
  2. You are selecting records that are have same orders_id:

    $ot_query = tep_db_query("select * from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int) $order_id . "'");
    

    As I can see, orders_id is ID of the order and orders_total_id is id of sub-total record. In your example you have:

    Sub-Total: $49.99
    Gift Certificates (-) : $37.00
    

    49.99 – 37.00 = 12.99, so:

    Total: $12.99
    
    Login or Signup to reply.
  3. The problem here is on the line after your while() — the one that sets $order_total_sql. You’re doing a SELECT using just one record — and it’s NOT the one that you’ve stored in $ot from the previous SELECT. You’re basing your output on the wrong data.

    So the answer is … use $ot instead of making a second SELECT.

    $ot_query = tep_db_query("select * from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int) $order_id . "'");
    if (tep_db_num_rows($ot_query)) {
        while ($ot = tep_db_fetch_array($ot_query)) {
            $email_order .= strip_tags($ot['title']) . ' ' . strip_tags($ot['text']) . "n";
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search