How to receive an array of results in a variable

Asked

Viewed 197 times

1

Good morning, I’m trying to carry out a very simple project using the Codeigniter framework, but I always had this question before even implementing a framework, I always beat myself up to receive data that is an array, and show these results on the screen to the user, I usually perform a query in the database and want to show these results to the user, I have not been able to use with foreach because it always shows me only the last result, and I want all the records that the query to the database returned me. Can anyone help me? or link to an article on the subject?

I have an example that I show users the schedules that are in the database and it selects some of these schedules, but before registering the schedules that he selected I want, impose on the screen with echo to confirm the request of the user.

MODEL

public function select_horarios(){
    $this->db->select('hora');
    $this->db->from('horarios');
    $query = $this->db->get();

    if($query){
        return $query->result_array();
    }else{
        return false;
    }
}

CONTROLLER

public function index(){
    $this->load->model('administrativo/abrirfechar_agenda', 'agenda');



    if(isset($_POST['acao']) && $_POST['acao'] == 'Abrir Agenda >>'){
        $data = $this->input->post('data');
        $horarios = $this->input->post('horarios');

        //Data Formatada
        $data_form = date('d/m/Y', strtotime($data));

        if(empty($data)){
            echo 'Selecione a Data';
        }else if(empty($horarios)){
            echo 'Informe os horarios para a data '.$data_form;
        }else{              
            //INICIANDO A CONSULTA PELA DATA E HORARIOS SELECIONADOS                

            //Tratamento de matriz
            foreach($horarios as $linha){
                $horas = $linha;
            }
            //INICIANDO O CADASTRO NO BD PARA ABERTURA DE AGENDA.
            echo "Passou na validação ".$horas;
            }       
    }

    $dados['horarios'] = $this->agenda->select_horarios();
    $this->load->view('administrativo/abrirfechar_agenda', $dados);
}

VIEW

echo form_label('Selecione os Horários: ', 'horario');
    echo "</br>";

    foreach($horarios as $linha){           
        echo form_checkbox('horarios[]',$linha['hora']);
        echo $linha['hora'];
        echo "</br>";
    }
  • Please post the code you are trying to implement, from model, view and controller.

2 answers

1

Model

public function get_all($limit = NULL, $offset = NULL)
{
        $this->db->select('cli_id, cli_nome');
        $this->db->from("cliente");        
        $query = $this->db->get();
        $data = $query->result();
        return $data;
}

$results = $this->Cliente_model->get_all_where($limit, $offset);

foreach ($results as $row) {
  echo $row->cli_nome;
}
  • This is an example how to use codeigninter to display database data;

  • I just put the wrong function name get_all_where -> get_all and also forgot to remove the 2 attributes that are not being used.

1


In this case, if you are going to select which users you selected without seeing your full view, I would do so:

// controller
public function index(){
    $this->load->model('administrativo/abrirfechar_agenda', 'agenda');
    $dados['horarios'] = $this->agenda->select_horarios();
    $this->load->view('administrativo/abrirfechar_agenda', $dados);
}


// model
public function select_horarios(){

    $this->db->select('hora');
    $query = $this->db->get('horarios')->result();

    if($this->input->post('data') && $this->input->post('horarios')){ // Se houver post data, horarios
        $horarios_selecionados[] = array();
        foreach($query as &$value){ // busco todos os horários
            // neste caso, fazemos então a comparação se existe já o horário cadastrado
            // se sim, ele adiciona na array, para que consigamos dizer ao usuário qual horári
            // já tem cadastrado
            $this->db->where('horario', $value);
            $this->db->where('data', $this->input->post('data'));
            $value->horarios = $this->db->get('horarios')->result();
            $value->data = $this->input->post('data');
        }
    }

    if($query){
        return $query;
    }else{
        return false;
    }
}

// view
foreach($horarios as $value){
    foreach($value->horarios as $h_selecionado){
        echo $h_selecionado."<br>";
    }
}
  • 1

    Now for this example I was able to show the results of the schedules that are in the database, but I can’t show on the screen the schedules that the user selected, because with the code I posted only shows me the last result. and I want all the schedules he selected

  • Try the way I passed it on to you now.

  • 1

    Andre but this returning all the times that the user typed are equal in the database, this right but I need to consult the date, for example if not day 02/01/2019 already have registration of the schedule 09:00, I can not register this date

  • See if it works the way I gave it to you... of course it’s a superficial solution, without seeing your view or the database...

  • It works Yes Andre I made some modifications and it worked perfectly, thanks for the help... as I do not understand how the site works what I must do to close the question since Oce already solved my doubt?

  • You can vote (up arrow, in my reply, and mark as correct - V below arrows) thank you :)

Show 2 more comments

Browser other questions tagged

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