user6332126
user6332126

Reputation: 27

new line using string replace

I have problems with getting data from mysql database.Let's say that I have text like this in mysql table:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Suspendisse lobortis sit amet est in dapibus. Cras in dui diam.

This new line makes the problem.

I'm using jQuery to echo data to auto creatinig div's.

Here's the code:

for(i = <?php echo $row; ?>; i > 0; i--){
    count = 1;
    <?php
        while($col = mysqli_fetch_assoc($result))
        {
            $cols[] = $col;
        }
        foreach($cols as $col){
    ?>
    $("#text"+count+"").text("<?php echo $col['post'] ?>");
    $("#username"+count+"").text("<?php echo $col['username'] ?>");
    count++;
    <?php } ?>
}

I've tried to remove new line or to replace it with <br> using string replace like str_replace("\n", '<br />', $col['post']); but it didn't work. I've also tried many other codes and nothing worked for me.

Thanks for suggestions

Upvotes: 0

Views: 70

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

You would be much better off seprating out your DB access logic and the client-side javascript stuff. You can also use nl2br() to convert newlines to <br>. So something like this should work better.

<?php
    $rows = array();
    while($row = mysqli_fetch_assoc($result)) {
        // replace new lines in post with HTML breaks
        $row['post'] = nl2br($row['post']);
        $rows[] = $row;
    }
?>
<script>
// create javascript literal by echoing out JSON serialization of $rows from PHP
var dbRows = <?php echo json_encode($rows); ?>;
// iterate through DB records and create elements
$.each(dbRows, function(index, row) {
    $('<div class="text">').text(row.post).appendTo('#someTargetElement');
    $('<div class="username">').text(row.username).appendTo('#someTargetElement');
});
</script>

A few changes from your approach include:

  • Using nl2br() on retrieved data from DB to prepare for use in HTML.
  • Providing better separation between PHP and javascript. This is done by actually making the DB data available in javascript to manipulate instead of just injecting throuhgout javasctipt.
  • Actually creating a new div elements to append to the DOM. Your code did not do this at all
  • Ditching the #text* anti-pattern. What value do the id labels add? Typically when using libraries like jQuery you want all items that are similar to have similar class so you can operate on them as a class.

Upvotes: 1

Davuz
Davuz

Reputation: 5276

The first

If the string output to a textarea or pre tag, \n will make new line in string. If you don't want to have new line, just remove \n.

The second

If the string output to another HTML tags, <br> will make new line, just remove it to stop go to new line.

The third

In PHP, you can have new line in long string by concat strings with const PHP_EOL. Ex: "Hello" . PHP_EOL . "Wolrd"

In jQuery, $.text() will put escaped string to elment's text node (the content), $.html() will insert raw html (as string) to document.

The last

Please don't put your PHP codes together Javascript.

Upvotes: 0

Related Questions