How to send back my array’s response to views? and appear within td

Asked

Viewed 65 times

0

//VIEWS FOLDER

<?php
$botao="Sorteio";
$action="sorteio1";
?>
<!doctype - html5>
<html>
    <head>
    <title>Array</title>
        <link rel="stylesheet" type="text/css" href="css/style_home.css">
        <meta charset="utf-8">
    </head>
    <body>
        <form name="frmsorteio" method="post" action="router.php?controller=sorteio&modo=<?php echo($action)?>">
            <input class="botao1" type="submit" name="btn_prymari" value="<?php echo($botao)?>">
        <table class="tabela">
            <tr>
                <td class="colunas"></td>
                <td class="colunas"></td>
                <td class="colunas"></td>

//ROUTER.PHP

<?php

    $controller=$_GET['controller'];
    $modo=$_GET['modo'];

    switch($controller)
    {
        case 'sorteio':

            require_once('controllers/sorteio_controller.php');
            require_once('models/sorteio_class.php');

            switch($modo)
            {
                case 'sorteio1':
                    $controller_sorteio = new ControllerSorteio();
                    $controller_sorteio->Sorteio1();
                    break;
            }
    }

?>

//FOLDER CONTROLLERS

<?php
 class ControllerSorteio{

        public function Sorteio1(){

            if($_SERVER['REQUEST_METHOD']=='POST'){
              require_once('models/sorteio_class.php');
            }
        }
    }
?>

//MODEL FOLDER

<?php
class CodeGen{
    private $codes = array();
    public function __construct($codes) {
        $this->codes = $codes;
}
    public function getRandomCode($min, $max){  
       $next = 60; 
            while (count($this->codes) < $next) {
                $code = mt_rand($min, $max);    
                if (!in_array($code, $this->codes)) {           
                    $this->codes[] = $code;   
         }      
    }   
}   
    public function getLastCode(){
        return ($this->codes);   
    }
}
    $codes = array();
    $CodeGen = new CodeGen($codes);
    $CodeGen->getRandomCode(0, 60);
    print_r $CodeGen->getLastCode();
?>

2 answers

0

I believe, at least for now, that your code does not need a model, just a view and a controller, but leaving it in the same pattern as you are doing:

View:

<?php
    session_start(); //função necessária para usar a variável global $_SESSION, deve ser chamada antes de qualquer outro código php

    $botao="Sorteio";
    $action="sorteio1";
?>
<!doctype - html5>
<html>
    <head>
    <title>Array</title>
        <link rel="stylesheet" type="text/css" href="css/style_home.css">
        <meta charset="utf-8">
    </head>
    <body>
        <form name="frmsorteio" method="post" action="router.php?controller=sorteio&modo=<?php echo($action)?>">
            <input class="botao1" type="submit" name="btn_prymari" value="<?php echo($botao)?>">
            <?php
                //Verifica se existe o array_sorteio e se ele não está vazio para criar a tabela
                if(isset($_SESSION["array_sorteio"]) && $_SESSION["array_sorteio"] != "") {
            ?>
                <table class="tabela">
                    <tr>
                        <?php 
                            $array = $_SESSION["array_sorteio"]; //Atribui a variável array o valor que está na sessão "array_sorteio"
                            unset($_SESSION["array_sorteio"]); //Destroi a posição "array_sorteio" da variável $_SESSION

                            for($i = 0; i < count($array); $i++) {
                                echo "<td>".$array[i]."</td>";
                            }
                        ?>
                    </tr>
                </table>
            <?php
                }
            ?>

Router.php:

