Search in database with Foreach in my model Codeigniter

1

Hello to the whole stackoverflow community, my respects for this site is undoubtedly full of geniuses and I hope you can help me: / ...

The issue here is that I have to make a report of how many people were cited by campaign from the first of the month until today, that I was doing manually, each adding a new campaign I manually added another function in the model looking for those cited with respect to the new campaign ... but I want to optimize the time, making a function that facilitates all this ...

I will explain how I have structured my code:

In my controller I have this:

$Nombres_campañas = $this->realreporteo->Nombres_campañas(); //Con esto obtengo los nombres de las campañas que existen.

if($Nombres_campañas != FALSE)
{
$result = array();

foreach($Nombres_campañas as $row) 
{
   $info = $row->empresa;
   $result[] = $this->realreporteo->Formula_numeros_citados($info);
}   

$data['resultados'] = $result;

}       


$this->load->view('headers/librerias');
$this->load->view('headers/menu-admin');
$this->load->view('reporteo', $data);

In my model I have the functions like this

function Nombres_campañas() // Este es para sacar los nombres de las campañas
{ 
        $this -> db -> select('empresa,fechaent');

        $this -> db -> from($this->TableReporteo);  

        $this -> db -> where('fechaent >= ', strtotime(date('1-m-Y')), TRUE);
        $this -> db -> where('fechaent <= ', strtotime(date('d-m-Y')), TRUE);

        $this->db->group_by('empresa');

        $query = $this->db->get();


        $row = $query->result();

        return $row;

}

This is my function that should take the count

function Formula_numeros_citados($info)
{ 

        $this -> db -> select('empresa,fechaent,estatus');

        $this -> db -> from($this->TableReporteo);

        $this -> db -> where('estatus','Citado');

        $this -> db -> where('empresa',$info);

        $query = $this->db->get();

        $row = $query->row();

        return $row;

}

And I want it to be able to print in my view on a table with a foreach

foreach($resultados as $value)
            {

             echo 
             '
             <tr>
                 <td>'.$value->empresa.'</td>
                 <td>'.count().'</td>
             </tr>
             ';

             }  

My database is called reporting and has basic varchar type fields "name, company, date, campaign, status, telephone, etc etc ..."

Currently the code that I showed you throws a result like this in my view: If you throw me the names of the campaigns but the count does me wrong: (

I would very much appreciate if you could help me: /

    
asked by Sebastian Hoover 20.11.2017 в 17:08
source

1 answer

0

I hope my little suggestions will help you

controller

$nombre_campana     = $this->realreporteo->nombres_campanas();
$result         = array();//fuera del if para su validacion si el resultado es false

if( nombre_campana){
    foreach($nombre_campana as $item){
        $result[]   = array(
                    'company'   => $item->empresa,
                    'info'      => $this->realreporteo->formula_numeros_citados($item->empresa)
                    );
    }
}

$data['resultados]  = $result;

model

function nombres_campanas ( ) {
    $this->db->select('empresa, fechaent')
        ->from($this->TableReporteo)
        ->like('fechaent', date('m-Y'), 'both')
        ->order_by('id', 'asc');

    return $this->db->get()->result();
}

function formula_numeros_citados ( $info ) {
    $this->db->select('fechaent, estatus')
        ->from($this->TableReporteo)
        ->where('estatus', 'Citado')
        ->where('empresa', $info)
        ->order_by('id', 'asc');

    return $this->db->get()->result();
}

in the view

$prin = '';
foreach($resultados as $item){
    $prin .= '<tr>'
        .'<td>' . $item->company. '</td>'
        .'<td>' . ( count($item->info) > 0) ? $item->info : 0 . '</td>'
        .'</tr>';
}

echo $prin;

This is what I can think of that you can change, let me know if it worked for you or not

    
answered by 21.11.2017 / 16:39
source