Welcome Guest, Not a member yet? Register   Sign In
Query multiple tables and echo the result
#1

[eluser]gmask[/eluser]
Hello, I'm having a really hard time getting the results I want from the database. Here's what I'm trying to do: I have an entries view that lists all of the entries in my database. In each of these entries is a field called 'category' which points to the `categories` table. I would like to be able to have the ability to show which category my entry was posted in, but I can't seem to get it working. Here's the way I'm going about it, please correct me if I'm being silly:

This is in my model
Code:
function entries($num, $offset)
{
    $query = $this->db->query("SELECT categories.title, categories.id, entries.* FROM entries,categories WHERE entries.category=categories.id ORDER BY entries.type DESC, entries.id DESC LIMIT $num");
    return $query;
}

This is the relevant section of my controller
Code:
function entries()
{
    $config['base_url'] = base_url().'/entries/';
    $config['total_rows'] = $this->db->count_all('entries');
    $config['per_page'] = '10';
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';
    $this->pagination->initialize($config);
        
    $this->load->model('fetch_model');
    $data['entries'] = $this->fetch_model->entries($config['per_page'],$this->uri->segment(3));
    $this->template->load('templates/default', 'entries', $data);
}

This is the relevant section of my view
Code:
&lt;?php foreach($entries->result() as $entry) { ?&gt;
    <div class="result">
        <p class="left-title">&lt;?php echo anchor('edit_entry/'.$entry->id, $entry->title); ?&gt;</p>
        <p class="right-title">&lt;?php echo anchor('edit_category/'.$entry->category, 'Parent'); ?&gt;</p>
    </div>
&lt;?php } ?&gt;

Obviously I'd like to be able to echo the category title field in the anchor, replacing the 'Parent' text.

What's the best way to do this?


Messages In This Thread
Query multiple tables and echo the result - by El Forum - 09-07-2010, 01:28 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 12:59 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 01:34 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 01:51 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 02:21 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 02:22 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 02:23 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 02:24 PM
Query multiple tables and echo the result - by El Forum - 09-08-2010, 05:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB