Doubt with foreach [closed]

-1

This system is done using PDO, mvc and bootstrap library, I will explain my doubt.

In the controller part I have a QuestãoController.php file there I created a chamaFormulário() method, inside this method I instantiate the DAO file of QuestaoDAO there is all select to fetch a data in the database I need to make one foreach to get certain field from the table of my bank in the case table questions

QuestoesDAO.php

<?php
require_once ("../library/data/DataBase.php");

class QuestoesDAO extends DataBase {

 private $tabela = "questoes";

 // Retorna todos os dados da tabela questoes
 public function getAll()
 {
    return $this->db->select("SELECT * FROM {$this->tabela}");
 }

 // Retorna uma questão em particular
 public function getById($id)
 {
    $id = (int)$id;
    return $this->db->select("SELECT QUESTOES FROM {$this->tabela} WHERE idQUESTOES = :id",array(':id'=>$id),FALSE);

 }

 // Retorna uma questão a partir de uma pesquisa
 public function getLike($busca)
 {
    return $this->db->select("SELECT * FROM {$this->tabela} WHERE QUESTOES LIKE :busca",array(':busca'=>$busca));
 }

 // Remove uma questão
 public function remove($id)
 {
    $id = (int)$id;
    return $this->db->delete($this->tabela,"idQUESTOES = '$id'");
 }

 public function cadastrarQuestoes(Questoes $questoes)
 {
    $valores = array('QUESTOES'=>$questoes->getQuestoes(),'DESCR_QUESTOES'=>$questoes->getDescQuestoes(),'ORDEM'=>$questoes->getOrdem());

    return $this->db->insert($this->tabela, $valores);
 }

 public function preencheGrid()
 {
    return $this->db->select("SELECT id as idQUESTOES,
                                     QUESTÃO as QUESTOES,
                                     DESCR_QUESTOES as DESCR_QUESTOES FROM
                                     {$this->tabela}");
 }



 public function atualizar(Questoes $questoes)
 {
    $valores = array('idQUESTOES'=>$questoes->getId(),
            'QUESTOES'=>$questoes->getQuestoes(),
            'DESCR_QUESTOES'=>$questoes->getDescQuestoes(),
            'ORDEM'=>$questoes->getOrdem()
    );

    $where = "idQUESTOES = " . (int) $questoes->getId();

    //Executa a operação

    return $this->db->update($this->tabela, $valores, $where);
 }
}

QuestoesController.php

<?php
require_once("../model/pojo/Questoes.php");
require_once("../model/dao/QuestoesDAO.php");


class QuestoesController {

private $dao;
private $questoes;
public $retorno;
public $fieldvalue;



public function __construct() 
{
    $this->dao = new QuestoesDAO();
    $this->questoes = new Questoes();

    if (isset($_GET['action'])) {
        if ($_GET['action'] == 'alterar') {
            $this->showDadosForm();
        }   
        else if ($_GET['action'] =='remover') {
            $this->remover();
        }

    }

    if( $_SERVER['REQUEST_METHOD'] == 'POST'){
        $acao = $_POST['action'];
        if ($acao =='inserir') {
            $this->inserir();
        }
        else
            if ($acao =='alterar') {
                $this->alterar();
            }
    }


}



public function inserir() {
    $this->recebeDados();
    $cadastrou = $this->dao->cadastrarQuestoes($this->questoes);
    if ($cadastrou) {
        $this->retorno = "<div class='alert alert-success'>
                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                <strong>Sucess!</strong> Questão inserido com sucesso!
                </div>";
    }
    else {
        $this->retorno = "<div class='alert alert-success'>
                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                <strong>Sucess!</strong> Erro ao inserir questão!
                </div>";
    }

    return $this->retorno;
}

public function alterar() {
    $this->recebeDados();
    $this->questoes->setId($_POST['id']);

    if ($this->dao->atualizar($this->questoes)) {
        $this->retorno = "<div class='alert alert-success'>
                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                <strong>Sucess!</strong> Questão alterada com sucesso!
                </div>";
    }

    else {
        $this->retorno = "<div class='alert alert-success'>
                <button type='button' class='close' data-dismiss='alert'>&times;</button>
                <strong>Sucess!</strong> Erro ao alterar questão!
                </div>";
    }

}

public function preencheGrid()
{
    return $this->dao->preencheGrid();
}

public function getAll()
{
    return $this->dao->getAll();
}

public function showDadosForm()
{
    $id = $_GET['id'];
    $this->fieldvalue = $this->dao->getById($id);
    //var_dump($this->fieldvalue);
}

public function recebeDados()
{
    $this->questoes->setQuestoes($_POST['txt_questao']);
    $this->questoes->setDescQuestoes($_POST['desc_questao']);
    $this->questoes->setOrdem($_POST['ordem']);
}

public function remover() 
{
    $id = $_GET['id'];
    return $this->dao->remove($id);
}


public function chamaFormulario()
{
    $list = $this->dao->getAll();

    foreach($list as $questoes) {
        $valores = array('idQUESTAO1'=> '1' , 'QUESTOES1'=>'O que vc acha dos serviços prestados pela nossa empresa?','DESC_QUESTAO1'=>'primeira questao','ORDEM1'=>'1',
          'idQUESTOES2'=>'2','QUESTOES2'=>'Como vc analisa a empresa','DESC_QUESTAO2'=>'segunda questao','ORDEM2'=>'2'                  
        );

    }

    return $valores['QUESTOES1']."<br/><br/>";

}   

}

$class = new QuestoesController();

Well in the method callFormula I created an associative array in it I need to specify fields of the table questions

idQuestions => '1', 'QUESTOES' = > $ questoes [field1data here]

asked by anonymous 11.11.2015 / 18:58

2 answers

-2

You can do this, the $ d variable will use the array you created. Example, this will work out.

<?php
//array
$d = $arrayName = array('A' => 1,'B'=>2 );

foreach ($d as $key => $value) {
    echo $key.'=>'.$value.'<br>';
}
/*
vai imprimir isso
chave A=> valor 1
chave B=> valor 2
*/
?>
    
11.11.2015 / 19:18
-2

I just printed, but then you can do the treatment as you need to return the data, if it is in array form or just like this.

<?php
public function getAll()
{
    //array
    $d =  $this->dao->getAll();
    foreach ($d as $key => $value) {
        echo $key.'=>'.$value.'<br>';
    }
}

/*
vai imprimir isso
idQuestoes=>1
QUESTOES=> questoes
*/
?>
    
11.11.2015 / 19:33