By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Using while and foreach

P: n/a
Hi there ;)

Im building a MySQL table for a menssaging system, its a table that has 3
fields de UserID, MTime and Mess.
My problem is that i cant find a way that using only a mysql query i get the
time and the message, and that for each row.

Basicly the code would look like this, to give an idea...

<?php
$News=mysql_query("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
ORDER BY MTime DESC");
while ($line = mysql_fetch_array($News, MYSQL_ASSOC)) {
foreach ($line AS $Date) {
print "\t\t<p><b>$Date</b></p><p>$Mess</p>\n";
}
}
?>

Any ideas how to do it ?

The main look of the thing is to have the Time in bold followed by the
message, and that for each row.

Thx

Marco
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Marco wrote:
Im building a MySQL table for a menssaging system, its a table that has 3
fields de UserID, MTime and Mess.
My problem is that i cant find a way that using only a mysql query i get the
time and the message, and that for each row.

Basicly the code would look like this, to give an idea...
(slightly edited)

<?php
$News=mysql_query("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
ORDER BY MTime DESC");
while ($line = mysql_fetch_array($News, MYSQL_ASSOC)) {
# foreach ($line AS $Date) {
print "\t\t<p><b>{$line['MTime']}</b></p><p>{$line['Mess']}</p>\n";
# }
}
?>

Any ideas how to do it ?


mysql_fetch_array() [as the name suggests] returns an array; so your
$line variable will be something like

$line['MTime'] = '2004-02-03';
$line['Mess'] = 'Message text';

When you do the foreach() to this array, it will loop twice. The first
time $Date will have '2004-02-03' and the second time it will have
'Message text'. Doing it with the foreach gives you no control over each
of the isolated elements; use them directly from the $line array.
Hope this helps.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
Marco wrote:

Hi there ;)

Im building a MySQL table for a menssaging system, its a table that has 3
fields de UserID, MTime and Mess.
My problem is that i cant find a way that using only a mysql query i get the
time and the message, and that for each row.

Basicly the code would look like this, to give an idea...

<?php
$News=mysql_query("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
ORDER BY MTime DESC");
while ($line = mysql_fetch_array($News, MYSQL_ASSOC)) {
foreach ($line AS $Date) {
print "\t\t<p><b>$Date</b></p><p>$Mess</p>\n";
}
}
?>


I think this should work:

while ($line = mysql_fetch_array($News, MYSQL_ASSOC)) {
print "\t\t<p><b>".$line['MTime']."</b></p><p>".$line['Mess']."</p>\n";
}

You might want to use htmlentities(), at least on the Mess.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #3

P: n/a

"Pedro Graca" <he****@hotpop.com> wrote in message
news:bv************@ID-203069.news.uni-berlin.de...
Marco wrote:
Im building a MySQL table for a menssaging system, its a table that has 3 fields de UserID, MTime and Mess.
My problem is that i cant find a way that using only a mysql query i get the time and the message, and that for each row.

Basicly the code would look like this, to give an idea...


(slightly edited)

<?php
$News=mysql_query("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
ORDER BY MTime DESC");
while ($line = mysql_fetch_array($News, MYSQL_ASSOC)) {
# foreach ($line AS $Date) {
print "\t\t<p><b>{$line['MTime']}</b></p><p>{$line['Mess']}</p>\n";
# }
}
?>

Any ideas how to do it ?


mysql_fetch_array() [as the name suggests] returns an array; so your
$line variable will be something like

$line['MTime'] = '2004-02-03';
$line['Mess'] = 'Message text';

When you do the foreach() to this array, it will loop twice. The first
time $Date will have '2004-02-03' and the second time it will have
'Message text'. Doing it with the foreach gives you no control over each
of the isolated elements; use them directly from the $line array.
Hope this helps.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--


Indeed i noticed it did 2 times the loop ,when i tryed to use it the first
time.

Thanks for your insights.
Jul 17 '05 #4

P: n/a

"Shawn Wilson" <sh***@glassgiant.com> wrote in message
news:40***************@glassgiant.com...
Marco wrote:

Hi there ;)

Im building a MySQL table for a menssaging system, its a table that has 3 fields de UserID, MTime and Mess.
My problem is that i cant find a way that using only a mysql query i get the time and the message, and that for each row.

Basicly the code would look like this, to give an idea...

<?php
$News=mysql_query("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID' ORDER BY MTime DESC");
while ($line = mysql_fetch_array($News, MYSQL_ASSOC)) {
foreach ($line AS $Date) {
print "\t\t<p><b>$Date</b></p><p>$Mess</p>\n";
}
}
?>


I think this should work:

while ($line = mysql_fetch_array($News, MYSQL_ASSOC)) {
print "\t\t<p><b>".$line['MTime']."</b></p><p>".$line['Mess']."</p>\n";
}

You might want to use htmlentities(), at least on the Mess.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com


It works great even with out using the htmlentities(), im going to see what
htmlentities do in the manual, just in case i need it or if its better to
use it.

Thanks
Marco
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.