Solved

Create single variable from while loop

Posted on 2014-09-20
4
133 Views
Last Modified: 2014-09-20
I am trying to place a while loop into a variable that I can use as a single echo. What is happening is that the loop is only displaying the first record.

All db connections are in place and connected. I would appreciate it if someone could point out my error. Many thanks

if (mysql_num_rows($result1) >0) {

        $msgread = "";

        while($row = mysql_fetch_array($result1)) {

            $msgread = "<FONT COLOR='1d99f0'>" . "<b>" . $row['to_user'] . "</b>" . "</font>";
            $msgread .= "<p />";
            $msgread .= date("d/m/Y");
            $msgread .= "<p />";
            $msgread .= $row['message'];
            $msgread .= "<p />";
            $msgread .= $row['from_user'];

            }                               
            $error1 = false;
            }

            if($error1 == 0)  {

            echo $msgread;

            }

Open in new window

0
Comment
Question by:peter-cooper
  • 3
4 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40334289
Try changing line 7 to this (use string concatenation)
$msgread .= "<FONT COLOR='1d99f0'>" . "<b>" . $row['to_user'] . "</b>" . "</font>";

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40334293
That said, please take a moment to learn about the concepts of "valid HTML."  You really want to learn  how to use CSS to style your HTML documents, too.  A hardwired statement like FONT COLOR doesn't have much going for it in the 21st century.

You'll also want to get off MySQL as soon as possible.  PHP is doing away with MySQL support.  The reasons for this, and the alternatives that can help you keep your web site working, are documented in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Closing Comment

by:peter-cooper
ID: 40334342
Thank you very much Ray.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40334367
You're welcome and thanks for the points.  You might find this easier if you can get away from all the fiddly punctuation.  Two things that can help you do that are HEREDOC notation and object notation.  This script would be functionally equivalent to the snippet posted above, and IMHO would be much easier to type and read.

// IF THERE ARE ROWS FROM THE QUERY
if (mysql_num_rows($result1) > 0) 
{
    // SET UP VARIABLES FOR OUR BROWSER OUTPUT
    $msgread = NULL;
    $datenow = date('d/m/Y');

    // RETRIEVE THE ROWS INTO A ROW OBJECT
    while($row = mysql_fetch_obj($result1)) 
    {
        // FORMAT THE OUTPUT WITH HEREDOC NOTATION
        $row = <<<EOD
<FONT COLOR='1d99f0'><b>$row->to_user</b></font>
<p />
$datenow
<p />
$row->message
<p />
$row->from_user
EOD;

        // CONCATENATE THE ROW TO THE OUTPUT DOCUMENT
        $msgread .= $row . PHP_EOL;
    }
    
    // AFTER ALL ROWS ARE FORMATTED
    echo $msgread;
} 

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now