Codeigniter database information array

Asked

Viewed 242 times

2

For example, I am sending an id array [0] => 1, [1] => 3 and I want to show this in my html as a result. In the case that you search in the database with the first id save in the array and then search with the second id and finally return an array containing the other two arrays.

public function getRecibos($codigosRecibo)
{      
    $result = array('');

    foreach ($codigosRecibo as $codigoRecibo)
    {
        $this->db->select('*'); 
        $this->db->where('idRecibo', $codigoRecibo);
        $this->db->from('recibo');
        $result = $this->db->get();
    }       
        return $result->result();
}

I did so but it is always making the indexes of the array to be 0

public function getRecibos($codigosRecibo)
{      
    $result = array('');

    foreach ($codigosRecibo as $codigoRecibo)
    {
        $this->db->select('*'); 
        $this->db->where('idRecibo', $codigoRecibo);
        $this->db->from('recibo');
        $result[] = $this->db->get();
    }       
        return $result->result();
}

Array
(
    [0] => Array
        (
            [idRecibo] => 2
            [recibo_Recebi] => Condomínio Edifício Dona Gladis 
            [recibo_Relativo] => 02 consultas periódicas - Catia da Silva, Gabriel Vinholes 
            [recibo_Valor] => 64,00 (sessenta e quatro reais)
            [recibo_Dia] => 01
            [recibo_Mes] => Março 
            [recibo_Ano] => 2019
            [recibo_Data] => 2019-03-01
            [recibo_Forma_pgto] => 0
            [recibo_idCheque] => 0
            [recibo_Pago] => s
            [recibo_DataPgto] => 2019-03-13
            [idCheque] => 
            [cheque_Emitente] => 
            [cheque_Numero] => 
            [cheque_Banco] => 
            [cheque_Cic_Cnpj] => 
        )

)
Array
(
    [0] => Array
        (
            [idRecibo] => 1
            [recibo_Recebi] => Condomínio Edifício Porto Vecchio 
            [recibo_Relativo] => 01 consulta periódica - Ronaldo Faria 
            [recibo_Valor] => 32,00 (trinta e dois reais)
            [recibo_Dia] => 01
            [recibo_Mes] => Março
            [recibo_Ano] => 2019
            [recibo_Data] => 2019-03-01
            [recibo_Forma_pgto] => 0
            [recibo_idCheque] => 0
            [recibo_Pago] => s
            [recibo_DataPgto] => 2019-03-13
            [idCheque] => 
            [cheque_Emitente] => 
            [cheque_Numero] => 
            [cheque_Banco] => 
            [cheque_Cic_Cnpj] => 
        )

)

2 answers

1


Change your code, because really you are doing wrong, ie instead of passing the values to the array, you are returning only the result, example:

public function getRecibos($codigosRecibo)
{      
    $result = array();

    foreach ($codigosRecibo as $codigoRecibo)
    {
        $this->db->select('*'); 
        $this->db->where('idRecibo', $codigoRecibo);
        $this->db->from('recibo');
        $result[] = $this->db->get()->result(); // acumulando o resultado
    }       
    return $result;
}

but, apparently there is another way better and also that provides a satisfactory performance using where_in, example:

public function getRecibos($codigosRecibo)
{   
      $this->db->select('*'); 
      $this->db->where_in('idRecibo', $codigosRecibo);
      $this->db->from('recibo');
      return $this->db->get()->result();
}

Reference: Query Builder Class

  • 1

    Thanks beast. It worked perfectly. <3

  • If you can help me one more time, there’s another post of mine that I’m struggling with. https://answall.com/questions/367425/dois-foreach-em-um-select

1

If you change your array to

foreach ($codigosRecibo as $key => $codigoRecibo)

And in bd->get() change to

$result[$key] = $this->db->get();

Browser other questions tagged

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