Solved

Create single variable from while loop

Posted on 2014-09-20
4
130 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

18 Experts available now in Live!

Get 1:1 Help Now