Welcome Guest, Not a member yet? Register   Sign In
Beginner Database Help
#1

[eluser]Jonathan McGaha[/eluser]
I just started tearing into CI in the last week. I thought to get my feet wet I would take a previously hand coded PHP/MYSQL site and move it to CI. Basically its just the Bible being fed from a database. In going through the documentation I can't quite figure out how the MVC relationship fits together. I have the whole mysql query in my controller. Should that be in a module instead? Then how do i put the results into my view? Because the echo is in the controller everything just appears at the top of my view. I'm sure these questions are posted somewhere but I don't even know what to search for. Thanks for giving some help to a new enthusiastic CI developer.

Here is the controller.
Code:
<?php
class Bible extends Controller {

    function index()
    {
        $this->load->view('bibleview');
        $this->load->database();
        $this->db->select('verses.book AS vbook, verses.chapter AS chapter, verses.verse AS verse, verses.text AS text, books.book AS book');
        $this->db->where('verses.book', '48');
        $this->db->from('verses');
        $this->db->join('books', 'verses.book = books.id', 'inner');
        $query = $this->db->get();
        
        
            foreach ($query->result() as $row)
            {
                echo $row->vbook;
                echo $row->chapter;
                echo $row->verse;
                echo $row->text;
                echo $row->book;
            }
    }
}
?>
#2

[eluser]adwin[/eluser]
you can store it into $data and the pass it on like this
Code:
<?php
class Blog extends Controller {

    function index()
    {
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";
                ........
        $data['bibledata'] = $this->db->get()->result;
        ...........
                $this->load->view('pageviewer', $data);
    }
}
?>


and on the view you just need to call :

foreach($bibledata as $data)
{
  ... loop your result in here ...
}
#3

[eluser]Jonathan McGaha[/eluser]
Thanks thats what I needed. Cheers
#4

[eluser]minimal design[/eluser]
I'm fairly new to MVC too, so I might be wrong, but the way I do it, which works pretty well, is retrieve the DB data in the model, then use the controller to send the relevant data to the view.

Model:
Code:
function get_data($id) {
    $sql = "SELECT * FROM etc...";
    $query = $this->db->query($sql, $id);
    if ($query->num_rows() > 0) {
        $result = $query->result_array();
        foreach ($result as $entry) {
            $entries[] = $entry;
        }
        return $entries;
    } else {
        return false;
    }
}

Controller:
Code:
function index($id) {    
    $data['entries'] = $this->data_model->get_data($id);
    $this->load->view('header', $data);
    $this->load->view('data_view');
    $this->load->view('footer');
}

Then the looping part's in the view is the same as posted above...
#5

[eluser]Jonathan McGaha[/eluser]
I was gonna try that too because the user guide does say that the models are for the database information.
#6

[eluser]gtech[/eluser]
minimals design is perfectly valid, the idea being that the model can deal with the database stuff, and the controller can deal with the routing and parsing information in and out, and the view can concentrate on displaying content.

if you extract the database methods to a model, multiple controller methods can reuse the same database code.
#7

[eluser]m4rw3r[/eluser]
ummm, the
Code:
$result = $query->result_array();
foreach ($result as $entry) {
    $entries[] = $entry;
}
is totally unnessesary, it only loops through the result and creating an exact duplicate of the result_array() return value.
So $result === $entries.

It would be a difference if you did some processing in the foreach, but now it's only a waste of CPU and RAM.
#8

[eluser]minimal design[/eluser]
[quote author="m4rw3r" date="1209418104"]ummm, the
Code:
$result = $query->result_array();
foreach ($result as $entry) {
    $entries[] = $entry;
}
is totally unnessesary, it only loops through the result and creating an exact duplicate of the result_array() return value.
So $result === $entries.

It would be a difference if you did some processing in the foreach, but now it's only a waste of CPU and RAM.[/quote]

I didn't mean that as a fully working example. It needs to be customized, and most of the time you'll need that loop to tweak your results... the "$sql = "SELECT * FROM etc..." won't work either... Wink
#9

[eluser]m4rw3r[/eluser]
Just thought it was a bit odd, writing an example with some unnessesary code pieces.

Of course a loop is inevitable, if not in the model or controller (for formatting and similar things, but some of them you can move directly to the view), you have to do it in the view Tongue




Theme © iAndrew 2016 - Forum software by © MyBB