Error in json ajax and codeigniter using join

0

I have a modal window that I want to show the product information, but the table in the database has two fields that store the id instead of the name. Example the product table

Code = 123456 Name = Product 1 Category = 1 - > there are another table categories containing the id and the name manufacturer = 1 - > there is also a table for manufacturers that contains the id and name

I have no problem in showing the information as it is in the products table, but it shows category = 1 and manufacturers = 1 and what I want is that it shows the names of the manufacturer and the category, and every time I do the query with join between the tables, it gives me an ajax error.

this is the function in the model that searches the product

public function get_by_id($id)
{
$this->db->select("p.*,c.nombre as categoria, f.nombre as fabricante");
$this->db->from("productos p");
$this->db->join("categorias c","p.categoria_id = c.id");
$this->db->join("fabricantes f","p.fabricante_id = f.id");   
$this->db->where("id",$id);   
$query = $this->db->get();
return $query->row();

}

this driver

public function producto_mostrar($id)
{
    $data = $this->productos->get_by_id($id);
    echo json_encode($data);
}

and this code in ajax

  function mostrar_producto(id)
{

$('#form')[0].reset(); 
$('.form-group').removeClass('has-error'); 
$('.help-block').empty(); 



$.ajax({
    url : "<?php echo site_url('almacen/productos/producto_mostrar')?>/" + id,
    type: "GET",
    dataType: "JSON",
    success: function(data)
    {

        $('[name="id"]').val(data.id);
        $('#codigo').text(data.codigo);
        $('#nombre').text(data.nombre);
        $('#descripcion').text(data.descripcion);
        $('#precio').text(data.precio);
        $('#precio_compra').text(data.precio_compra);
        $('#stock').text(data.stock);
        $('#stock_minimo').text(data.stock_minimo);
        $('#categoria').text(data.categoria);
        $('#fabricante').text(data.fabricante);
        $('#modal_formm').modal('show'); /
        $('.modal-title').text('Producto'); 

        $('#photo-preview').show(); 




    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error get data from ajax');
    }
 });
 }

if the function of the model uses it like this

 public function get_by_id($id)
{
    $this->db->from($this->table);
    $this->db->where('id',$id);
    $query = $this->db->get();

    return $query->row();
}

does not give an error but shows the information in the product table as it is with the id instead of the name of categories and manufacturer. I must clarify that this query with join works well if I do not use json but if I use it with json it gives me the error.

    
asked by Francisco 05.01.2018 в 06:13
source

1 answer

0

The json error is because you need to pass an array to the controller and with row () you do not get that effect. try to change this:

$this->db->select("p.*,c.nombre as categoria, f.nombre as fabricante");
$this->db->from("productos p");
$this->db->join("categorias c","p.categoria_id = c.id");
$this->db->join("fabricantes f","p.fabricante_id = f.id");   
$this->db->where("id",$id);
return $this->db->get()->result();

I also recommend you to do a console.log(data) in the ajax's success so you can see that it is returning the query and see how to get the data.

    
answered by 05.01.2018 в 09:34