CodeIgniter Forums
Line breaks? When pulling from the Database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Line breaks? When pulling from the Database (/showthread.php?tid=23300)



Line breaks? When pulling from the Database - El Forum - 10-06-2009

[eluser]JamieBarton[/eluser]
Hey guys,

I'm doing sort of a news system, what is the best way to perform line beaks. My articles in the DB have line breaks, however, when i load the view (below) everything is crammed together. How can I get round this?

controller:
Code:
function article()
    {
        $requested_id = $this->uri->segment(3);
        $data['query'] = $this->db->where('id', $requested_id);
        $data['query'] = $this->db->limit(1);
        $data['query'] = $this->db->get('news');
        
        $this->layout->view('news/article', $data, array('page_name' => "news"));
    }

view:
Code:
<div id="main">

    &lt;?php if ($query->num_rows() > 0) { $row = $query->row(); ?&gt;
    
        <h2>&lt;?=$row->title?&gt;</h2>
        
        <p>&lt;?=$row->body?&gt;</p>
    
    &lt;?php } else { ?&gt;
    
    <h2>Not found</h2>
    
    <p>Sorry, we couldn't find the news article you were after.</p>
    
    &lt;?php } ?&gt;
    
</div>


Regards,

Jamie


Line breaks? When pulling from the Database - El Forum - 10-06-2009

[eluser]imn.codeartist[/eluser]
If your line break is \n then use the below code
Code:
<p>&lt;? echo nl2br($row->body)?&gt;</p>



Line breaks? When pulling from the Database - El Forum - 10-06-2009

[eluser]BrianDHall[/eluser]
You could try replacing "\n" (newline) with <br>. It's the easiest way I know of.

EDIT: dixcoder beat me to it.

Also, there is always the <pre> HTML tag.