PHP text display problem

Adding linebreaks to concatanated strings

Page 1 of 1

1 Replies - 732 Views - Last Post: 18 October 2009 - 02:47 AM Rate Topic: -----

#1 BlissC  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 27-September 09

PHP text display problem

Posted 17 October 2009 - 04:36 PM

I'm having problems with the display of text being pulled from a database I'm developing. I'm trying to display data from three fields in one table cell, and currently for the table cell I have (for this single cell):

echo "<td valign='top'>" . nl2br( $row['edition']) . nl2br( $row['publisher']) . nl2br( $row['date']) ."</td>";


This displays the data from the three fields in the table cell, but there's no space between the data, or linebreaks, so it's pretty difficult to read. I've tried replacing the n12br's with <br />, and also tried \n, but PHP's not happy with those at all and displays syntax errors.

I wouldn't normally have set the cell up like that, concatanating all the strings - normally I'd simply echo the HTML and variables like so,

echo "<td>$edition<br />$date<br />$publisher</td>\n";


which I've tried to do with this, but it's not playing. Rather than doing it all the long way and coding it all myself I tried to save some time and used a CRUD scaffolding generator, and this is the way it's given the code for each cell:

echo "<td valign='top'>" . nl2br( $row['isbn']) . "</td>";


As I wanted the data from three of the fields in the same cell, I've edited the table slightly and tried to put the three fields in one cell myself, but I've never used n12br() before (didn't know it existed or what it did until a couple of hours ago!). When I try to put all the three fields in one cell though as in my first code example I get a variety of PHP syntax errors, depending on whether I've tried substituting <br />, \n, or tried putting a space or comma between the variables.

Can anyone advise what's the correct way to do this please?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP text display problem

#2 BlissC  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 27-September 09

Re: PHP text display problem

Posted 18 October 2009 - 02:47 AM

Just a quick update for anyone else searching for a solution to a similar problem, after a good night's sleep and checking out the rest of the code generated by the PHP generator to see if I could get any ideas of how to do this I found a solution this morning.

Rather than using the original code which gave me all three fields strung together without spaces or linebreaks:

echo "<td valign='top'>" . nl2br( $row['edition']) . nl2br( $row['publisher']) . nl2br( $row['date']) ."</td>";


I eventually found that this works, and gives me the nice formatted code I want:

echo "<td valign=\"top\">{$row['edition']}<br />{$row['publisher']}<br />{$row['date']}</td>";


Initially I tried $row['edition'] etc. for each field, but this too gave me a PHP error. The solution was to include each in curly braces.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1