/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=php]
$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”;
}
}
[/code]

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.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 18.2012 — 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.:
[code=php]
$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($ot, 1)."</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";
}
}[/code]
Copy linkTweet thisAlerts:
@gtilflmauthorJul 18.2012 — Good idea NogDog!

That produced the following...

[code=php]
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
)
[/code]


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?
Copy linkTweet thisAlerts:
@NogDogJul 18.2012 — 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?
[code=php]
$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";
}
} [/code]
Copy linkTweet thisAlerts:
@gtilflmauthorJul 18.2012 — Fireworks, celebrations, and parades!

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

Thanks a lot!
×

Success!

Help @gtilflm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...