/    Sign up×
Community /Pin to ProfileBookmark

Do I need a nested while loop for this?

Hello. I’m trying to display a MySQL timestamp in the user’s timezone for each entry in a databse table. BTW, this is OsCommerce, so disregard the weird function names.

This code converts the timestamp for one entry….

[code=php]
$timestamp_from_mysql = tep_db_query(“SELECT UNIX_TIMESTAMP(`date_added`) FROM `”.TABLE_TUTORING_UNITS_COMMENTS.”` WHERE `customers_id` = ‘”.$customer_id.”‘ ORDER BY `date_added`”);
$timezone_from_mysql = tep_db_query(“SELECT `customers_timezone` FROM `”.TABLE_CUSTOMERS.”` WHERE `customers_id` = ‘”.$customer_id.”‘”);

if($timestamp_from_mysql!=false&&$timezone_from_mysql!=false){
$timestamp_from_mysql=tep_db_fetch_array($timestamp_from_mysql);
$timezone_from_mysql=tep_db_fetch_array($timezone_from_mysql);

$timestamp=$timestamp_from_mysql[‘UNIX_TIMESTAMP(`date_added`)’];
$timezone=$timezone_from_mysql[‘customers_timezone’];

$dt_zone=new DateTimeZone($timezone);
$time=date(‘r’,$timestamp);
$dtime=new DateTime($time);
$dtime->setTimeZone($dt_zone);

echo $dtime->format(“M. j, Y a\t g:i a”);
}
[/code]

This is my current code…

[code=php]
$tutoring_comments_query = tep_db_query(“select comments from ” . TABLE_TUTORING_UNITS_COMMENTS . ” where customers_id = ‘” . (int)$customer_id . “‘ order by date_added”);

if (tep_db_num_rows($tutoring_comments_query)) {
while ($tutoring_comments = tep_db_fetch_array($tutoring_comments_query)) {

$timezone_from_mysql = tep_db_query(“SELECT `customers_timezone` FROM `”.TABLE_CUSTOMERS.”` WHERE `customers_id` = ‘”.$customer_id.”‘”);
$timestamp_from_mysql = tep_db_query(“SELECT UNIX_TIMESTAMP(`date_added`) FROM `”.TABLE_TUTORING_UNITS_COMMENTS.”` WHERE `customers_id` = ‘”.$customer_id.”‘ ORDER BY `date_added`”);

if($timestamp_from_mysql!=false&&$timezone_from_mysql!=false){
$timestamp_from_mysql=tep_db_fetch_array($timestamp_from_mysql);
$timezone_from_mysql=tep_db_fetch_array($timezone_from_mysql);

$timestamp=$timestamp_from_mysql[‘UNIX_TIMESTAMP(`date_added`)’];
$timezone=$timezone_from_mysql[‘customers_timezone’];

$dt_zone=new DateTimeZone($timezone);
$time=date(‘r’,$timestamp);
$dtime=new DateTime($time);
$dtime->setTimeZone($dt_zone);
}

echo ‘ <tr>’ . “n” .
‘ <td class=”main” align=”center”>’ . ‘&nbsp;’ . $dtime->format(“M. j, Y a\t g:i a”) . ‘&nbsp;’ . ‘</td>’ . “n” .
‘ <td class=”main” align=”center”>’ . ‘&nbsp;’;
if ($orders_history[‘customer_notified’] == ‘1’) {
echo tep_image(DIR_WS_ICONS . ‘tick.gif’, ICON_TICK) . ‘&nbsp;’ . “</td>n”;
} else {
echo tep_image(DIR_WS_ICONS . ‘cross.gif’, ICON_CROSS) . ‘&nbsp;’ . “</td>n”;
}
echo ‘ <td class=”main”>’ . ‘&nbsp;’ . nl2br(tep_db_output($tutoring_comments[‘comments’])) . ‘&nbsp;</td>’ . “n” .
‘ </tr>’ . “n”;
}
} else {
echo ‘ <tr>’ . “n” .
‘ <td class=”main” colspan=”5″>&nbsp;There are no tutoring comments at this time.</td>’ . ‘&nbsp;’ . “n” .
‘ </tr>’ . “n”;
}
[/code]

Currently, I’m getting several entries so I know the main while loop is working. It’s just that the timestamps that come from that are all displaying the time of the first entry instead of the time for each entry. So, it seems like the second while loop (the one for converting the timezone) is only getting run once.

I’m not married to the nested while structure. If someone has a better idea, please enlighten me.

Thanks for helping!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@gtilflmauthorJul 22.2009 — Any help on this one?

Please?

?
Copy linkTweet thisAlerts:
@DaiLaughingJul 22.2009 — I can't even see a second while loop!

Maybe try writing down the simplified structure with arrows and stuff (whatever works for you) so you can follow the structure without worrying about code.
Copy linkTweet thisAlerts:
@gtilflmauthorJul 22.2009 — My bad on the second while loop. When I first posted, I was playing around with inserting a second while loop and apparently I forgot to copy/paste the code at that point.

I would still like help with how to get the timezone conversion to happen for all entries instead of just the first one.

Any thoughts?
Copy linkTweet thisAlerts:
@DaiLaughingJul 22.2009 — Sorry it's not the way I do things and I think I'm to tired to grasp what you want to happen. Someone with a brain is bound to come along soon!
×

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.11,
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,
)...