Using while and foreach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco

    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_que ry("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
    ORDER BY MTime DESC");
    while ($line = mysql_fetch_arr ay($News, MYSQL_ASSOC)) {
    foreach ($line AS $Date) {
    print "\t\t<p><b>$Dat e</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


  • Pedro Graca

    #2
    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_que ry("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
    ORDER BY MTime DESC");
    while ($line = mysql_fetch_arr ay($News, MYSQL_ASSOC)) {
    # foreach ($line AS $Date) {
    print "\t\t<p><b>{$li ne['MTime']}</b></p><p>{$line['Mess']}</p>\n";
    # }
    }
    ?>

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

    mysql_fetch_arr ay() [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 =--

    Comment

    • Shawn Wilson

      #3
      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_que ry("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
      > ORDER BY MTime DESC");
      > while ($line = mysql_fetch_arr ay($News, MYSQL_ASSOC)) {
      > foreach ($line AS $Date) {
      > print "\t\t<p><b>$Dat e</b></p><p>$Mess</p>\n";
      > }
      > }
      > ?>[/color]

      I think this should work:

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

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

      Shawn
      --
      Shawn Wilson
      shawn@glassgian t.com

      Comment

      • Marco

        #4
        Re: Using while and foreach


        "Pedro Graca" <hexkid@hotpop. com> wrote in message
        news:bvogfl$v11 su$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_que ry("SELECT MTime,Mess FROM Star_Mess WHERE UserID = '$UID'
        > ORDER BY MTime DESC");
        > while ($line = mysql_fetch_arr ay($News, MYSQL_ASSOC)) {
        > # foreach ($line AS $Date) {
        > print "\t\t<p><b>{$li ne['MTime']}</b></p><p>{$line['Mess']}</p>\n";
        > # }
        > }
        > ?>
        >
        >[color=green]
        > > Any ideas how to do it ?[/color]
        >
        > mysql_fetch_arr ay() [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.


        Comment

        • Marco

          #5
          Re: Using while and foreach


          "Shawn Wilson" <shawn@glassgia nt.com> wrote in message
          news:401FC518.1 1934FE3@glassgi ant.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_que ry("SELECT MTime,Mess FROM Star_Mess WHERE UserID =[/color][/color]
          '$UID'[color=blue][color=green]
          > > ORDER BY MTime DESC");
          > > while ($line = mysql_fetch_arr ay($News, MYSQL_ASSOC)) {
          > > foreach ($line AS $Date) {
          > > print "\t\t<p><b>$Dat e</b></p><p>$Mess</p>\n";
          > > }
          > > }
          > > ?>[/color]
          >
          > I think this should work:
          >
          > while ($line = mysql_fetch_arr ay($News, MYSQL_ASSOC)) {
          > print "\t\t<p><b>".$l ine['MTime']."</b></p><p>".$line['Mess']."</p>\n";
          > }
          >
          > You might want to use htmlentities(), at least on the Mess.
          >
          > Shawn
          > --
          > Shawn Wilson
          > shawn@glassgian t.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


          Comment

          Working...