ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Codeigniter model pass value controller to view with join tables

August 13, 2014 8:47am

Subscribe [3]
  • #1 / Aug 13, 2014 8:47am

    webmarkcompt

    17 posts

    Hi all,

    I having a problem passing values ​​in a column according to the id of the product.

    The join tables is running but returns all column values. I think the solution is simple but I am not able to get there. Please some help to solve this issue?

    Here is the tables

    TABLE: tbl_lining
    id, article, description, observations

    TABLE:products

    [Table Products - picture] http://goo.gl/jjUXyy

    Function used in model


    MODEL

    public function getliningsale($id)
    {
        $this->db->select('*');
        $this->db->from('products');
        $this->db->join('tbl_lining', 'products.lining=tbl_lining.id', 'left');
        $this->db->where('products.id', $id);  // Also mention table name here
        $query = $this->db->get();    
        if($query->num_rows() > 0)
            return $data->result();
    }

    CONTROLLER

    function liningsale()
    {   
    
    $data['lining'] = $this->sales_model->getliningsale($id);
    $this->load->view('add', $data);
    }

    Trying to display one record according product id

    VIEW

    echo '<td><input class="span1 tran2" name="lining\\\\'+ count +\\\\'" type="text"';
    echo 'value="';
     foreach ($lining as $valor) {
     echo $valor->article;
     echo '-';
     echo $valor->description; 
     echo '">';
     }
    
    echo '</td>';


    Instead of that, is displaying all records

    [Table Result - picture](http://goo.gl/BhHunM)


    Can anyone please help me to solve this issue?

     

  • #2 / Aug 13, 2014 12:46pm

    InsiteFX

    6819 posts

    As far as the id it depends on what you are passing to the model method.

    foreach ($liningsale as $valor) {
    
    // should be
    foreach ($lining as $valor) {

     

  • #3 / Aug 13, 2014 1:03pm

    webmarkcompt

    17 posts

    Sorry,
    I’ve done too many changes that I ended up copying the wrong function.

    Posting a var_dump($query->num_rows()); it appears that the ID is null.

    int(0) SELECT * FROM (`products`) LEFT JOIN `tbl_lining` ON `products`.`lining`=`tbl_lining`.`id` WHERE `products`.`id` IS NULL

    So… I supose that’s the problem.  I can’t figure out the solution to solve it..

    Any sugestion?
       

  • #4 / Aug 13, 2014 1:22pm

    InsiteFX

    6819 posts

    You need to pass an id to the method.

    Usually this done but getting it from a table that lists all the records with actions [edit, delete etc;]

  • #5 / Aug 13, 2014 1:27pm

    webmarkcompt

    17 posts

    That’s the tricky part…

    This is a table of orders that loads the data via jquery (see picture here pls goo.gl/6Jl7XO).

    The script was already functional. Was not made by me. I just need to add more information to it.

    Seeking only the IDs stored in each column without JOIN, (lining / leather / others) the query is successful, with display of the IDs store in columns. 

    If I put here the code You can take a look?

  • #6 / Aug 13, 2014 1:58pm

    CroNiX

    4713 posts

    In your controller/liningsale method, you are:

    $data['lining'] = $this->sales_model->getliningsale($id);

    Where is $id coming from? It’s not in the code, so it appears to be undefined

  • #7 / Aug 13, 2014 2:08pm

    webmarkcompt

    17 posts

    Model:

    public function getliningsale($id)
    {
        $this->db->select('*');
        $this->db->from('products');
        $this->db->join('tbl_lining', 'products.lining=tbl_lining.id', 'left');
        $this->db->where('products.id', $id);  // Also mention table name here
        $query = $this->db->get();    
        if($query->num_rows() > 0)
            return $data->result();
    }

    That id should be from model?

    Note that I never worked with codeigniter. Maybe I’m not doing the right process.
    Any help would appreciate

  • #8 / Aug 13, 2014 2:13pm

    CroNiX

    4713 posts

    Your model seems correct. Your controller isn’t as that’s what’s passing the $id to the model, but it’s not defined in the controller before you pass it.

  • #9 / Aug 13, 2014 2:22pm

    webmarkcompt

    17 posts

    How can I define that in the controller?

  • #10 / Aug 13, 2014 2:28pm

    CroNiX

    4713 posts

    Hard to tell exactly as there seems to be a lot of missing code that is relevant. It looks like it might come from the form data but the partial code you posted doesn’t show the rest. You also mention using jQuery to load the data but don’t show that code either.

  • #11 / Aug 13, 2014 2:42pm

    webmarkcompt

    17 posts

    Since are many lines of code, I send it in attachment .zip

    Files: sales_model (model), add (view), sales (controller)

    If you can take a look, I will be very grateful!

    I already try using the jquery, but without sucess…


    Note:

    add.php : line 530

    sales_model.php:  line 1105

    sales.php : last line

  • #12 / Aug 13, 2014 2:49pm

    CroNiX

    4713 posts

    I’m sorry, but I really don’t have time to sort through 4,300+ of lines of code.

  • #13 / Aug 13, 2014 2:56pm

    webmarkcompt

    17 posts

    The code that would interest is between the lines that I’ve said.

    add.php 500

    If I use jquery the query runs and display the id records.
    In last case I would not use ID’s, and post data directly to table. But it would have more advantages for future use if I could ID’s..

    Can You just take a look please?

  • #14 / Aug 15, 2014 8:06am

    InsiteFX

    6819 posts

    then post the jQuery code, that’s what is most likely passing the id.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases