Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 03:55 AM
Marco
Guest
 
Posts: n/a
Default Using while and foreach

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





  #2  
Old July 17th, 2005, 03:55 AM
Pedro Graca
Guest
 
Posts: n/a
Default Re: Using while and foreach

Marco wrote:[color=blue]
> 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...[/color]

(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";
# }
}
?>

[color=blue]
> Any ideas how to do it ?[/color]

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 =--
  #3  
Old July 17th, 2005, 03:55 AM
Shawn Wilson
Guest
 
Posts: n/a
Default Re: Using while and foreach

Marco wrote:[color=blue]
>
> 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";
> }
> }
> ?>[/color]

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
shawn@glassgiant.com
http://www.glassgiant.com
  #4  
Old July 17th, 2005, 03:56 AM
Marco
Guest
 
Posts: n/a
Default Re: Using while and foreach


"Pedro Graca" <hexkid@hotpop.com> wrote in message
news:bvogfl$v11su$1@ID-203069.news.uni-berlin.de...[color=blue]
> Marco wrote:[color=green]
> > Im building a MySQL table for a menssaging system, its a table that has[/color][/color]
3[color=blue][color=green]
> > fields de UserID, MTime and Mess.
> > My problem is that i cant find a way that using only a mysql query i get[/color][/color]
the[color=blue][color=green]
> > time and the message, and that for each row.
> >
> > Basicly the code would look like this, to give an idea...[/color]
>
> (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";
> # }
> }
> ?>
>
>[color=green]
> > Any ideas how to do it ?[/color]
>
> 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 =--[/color]

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

Thanks for your insights.


  #5  
Old July 17th, 2005, 03:56 AM
Marco
Guest
 
Posts: n/a
Default Re: Using while and foreach


"Shawn Wilson" <shawn@glassgiant.com> wrote in message
news:401FC518.11934FE3@glassgiant.com...[color=blue]
> Marco wrote:[color=green]
> >
> > Hi there ;)
> >
> > Im building a MySQL table for a menssaging system, its a table that has[/color][/color]
3[color=blue][color=green]
> > fields de UserID, MTime and Mess.
> > My problem is that i cant find a way that using only a mysql query i get[/color][/color]
the[color=blue][color=green]
> > 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 =[/color][/color]
'$UID'[color=blue][color=green]
> > 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";
> > }
> > }
> > ?>[/color]
>
> 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
> shawn@glassgiant.com
> http://www.glassgiant.com[/color]

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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.