Welcome Guest, Not a member yet? Register   Sign In
Weird problem updating a message
#1

[eluser]Mowgli[/eluser]
Hello,

I'm trying to make a simple blog (from scratch) and got stuck here.

Code:
function update_topic($insert)
    {
       $data = array(
               'titlu' => $insert['titlu'],
               'mesaj' => '<pre>'.htmlspecialchars($insert['mesaj']).'</pre>'
            );

        $this->db->where('id', $insert['id']);
        $this->db->update('topics', $data);
    
    }


    function get_topic($id)
    {
        $results = $this->db->get_where('topics', array('id' => $id))->result();
        $result = $results[0];
        $subs = '<pre>';
        $result->mesaj = str_replace($subs,"",$result->mesaj);
        $subs = '</pre>';
        $result->mesaj = str_replace($subs,"",$result->mesaj);
        return $result;
    }


So basically what am I trying to do here is to preserve the message exactly as the user wrote it. That's why i'm using the <pre> tag. The big problem appears when i try to edit the message. I call get_topic() function and I remove the <pre> tags from the existing message that is stored into the database. I do this because i don't want (and i don't allow) special characters (like < or > )in textarea, i don't want to see them every time i hit edit. So once i got the topic text from the database I assign it as a value to a textarea so i could easily edit and update it. After I push the "update" button I add the '<pre>' tags again, after checking for special chars, of course.
Apparently everything works fine except for this wired thing i don't understand. Each time i edit the message a new break line appears between each row. For instance if the original message was this:

-------------------
test

test test

test test test
-------------------
After 1 update (even if i don't edit anything) i get this:
-------------------
test


test test


test test test
-------------------
After 2 updates:
-------------------
test



test test



test test test
-------------------
And so on. The distance between the rows keeps growing and i don't understand why.
Any help would be much appreciated.

PS: is this the right way to do it ? I mean using the <pre> tag. This is my first attempt to create a blog from scratch so please bare with me.



LE: I found a solution but i still don't know what could've cause this...
Solution: preg_replace("/\n\n+/s","\n",$string);


Messages In This Thread
Weird problem updating a message - by El Forum - 07-17-2011, 09:40 AM



Theme © iAndrew 2016 - Forum software by © MyBB