Jump to content

Using nl2br() function inside while and echo


Recommended Posts

Hi

I am trying to use the nl2br function like this

while ($row = mysqli_fetch_array($query)) {
//May need this later to output pictures   
//     $imageURL = 'upload/'.rawurlencode($row["filename"]);
  echo
  
   "  <div class='divTableRow'>
<div class='divTableCell'>{$row['User']} ;</div>
<div class='divTableCell'>nl2br({$row['CommentText']});</div>
</div>


    \n";
}

However the output just looks like the attached picture.

When I check in the sql db I can see the line breaks when doing a select * from Table ;

 

image.png.6dc8f81905f12de97a48e47c6babb2bc.png

Link to post
Share on other sites

You cannot place a function call in a string and expect it to be executed - it just becomes part of the string. Instead you need to concatenate.

For example

echo "<div class='divTableCell'>" . nl2br($row['CommentText']) . "</div>";

 

Link to post
Share on other sites

Yes I tried this using single quotes which just outputs it as a string

<div class='divTableCell'> ' . nl2br({$row['CommentText']}) . ';</div>

And if I use double quotes as you've done it causes my page not to load

 

while ($row = mysqli_fetch_array($query)) {
//May need this later to output pictures   
//     $imageURL = 'upload/'.rawurlencode($row["filename"]);
  echo
  
   "  <div class='divTableRow'>
<div class='divTableCell'>{$row['User']} ;</div>
<div class='divTableCell'> " .  nl2br({$row['CommentText']}) . ";</div>
</div>


    \n";
}

 

Link to post
Share on other sites

I also tried declaring the variable ($testvar)  outside the echo statement but the page still won't load even then

 

while ($row = mysqli_fetch_array($query)) {
//May need this later to output pictures   
//     $imageURL = 'upload/'.rawurlencode($row["filename"]);
$testvar = nl2br({$row['CommentText']}) ; 
  echo

  
   "  <div class='divTableRow'>
<div class='divTableCell'>{$row['User']} ;</div>
<div class='divTableCell'>  $testvar ;</div>
</div>


    \n";
}

echo $_SESSION['id'] ; 
?> 

 

Link to post
Share on other sites
$testvar = nl2br({$row['CommentText']}) ;
                 ^                   ^

and remove the curly braces.

Turn on php error reporting, that line above should give

Quote

Parse error: syntax error, unexpected '{' in C:\inetpub\wwwroot\test\noname28.php on line ...

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.