Jump to content

Format MySQL Data


MysticKnight

Recommended Posts

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?

Link to comment
Share on other sites

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 ?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.