[eluser]mkairys[/eluser]
I've recently started to use CodeIgniter since I found it the easiest framework to work with to get stuff done. I've been working on a web design gallery site as a side project and seem to have hit a problem (probably due to being sleep deprived, I'm sure there would be a simple answer).
Essentially for the index I wanted to display a few items in the gallery in a paginated form (this works). I also wanted to display the amount of comments for each particular item however had problems getting the row id from the loop displaying the query results. Here's my code so far.
Controller
Code:
function index()
{
/* Set Variables */
$data['title'] = "Gallery Index";
/* Initialise Database */
$this->load->database();
/* Pagination */
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/public_html/cssgallery/index.php/gallery/index/'; // Leave temporarily
$config['total_rows'] = $this->db->count_all('design');
$config['per_page'] = '6';
$this->pagination->initialize($config);
/* Database Queries */
/* Query: Display Gallery Item */
$this->db->select('*');
$this->db->from('design');
$this->db->join('site', 'site.site_id = design.site_id');
$this->db->join('category', 'category.category_id = site.category_id', 'left');
$this->db->limit($config['per_page'], $this->uri->segment(3));
$data['query'] = $this->db->get();
/* Query: Display Comment Count */
$this->db->select('*');
$this->db->from('design_comments');
$this->db->where('design_id', $variable);
$data['query2'] = $this->db->get();
/* Output */
$this->load->view('gallery', $data);
}
View
Code:
<?php $this->load->view('layout/header', $data);?>
<?php foreach ($query->result_array() as $row):?>
<p>
<?php echo $row['design_id'];?>
<a href="<?php echo $row['site_address'];?>"><?php echo $row['site_title'];?></a>
<br />
Category: <?php echo $row['category_name'];?>
<br />
Comments: <?php echo $query2->num_rows()?>
</p>
<?php endforeach;?>
<?php echo $this->pagination->create_links();?>
<?php $this->load->view('layout/footer', $data);?>
In the 2nd query in the controller I have just left '$variable' to show where I wanted to get the row id from the loop in my view. If I leave a defined number such as 1 it returns results fine so I'm pretty sure the query is spot on. I just wasn't quite sure how to grab design_id from the loop array and output the result.
Also was wondering, am I on the right track to keep my code clean? I'll move the database specific stuff to Models later, just wanted to make sure I get the queries working first.
Any help is greatly appreciated!