CodeIgniter Forums
Content positioning problem - 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: Content positioning problem (/showthread.php?tid=30681)



Content positioning problem - El Forum - 05-22-2010

[eluser]Unknown[/eluser]
Hi, new to CI and running into a strange issue. Any content I try to load is inserted at the very top of the page, above the doctype. Here's the controller:
Code:
<?php
class Home extends Controller {

    function index()
    {
        $this->load->view('header.inc');

        $this->load->view('nav.inc');

        $this->load->model('Db');

        $this->Db->getContent("Home"); //inserts at top of page not inline!?

        $this->load->view('footer.inc');
    }

}
?>

Here's the class I'm calling for content:
Code:
<?php
    class Db extends Model
    {
        public function dbConnect()
        {
            ...
        }

        public function sanitize($dirty)
        { //takes an array, must be connected to db
            $clean = array();

            foreach    ($dirty as $key => $value) {
                $clean["$key"] = mysql_real_escape_string($value);
            }

            return $clean;
        }
    
        public function getContent($title)
        {
            echo '<div id="copy">';
    
            $query = "...";
            $result = mysql_query($query);
            while ($data = mysql_fetch_object($result)) {
                echo $data->content;
            }

            echo '</div>';
        }
    }

    $db = new Db;
    $db->dbConnect();
?&gt;

Is this my mistake or is it a known quirk? Any help is greatly appreciated.


Content positioning problem - El Forum - 05-22-2010

[eluser]marcin_koss[/eluser]
In your Model class you're echoing the content. Instead you're supposed to assign that data to some variable and return it.

Then in controller you would assign it to $data['result'] = $this->Db->getContent("Home");

In your view file use echo $result wherever you want it to appear.

Code:
function index()
{
$this->load->model('Db');

$data['result'] = $this->Db->getContent("Home");

$this->load->view('header.inc');

$this->load->view('nav.inc');

$this->load->view('page_view' $data); // Here's the page where you want to display your data

$this->load->view('footer.inc');
}

Check the User Guide and the introductory movies that are available on CI website


Content positioning problem - El Forum - 05-22-2010

[eluser]Unknown[/eluser]
Thanks, I'd read most of the manual but somehow missed the dynamic data in views bit.