<?php
    session_start(); //função necessária para usar a variável global $_SESSION, deve ser chamada antes de qualquer outro código php

    $controller=$_GET['controller'];
    $modo=$_GET['modo'];

    switch($controller) {
        case 'sorteio':

            require_once('controllers/sorteio_controller.php');
            //require_once('models/sorteio_class.php'); - Retirar essa linha, o model vai ser inserido abaixo

            switch($modo) {
                case 'sorteio1':
                    $controller_sorteio = new ControllerSorteio();
                    //Aqui é inserido o model
                    if($controller_sorteio->Sorteio1()) {
                        //Se o model foi inserido..
                        $codes = array();
                        $CodeGen = new CodeGen($codes);
                        $CodeGen->getRandomCode(0, 60);
                        $array_sorteio = $CodeGen->getLastCode(); //Pega o array gerado no model e atribui a variável $array_sorteio

                        $_SESSION["array_sorteio"] = $array_sorteio; //Cria uma posição "array_sorteio" no array $_SESSION e atribui a ele o array gerado anteriormente
                        header("location: ./camiho/view.php"); //Envia o usuário para á pagina selecionada
                    }
                    break;
            }
    }

?>

Controller:

<?php
    class ControllerSorteio {
        public function Sorteio1() {
            if($_SERVER['REQUEST_METHOD']=='POST') {
                require_once('models/sorteio_class.php');

                //Cria um retorno para saber se o model foi (true) ou não (false) inserido
                return true;
            } else {
                return false;
            }
        }
    }
?>

Model:

<?php
    class CodeGen {
        private $codes = array();

        public function __construct($codes) {
            $this->codes = $codes;
        }

        public function getRandomCode($min, $max){  
            $next = 60; 
            while (count($this->codes) < $next) {
                $code = mt_rand($min, $max);    
                if (!in_array($code, $this->codes)) {           
                    $this->codes[] = $code;   
                }      
            }   
        }   

        public function getLastCode(){
            return ($this->codes);   
        }
    }

    /* Esse bloco de código deve ser chamado dentro do router.php e não abaixo da classe do model
    $codes = array();
    $CodeGen = new CodeGen($codes);
    $CodeGen->getRandomCode(0, 60);
    print_r $CodeGen->getLastCode();
    */
?>

I won’t be able to test the code but it’s more or less that (I would do) good luck, anything is just ask

  • OK, I’ve already put the full code of the models, I just don’t understand where I will create the $_SESSION, in the controllers?

  • Okay, your code is a little fuzzy and I won’t be able to come up with an answer now I have to leave but tomorrow I edit my answer and warning. When asking a question or editing leave your code idented correctly and with 4 extra spaces at the beginning of each line (to leave in code block format)

  • @Bruna edited the answer with the way I would do

  • William thank you very much, the code was perfect I just had to make some increments but everything worked out, thank you very much.

  • If you have resolved your doubts of this question, end with accepting an answer

-1

I could do it this way Before the Html tag, inside php:

try{
    require('conexao.php'); // Chama seu arquivo de conexão
    $sql = "Sua SCRIPT"; // Cria Script
    $stmt = $conexao->prepare($sql); //Prepara sua script com sua conexão
    $stmt->execute(); //Executa
}
catch(PDOExeption $e){
    echo " ".$e;
}

After the html tag:

<html>
<head>
<title>Array</title>
    <link rel="stylesheet" type="text/css" href="css/style_home.css">
    <meta charset="utf-8">
</head>
<body>
<?php
  while($consulta = $stmt->fetch()){
?>
    <form name="frmsorteio" method="post" action="router.php?
 controller=sorteio&modo=<?php echo($action)?>">
        <input class="botao1" type="submit" name="btn_prymari" value="<?php 
 echo($botao)?>">
    <table class="tabela">
        <tr>
            <td class="colunas"><?php echo $consulta["nomecoluna"];?></td>
            <td class="colunas"><?php echo $consulta["nomecoluna2"];?></td>
            <td class="colunas"><?php echo $consulta["nomecoluna3"];?></td>

Don’t forget to close on after the tr tag

<?php
   }
?>
  • The way it is, if the cunsult returns more than one line, it will create various Formulars and tables instead of just adding the line, besides the idea (as far as I can see from the code) is to keep the parts of it separate (files with model, others with the controller and others with the view)

  • is giving error only in variable #sql, do not know what to put in the script

  • thank you very much Gabriel

Browser other questions tagged

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