Lost GET variable into list_items()

edited July 2017 in Modules
In my model, I want to filter my query with a GET parameter, but when I put into the “where”, the value is lost. I also tried assig the value to any variable and I had no problem.
Why the value is lost?

function list_items($limit = NULL, $offset = NULL, $col = '', $order = 'ciclo desc', $just_count = FALSE)
{
$info = '';

if(isset($_GET['id_info']))
$info = $_GET['id_info'];

echo "info=".$info."\n";
$c = '1';

$array = array('id_info' => $info, 'ciclo' => $c);

var_dump($array);

$this->db->where($array);

$this->db->select('id, id_info, ciclo, presion_arterial, frecuencia_cardiaca, frecuencia_respiratoria, temperatura, talla, peso, imc, superficie_corporal', FALSE);

$data = parent::list_items($limit, $offset, $col, $order, $just_count);

$this->debug_query();

return $data;
}

The result in the browser is:

info= array(2) { ["id_info"]=> string(0) "" ["ciclo"]=> string(1) "1" } SELECT id, id_info, ciclo, presion_arterial, frecuencia_cardiaca, frecuencia_respiratoria, temperatura, talla, peso, imc, superficie_corporal FROM (`signos_vitales`) WHERE `id_info` = '' AND `ciclo` = '1' ORDER BY `id_info` asc LIMIT 50


View the code is:

info=1
array(2) {
["id_info"]=>
string(1) "1"
["ciclo"]=>
string(1) "1"
}
SELECT COUNT(*) AS `numrows`
FROM (`signos_vitales`)
WHERE `id_info` = '1'
AND `ciclo` = '1'

Comments

  • edited July 2017
    As you can see I have 2 different results, why?

    Thanks
  • edited 2:55AM
    doesn't the list_items() function reset the $this->db->select() statement?
    Maybe add a $this->db->where() and then pull the data directly?
  • edited 2:55AM
    Thanks, I tried change db->select() for db->query(), I explain:

    I have my URL like this:
    http://../fuel/signos_vitales/?ciclo=1&id_info=1

    function list_items($limit = NULL, $offset = NULL, $col = '', $order = 'ciclo desc', $just_count = FALSE) {

    $info = '';
    $ciclo = '';

    if(isset($_GET['id_info']))
    $info = $_GET['id_info'];

    if(isset($_GET['ciclo']))
    $ciclo = $_GET['ciclo'];

    $qry = 'select id, id_info, ciclo, presion_arterial, frecuencia_cardiaca, frecuencia_respiratoria, temperatura, talla, peso, imc, superficie_corporal from signos_vitales where id_info='.$info.' and ciclo='.$ciclo;

    $this->db->query($qry);

    $data = parent::list_items($limit, $offset, $col, $order, $just_count);

    $this->debug_query();

    return $data;

    }

    I verify that the variables $_GET have values, applying "echo", then apply in my query

    But the result is the same when debug:

    You have an error in your SQL syntax; check the manual that corresponds
    to your MySQL server version for the right syntax to use near 'and ciclo=' at line 1

    select id, id_info, ciclo, presion_arterial, frecuencia_cardiaca, frecuencia_respiratoria, temperatura, talla, peso, imc, superficie_corporal from signos_vitales where id_info= and ciclo=

    I don't know why lost the values of variables when execute query.

    Thanks

  • edited 2:55AM
    The list_items does an AJAX request too. Is the error happening in the AJAX request and are those values being passed in the AJAX request?
  • edited 2:55AM
    Yes, is in the AJAX request the error, check the following image:

    https://drive.google.com/file/d/0Bzij4lOEOgALZG5XTGlfdElmZkE/view
  • edited 2:55AM
    The $_GET variables will only get passed to the AJAX request if there is a form field set for it in the filters area. It essentially will serialize the form fields on the page and pass it to the AJAX request.
Sign In or Register to comment.