www.webdeveloper.com
Results 1 to 5 of 5

Thread: While loop gets same record multiple times

  1. #1
    Join Date
    Jul 2009
    Posts
    40

    While loop gets same record multiple times

    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.

    PHP Code:
    $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";
        }

    See the attached image for the table structure. See how 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.
    Attached Images Attached Images

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    Nothing jumps out at me. I guess the next thing I'd try is sticking in some debug code to see if any pattern emerges that might help indicate where the problem is, e.g.:
    PHP Code:
    $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)) {
            
    // DEBUG //
            
    echo "<pre>".print_r($ot1)."</pre>\n"// or user error_log() instead of echo
            // END DEBUG //
            
    $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";
        }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2009
    Posts
    40

    Debug results

    Good idea NogDog!

    That produced the following...

    PHP Code:
    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

    The problem is that I am getting the line "Sub-Total: $49.99" three times when I run the script. Does the above info. help?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,388
    Huh...I didn't see this before, but don't you just need to do this instead of running the same query again inside the loop?
    PHP Code:
    $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";
        }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2009
    Posts
    40

    Yay!!!

    Fireworks, celebrations, and parades!

    That worked. I kinda suck at loops, so I'm not surprised it was a structure thing.

    Thanks a lot!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles