0

i have two tables of database 1st table is: Products *id *brand *model

2nd table is: Sales *id *pid *date

i want to create list with getting data form both tables.

here is my Model:

class Sales_model extends CI_Model {
    function getAll() {


        $q = $this->db->get('sales');
        foreach ($q->result() as $row) {    
            $data[] = $row;

            $q2 = $this->db->get_where('products', array('id' => $row->pid));

            foreach ($q2->result() as $row2) {
                $data[] = $row2;
            }

        }
        return $data;
}

-------- and here is controller code ------------

class Sales extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->model('sales_model');

        $data['q'] = $this->sales_model->getAll();

        $this->load->view('sales', $data);
    }
}

-------------------- here is my view code ----------------

<?php foreach($q as $row) : ?>
    <tr>
        <td><?php echo $row->id; ?></td>
        <td><?php $date = new DateTime("@$row->date"); echo $date->format('D M d, Y'); ?></td>
        <td><?php echo $row->brand; ?></td>
        <td><?php echo $row->model; ?></td>
    </tr>
<?php endforeach; ?>

anybody help me, i want to get date from sales table and products detail who add in sales table. Thanks

1
  • This isn't really a CodeIgniter question but more of how to build the query to get your intended results right? Commented Oct 3, 2012 at 19:25

2 Answers 2

1

I'm not quite sure what data you require but this might help.

$sql = "SELECT products.id, products.brand, products.model, sales.id, sales.date FROM products INNER JOIN sales ON (products.id = sales.pid)";
$query = $this->db->query($sql); 

if($query->num_rows() > 0){
return $query->result_array();
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Model :

$this->db->select('p.*,s.date');
$this->db->from('product p');
$this->db->join('sales s','p.pid = s.pid');
$result = $this->db->get();

if($result->num_rows)
{
return $result->result();
}
return false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.