• 0

[PHP/MySQL] Maintaining wordwrap in submitted data


Question

I have a page that queries a database for an essay stored in it. Here's a snippet of the code where the user submits the essay:

Essay: <br> <textarea rows="10" cols="30" name="essay">Copy your essay here...</textarea><br>
<input type="Submit" name="Submit" value="Submit Essay">
</form>

And it is sent to a MySQL database via php where it is stored at "longtext" and later called back on another page. How can I preserve formatting or basically just paragraphs and line breaks?

7 answers to this question

Recommended Posts

  • 0

For some reason it's not letting me edit my post, but I should add that this is a snippet of the PHP code that calls back the submitted data from the database:

while ($row = mysql_fetch_array($result)) {

echo "<b>Submitter:</b>{$row['submitter']}<br><b>School:</b> {$row['school']}<br><b>Author:</b>{$row['author']}<br><b>Grade:</b> {$row['grade']}<br><b>Submitter's E-Mail:</b>{$row['subemail']}<br><b>Author's E-mail:</b>{$row['authemail']}<br><b>Phone:</b>{$row['phone']}<br><b>Essay:</b>{$row['essay']}<br>
}

  • 0

That's some... ermm... code ....there.

Try writing it like this for proper syntax.

while ($row = mysql_fetch_array($result)) {
echo "<b>Submitter:</b>" . $row['submitter'] . "<br><b>School:</b> " . $row['school'] . "<br><b>Author:</b>" . $row['author'] . "<br><b>Grade:</b> " . $row['grade'] . "<br><b>Submitter's E-Mail:</b>" . $row['subemail'] . "<br><b>Author's E-mail:</b> " . $row['authemail'] . "<br><b>Phone:</b> " . $row['phone'] . "<br><b>Essay:</b>" . nl2br($row['essay']) ."<br>
}

" . stuff . " is the string concatenation (chaining) syntax. Seperate text strings from language constructs like variables and functions.

  • 0
  amoeba said:
That's some... ermm... code ....there.

Try writing it like this for proper syntax.

while ($row = mysql_fetch_array($result)) {
echo "<b>Submitter:</b>" . $row['submitter'] . "<br><b>School:</b> " . $row['school'] . "<br><b>Author:</b>" . $row['author'] . "<br><b>Grade:</b> " . $row['grade'] . "<br><b>Submitter's E-Mail:</b>" . $row['subemail'] . "<br><b>Author's E-mail:</b> " . $row['authemail'] . "<br><b>Phone:</b> " . $row['phone'] . "<br><b>Essay:</b>" . nl2br($row['essay']) ."<br>
}

" . stuff . " is the string concatenation (chaining) syntax. Seperate text strings from language constructs like variables and functions.

585312945[/snapback]

That worked like a charm, thank you so much. I used a tutorial which apparently was not a very good one because it used syntax like that... :pinch: Learning php and mysql is becoming way too much fun :rofl:

  • 0

Btw, to include arrays into a double-quote-delimited string you can write the keys of the array without any quotes, instead of splitting the string or sorrounding it with curly brakets.

while ($row = mysql_fetch_array($result)) {

echo "<b>Submitter:</b>$row[submitter]<br><b>School:</b> $row[school]<br><b>Author:</b>$row[author]<br><b>Grade:</b> $row[grade]<br><b>Submitter's E-Mail:</b>$row[subemail]<br><b>Author's E-mail:</b>$row[authemail]<br><b>Phone:</b>$row[phone]<br><b>Essay:</b>".nl2br($row['essay']).'<br>';
}

Don't forget to close the string and put a semicolon to the end of the echo.

If you're not gonna use the in-string variable interpreter (only when you use double quotes) it's faster to use simple-quotes instead and then escape any occurences ( \' ) in the string.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.