Model View-Controller PHP

0

I'm doing a test web page with the Vista Driver Model, but I'm getting an error that I do not know how to solve. I leave the code to see if you can help me.

Index.php

<html>
    <body>
        <title>
            HUMAN DEVELOPMENT INDEX
        </title>
        <h1><p>HUMAN DEVELOPMENT INDEX</p></h1>
        <form action="controllers/function.php"method="post"/>
              <input type="number" name="value" value="0"><br>
              <input type="radio" name="greater" value=">" checked > Greater than 
              <input type="radio" name="greater" value="<"> Lower than <br>
              <input type="submit" value="submit"><br>
        </form> 
    </body>
</html>

controllers / function.php

<?php
//Llamada al modelo
require_once("../models/modelo.php");
$pai=new modelo();
$datos=$pai->get_paises();

//Llamada a la vista
require_once("../views/vista.php");
?>

models / modelo.php

<?php
class modelo
{
    private $dbh;
    private $paises;

    public function __construct() 
    {
            $this->paises = array();
            $this->dbh = new PDO('mysql:host=localhost;dbname=paises', "root", "");
    }

    private function set_names() 
    {
        return $this->dbh->query("SET NAMES 'utf8'");
    }

    public function get_paises()
    {
        self::set_names();
        $comp = $_POST['value'];
        $consulta="select pais, hdi from paises where hdi > $comp";
        foreach ($this->dbh->query($consulta) as $res) 
        {
            $this->paises[]=$res;   
        }
        return $this->paises;
        $this->dbh=null;
    }
}
?>

views / vista.php

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8" />
    <title>Modelo-vista-controlador</title>
</head>
<body>
    <h1>Paises</h1>
    <table border="1">
        <tr>
            <td><strong>Pais</strong></td>
            <td><strong>HDI</strong></td>
        </tr>
        <?php
            for($i=0;$i<count($pai);$i++)
            {
                ?>
                    <tr>
                        <td><?php echo $pai[$i]["pais"]; ?></td>
                        <td><?php echo $pai[$i]["hdi"]; ?></td>
                    </tr>
                <?php
            }
        ?>
    </table>
</body>
</html>

From what I see, you get to create the table of the view and when you are going to put the first data of the database, the following error jumps:

I hope you can help me. A greeting and thanks;)

    
asked by Alfred 18.02.2018 в 21:23
source

1 answer

3

You are trying to access $pai as if it were an array and it is not, $pai is an instance of modelo . In your view you pass the countries in the variable $datos , so in the view you should use

<?php
        for($i=0;$i<count($datos);$i++)
        {
            ?>
                <tr>
                    <td><?php echo $datos[$i]["pais"]; ?></td>
                    <td><?php echo $datos[$i]["hdi"]; ?></td>
                </tr>
            <?php
        }
    ?>
    
answered by 18.02.2018 / 21:26
source