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
)
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$otfrom the previous SELECT. You’re basing your output on the wrong data.So the answer is … use
$otinstead of making a second SELECT.