Search results in Mysql database with Codeigniter

Asked

Viewed 98 times

1

I need to find a result in the database, and show it on the cart screen. I am using codeigniter, already created the model and controller and I am calling in the view, but the result does not come.

The value I look for is’s' or 'n' in the table 'ga845_client' column 'wholesaler'.

I appreciate the help.

Controller (shopping cart)

public function exibeAtacadista(){
    //busca os arquivo na tabela cliente
    $this->load->model('carrinho_model');
    $atacadista = $this->carrinho_model->getAtacadista($data);
    }

Model (cart_model)

public function getAtacadista($data){ 
        $this->db->flush_cache(); 
        $sql = "SELECT * FROM `ga845_cliente` WHERE atacadista = 's' = ".$data['atacadista']." "; 
        $query = $this->db->query($sql); 
        print_r($query); die();
        return $query->result(); 
    }

View (cart)

I’m calling it that.

echo 'teste:' .$atacadista;
  • Does the query return any list? If yes, it could show how the data comes?

  • Returns nothing, goes blank, but in the bank has the value’s'

  • I use different commands to search the model, take a look and see if it helps you. function getSetor($data) {
 $this->db->flush_cache();

 $sql = "
 SELECT u.id_setor, s.nome_setor FROM usuarios u
 LEFT JOIN setores s ON s.id_setor = u.id_setor
 WHERE u.id_usuarios = ".$data['id_usuario']."
 ";

 $query = $this->db->query($sql);
 return $query->result();
 }

  • Worse that didn’t happen either, as I’m learning to deal with codeigniter, I still have a hard time understanding where the mistake might be.

  • You tested the query in the database?

  • Sorry ignorance, I’m in the basics of PHP yet, how do I do this?

  • PHP runs on a server, usually the staff uses Xampp’s phpAdmin, that would be?

  • All right, that’s right

  • By accessing phpAdmin, log into your database where you have the tabela ga845_cliente, when you click on this table, the data will appear. Above the data there are tabs, one of them has the SQL name, click on it. In this part, you paste the code SELECT * FROM ga845_clientWHEREwholesaler = 'S' and rotate to see the result.

  • Right, he showed the two results I have with the S parameter'

  • Blz, so , in your controller you need to give a print_r($this->carrinho_model->getAtacadista($this->session->userdata('atacadista'));) die();

  • And see if printa in the browser console(F12 - Network) the list

  • I put it inside the controller I mentioned above, and it did not return anything on the console

  • Jeez... What if you put the print_r in the model? It returns something? Where is it return $query puts print_r($query); die();

  • Also nothing, to see if I’m not doing wrong, pasted inside the model file after the line "Return $query;"

  • Put this command before Return, and see if it prints something, as it is not possible that it will not print....

  • I put and also showed nothing, I’m going to my view page that calls this model and controller, right?

  • That’s right, in your view you call this function and should print something on the console... And the query is correct, so much so that in the database it does the search and returns... How is your configuration, in php, to connect in the database? The path would be Application/config/database.php and see the configuration in the $db variable[]

  • Worse than not calling anything, the configuration is right, because it looks for the other values of other tables. What should I call it in the view? Just to confirm

  • I use Ajax to call, I think it won’t fit here in the comment, so create an answer to show you.

Show 15 more comments

1 answer

0

I use AJAX to call the controller functions from the view.

But in your code, in Controller you’re calling $this->carrinho_model->getAtacadista() and in modal the function name is different public function getAtacado() shouldn’t be public function getAtacadista() ?

function inserirProcesso() {
        var base_url = "<?php echo base_url() ?>"; // http://localhost/sistema/
            var data = $("#formInserirProcesso").serialize(); //Pega os dados dos input do form

            $.ajax({
                    type : 'POST',
                    url  : base_url +'carrinho/exibeAtacadista',
                    data : data,
                    success :  function(response) {
                       alert('Confere se na console mostrou os dados');
                    }
            });
            return false;
    }
  • I updated the codes above, as we were talking, I did not understand how to call by AJAX, can see if I am calling the variable correctly?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.