Welcome Guest, Not a member yet? Register   Sign In
Display both name catagories and entries
#1

[eluser]kieutuan[/eluser]
I want display following
-- Catagories 1
+ entry 1
+ entry 2
-- Catagories 2
+ entry 1
+ entry 2

i just can get name catagorie or entries on view
but can't get both.


How do it?
#2

[eluser]Sbioko[/eluser]
Too general question. More details please.
#3

[eluser]CroNiX[/eluser]
You didn't provide enough details to answer the question, such as the database structure or the query you are using to get your data or how you are assembling the data in the view. Post some code.
#4

[eluser]kieutuan[/eluser]
Table Catagories (id, name)
Table Entries (id, cat_id, title, body)


Model
Code:
<?php
class Site_model extends Model{
    function Site_model()
    {
        parent::Model();
    }
    
    function get_records(){
        $this->db->order_by('id', 'desc');
        $query = $this->db->get('catagories');
        return $query->result();
    }
}
?>

Controllers
Code:
<?php
class Home extends Controller
{
    function index()
    {    
            $this->load->model('site_model');
        
        if($query = $this->site_model->get_records())
        {
            $data['records'] = $query;
        }
        
        $this->load->view('include/contents', $data);
    }
}
?>


View
Code:
<?php foreach($records as $rows): ?>
<h1>Catagories: &lt;?=$rows->name?&gt;</h1>
   &lt;?php
   $entries = $this->db->get_where('entries', array('cat_id'=>$rows->id));
   foreach($entries->result() as $entries_row):
   ?&gt;
   <h2>&lt;?=$entries_row->Title?&gt;</h2>
   <p>&lt;?=$entries_row->body?&gt;</p>
   &lt;?
   endforeach;
   ?&gt;
&lt;?php endforeach; ?&gt;


I want write query to get entries in Model or Controllers
how do write it?
#5

[eluser]7amza[/eluser]
try to use join .
#6

[eluser]kieutuan[/eluser]
i've try use join
Code:
SELECT * FROM catagories
INNER JOIN entries
ON id=cat_id
but it'll display
-- Cat 1
+ Entry 1
-- Cat 1
+ Entry 2

I want display
-- Cat 1
+ Entry 1
+ Entry 2
#7

[eluser]kieutuan[/eluser]
bump
plz help me
#8

[eluser]WanWizard[/eluser]
Code:
$cat = '';
foreach ($results as $result)
{
    if ( $result->cat != $cat )
    {
        // print your category header here...
        $cat = $result->cat;
    }
    // print your entry stuff here...
}




Theme © iAndrew 2016 - Forum software by © MyBB