CodeIgniter Forums
Little question about output - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Little question about output (/showthread.php?tid=1762)



Little question about output - El Forum - 06-25-2007

[eluser]VivaUkraine[/eluser]
In my controller i've code for retriving news from database :
Code:
$content = $this->db->get('news');

and i view i've a code like this.
Code:
<?foreach ($content->result() as $row):?>        
            <div class="item">
                
                <h1>&lt;?=$row->title?&gt;</h1>
                <div class="descr">&lt;?=$row->date?&gt;</div>
                &lt;?=$row->content?&gt;
                </div>
        &lt;?php endforeach; ?&gt;

The problem is that i want to do some replacements in $row->content variable. For ex. i want to replace all '&lt;code&gt;' with '<code>' and all '&lt;/code&gt;' with '</code>' tags. I dont want to do this in view file.... How can i do it in Controller ?


Little question about output - El Forum - 06-25-2007

[eluser]ztinger[/eluser]
in your controller

Code:
foreach ($content->result() as $row){      
            
         // do your replacements here  to    $row->content
}



Little question about output - El Forum - 06-26-2007

[eluser]VivaUkraine[/eluser]
[quote author="ztinger" date="1182820605"]in your controller

Code:
foreach ($content->result() as $row){      
            
         // do your replacements here  to    $row->content
}
[/quote]

these replacements can be see only in controller;
but in view file there is no replacements yet Wink


Little question about output - El Forum - 06-26-2007

[eluser]ztinger[/eluser]
well , they should be.

Be sure you are defining $data['content'] after you have done your replacements.


Little question about output - El Forum - 06-26-2007

[eluser]VivaUkraine[/eluser]
[quote author="ztinger" date="1182887993"]well , they should be.

Be sure you are defining $data['content'] after you have done your replacements.[/quote]

i've this code :

Code:
foreach($content->result() as $row) {
        $row->content = str_replace('archive','sscode',$row->content);    
                }
        $data['content'] = $content;

but in view file i have old output, before replaces...


Little question about output - El Forum - 06-26-2007

[eluser]marcoss[/eluser]
I think you are not getting this right, this is what you should do.

Code:
// controller

$query = $this->db->get('news');
if ($query->num_rows() > 0) :    //always check if you have records returned
    $result = $query->result();
    foreach($result as $row) {
        $row->content = str_replace('search','replace',$row->content);
        $content[] = $row;
    }
else :
    $content = 'No records found';
endif;

$data['content'] = $content;

Now if you have records an array with them will be returned, else you will get an string with error message. So your view should look like this.

Code:
// view

&lt;?php if (is_array($content)) : foreach ($content as $row) : ?&gt;
    <div class="item">
        <h1>&lt;?=$row->title?&gt;</h1>
        <div class="descr">&lt;?=$row->date?&gt;</div>
        &lt;?=$row->content?&gt;
    </div>
&lt;?php endforeach; else : ?&gt;
    &lt;?=$content?&gt;
&lt;?php endif; ?&gt;



Little question about output - El Forum - 08-08-2007

[eluser]Unknown[/eluser]
Hi,

I'm new to CI and a PHP beginner but would really like that code example to work in my project.

I'm trying to do a str_replace and the error is pointing to that command. I'm thinking that I would have to convert the object to an array first. Any help is appreciated.


Code:
Error Message: Undefined property: stdClass::$content


Controller:
Code:
function index()
    {

$query = $this->db->query('SELECT bible_books.name, bible_verses.t, bible_verses.v FROM bible_books, bible_verses WHERE bible_books.id=bible_verses.b AND name="Matthew" LIMIT 0 , 20');

if ($query->num_rows() > 0) :    
    $result = $query->result();
    foreach($result as $row) {
        $row->content = str_replace('search','replace',$row->content);
        $content[] = $row;
    }
else :
    $content = 'No records found';
endif;

$data['content'] = $content;

    }

View:
Code:
&lt;?php if (is_array($content)) : foreach ($content as $row) : ?&gt;
    <div class="item">
        <span>&lt;?=$row->v?&gt;</span>
        <span class="descr">&lt;?=$row->t?&gt;</span>
    </div>
&lt;?php endforeach; else : ?&gt;
    &lt;?=$content?&gt;
&lt;?php endif; ?&gt;



Little question about output - El Forum - 08-08-2007

[eluser]Phil Sturgeon[/eluser]
Code:
else :
    $content = 'No records found';
endif;

Code:
foreach ($content as $row)

That's gonna end in tears...


Little question about output - El Forum - 08-08-2007

[eluser]Derek Allard[/eluser]
Just a quick look, but I think you need
Code:
$content[] = $row->content;

in place of

Code:
$content[] = $row;

Now... more importantly. Welcome to CI! Wink