Solved

Create single variable from while loop

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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…

627 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