Solved

Create single variable from while loop

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

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 109

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 109

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
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 count occurrences of each item in an array.

820 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