Jump to content

Help with MySQL join


guinbRo

Recommended Posts

So this topic could go to code igniter, mysql queries, or here.  If this is in the wrong spot I apologize in advance. 

 

Anyways, my issue is generic.  I want to get better with using proper MySQL queries and I'm trying to display a list of school classes that have assignments associated with each one.  (Think like categories with blog posts associated to the categories).  What I want to do is display a Class, and underneath have a list of assignments.  However with the code I'm currently using it's printing the school class name for each assignment there is, not what I want or need.  I could do this pretty easily by simply executing two queries (one to get the class, and the one to get the assignments associated with said class), but I feel that there is an easier--and more correct way.  Here's the code i'm working with: 

 

blog_view.php

  <body style="margin-top: 100px;">
  <div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
      <div class="container">
        <a class="brand" href="#">McDonald Sandbox</a>
  </div>
</div>
  </div>   
  <div class="container-fluid">
    <div class="span2">
      <ul class="nav nav-pills">
        <li><a href="#"><i class="icon-plus-sign"></i> Add Class</a></li>
        <li><a href="#"><i class="icon-plus-sign"></i> Add Assignment</a></li>
        <li><a href="#"><i class="icon-wrench"></i> Settings</a></li>
      </ul>
    </div>
    <div class="span10">
      <?php foreach($categories as $item): ?>
        <h2><?php echo $item['cat_title']; ?></h2>
        <div class="well">
          <h4><?php echo $item['assign_name']; ?></h4>
          <?php echo $item['assign_desc']; ?>
        </div>
      <?php endforeach; ?>
    </div>
    </div>
  </body>
</html>

 

blog_model.php

<?php

class Blog_model extends CI_Model {

function __construct() 
{
    $this->load->database();
}


    function get_latest() {
        
        //Select our classes, and get assignments from each class
        $this->db->select('*');
        $this->db->from('categories');
        $this->db->join('assignments', 'categories.cat_id = assignments.assign_cat_id');

	//Return the classes and assignments in an array
	$query = $this->db->get();
	return $query->result_array();
                
    }
    
}

?>

 

blog.php (controller)

<?php
class Blog extends CI_Controller {

public function __construct() 
{
    parent::__construct();
    $this->load->model('blog_model');
}

public function index() 
{
	$data['categories'] = $this->blog_model->get_latest();
	$this->load->view('header');
	$this->load->view('blog-view', $data);
}

}
?>

 

Also please feel free to criticize coding habits that you feel I could do better in (not a whole lot of actual code there, but if anything stands out).  Thanks for your help :)

Link to comment
Share on other sites

Thank you.  I used the group_concat and group_by to achieve the results I some what wanted, but your method is giving me more control and is leaving less of a code footprint in my view file.  Thanks again :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.