[eluser]Marcus Marden[/eluser]
Hello friends,
I started to use CI and I am still learning. I am trying to do video script via CI and I need help for the queries below:
/controllers/category.php
Code:
$data['categories'] = $this->db->order_by('name', 'ASC')->get('category')->result_array();
foreach($data['categories'] as $category)
{
$data['videos'] = $this->db->select('seo, name, thumb, view, vote, score')->from('video')->where('category_id', $category['id'])->order_by('id', 'DESC')->limit(3)->get()->result_array();
// this code returns always category_id = 3 but I want to use it for all category ids.
}
$this->load->view('category_list_view', $data);
view/category_list_view.php
Code:
<?php foreach($categories as $category) { ?>
<h2><?php echo anchor('category/show/' . $category['seo'], $category['name'], array('title' => $category['name'])); ?></h2>
<?php foreach($videos as $video) { ?>
<h1><?php echo anchor('video/show/' . $video['seo'], mb_substr($video['name'], 0, 25, "utf-8"), array('title' => $video['name'])); ?></h1>
<?php } } ?>
I want to list all categories and 3 video files in categories.
My next question is, I am always using below code on view layer for list items.
Code:
foreach($query as $row) {
echo $row->id; // or if array $row['id']
}
Is it any solution to use it on controller layer and forward data to view layer?
Let me explain via codes.
Code:
$categories = $this->db->order_by('name', 'ASC')->get('category')->result_array();
foreach($categories as $category)
{
$data['title] = $category['title'];
}
$this->load->view('category_view', $data);
view layer
Code:
Category: <?php echo $title; ?> <br />
Returning ONLY "Category: Games" but should be:
Category: Games
Category: Popular
Category: Music
etc etc...
When I add foreach on view layer everything normal.
Hope that I explain my questions and thanks now for your helps and advice.