Solved

Create single variable from while loop

Posted on 2014-09-20
4
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 110

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 110

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 110

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

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…
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 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.
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 …

710 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