MysticKnight Posted April 12, 2022 Share Posted April 12, 2022 I’m new to creating database driven websites with MySQL and PHP. I think this is an easy question, but it has been anything but easy finding an answer. When someone submits 2 paragraphs to my MySQL database via a textarea in a feedback form, I want to display 2 paragraphs when I post that information at my website. How is it done? Quote Link to comment https://forums.phpfreaks.com/topic/314693-format-mysql-data/ Share on other sites More sharing options...
Barand Posted April 12, 2022 Share Posted April 12, 2022 Use this 1 Quote Link to comment https://forums.phpfreaks.com/topic/314693-format-mysql-data/#findComment-1595265 Share on other sites More sharing options...
gizmola Posted April 13, 2022 Share Posted April 13, 2022 It really depends on your markup. Another solution would be to render the data back into a form, perhaps with the original form elements set as read only. The important thing to note, is that with html, whitespace is not preserved. There is an expectation that markup will be used for formatting, and thus, the idea that a bunch of text with newlines in it, means there are separate paragraphs, is not a concept that exists in html. You can use nl2br as Barand suggested, but it's not that great a solution. It basically just replaces newline characters in the original text with '<br>' characters. A more robust solution would be to use explode in combination with something like trim to derive paragraph blocks and then render them using actual paragraph tags. This gives you much better control of the re-formatting of content. PHP has the explode function which makes this fairly easy to construct. Something along the lines of this could also be a solution: <?php // Original Text in a variable named $content function makeParagraphs($content, $cssClass='') { $content = explode(PHP_EOL, $content); $output = ''; $p = $cssClass ? '<p class="' . $cssClass . '">' : '<p>'; foreach ($content as $paragraph) { $paragraph = trim($paragraph); if (!empty($paragraph)) { $output .= $p . $paragraph . '</p>' . PHP_EOL; } } return $output; } I'm not a huge fan of this type of function for generating html output to be honest, as I try and always separate markup from data, but it gives you an idea of what a function like this could be used for, and problems it can solve for you. Most of my code utilizes a templating system (twig or laravel blade), so I wouldn't have written a function like this, but instead would probably do something more like this, if I was going to only utilize PHP built in templating: function makeParagraphs($content) { return array_filter(explode(PHP_EOL, $content), function($p) { return !empty(trim($p)); }); } ?> <?php foreach(makeParagraphs($content) as $paragraph): ?> <p class="largeText blue"><?= $paragraph ?></p> <?php endforeach ?> Quote Link to comment https://forums.phpfreaks.com/topic/314693-format-mysql-data/#findComment-1595290 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.