存储在数组中的列,但我无法显示数据

时间:2018-04-07 22:58:15

标签: php mysql search

我试图在数组中存储模型中Buscarme方法的数据,然后在视图中打印数据。

这是model / cliente.php

<?php
class cliente
{
    private $pdo;
    public $id;
    public $resultado;
    public $dni;
    public $Nombre;
    public $Apellido;  
    public $Correo;
    public $Telefono;
    public $res;

    public function __CONSTRUCT(){
        try{
            $this->pdo = conectar();
        } catch(Exception $e) {
            die($e->getMessage());
        }
    }

    public function Buscarme($dni){
        try {
            $stm = $this->pdo->prepare("SELECT * FROM cliente WHERE dni = :dni");
            $stm->bindParam(':dni', $dni, PDO::PARAM_INT);
            $stm->execute();    
            $res = $stm->fetchAll(PDO::FETCH_ASSOC);
            $resultado = array();
            $i = 0;
            foreach($res as $row){
                $resultado[$i]['id']       = $row['id'];
                $resultado[$i]['dni']      = $row['dni'];
                $resultado[$i]['Nombre']   = $row['Nombre'] ;
                $resultado[$i]['Apellido'] = $row['Apellido'];
                $resultado[$i]['Correo']   = $row['Correo'] ;
                $resultado[$i]['Telefono'] = $row['Telefono'] ;
            }
        } catch (Exception $ex) {
            die($e->getMessage());
        }
    }
}

此视图/ cliente / cliente.php

<h1 class="page-header">CRUD con el patrón MVC en PHP POO y PDO </h1>
<a class="btn btn-primary pull-right" href="?c=cliente&  =agregar">Agregar</a>
<a class="btn btn-primary pull-right" href="?c=cliente&a=ardila">Ardila</a>
<a class="btn btn-primary pull-right" href="?c=cliente&a=mateus">Mateus</a>
<br><br><br>
<table class="table  table-striped  table-hover" id="tabla">
<thead>
<tr>
<th style="width:180px; background-color: #5DACCD; color:#fff">ID</th>
<th style="width:120px; background-color: #5DACCD; color:#fff">DNI</th>
<th style="width:180px; background-color: #5DACCD; color:#fff">Nombre</th>
    <th style=" background-color: #5DACCD; color:#fff">Apellido</th>
    <th style=" background-color: #5DACCD; color:#fff">Correo</th>
 <th style="width:120px; background-color: #5DACCD;  color:#fff">Telefono</th>            
    <th style="width:60px; background-color: #5DACCD; color:#fff"></th>
    <th style="width:60px; background-color: #5DACCD; color:#fff"></th>
  </tr>
</thead>
<tbody>
<?php foreach($this->model->Listar() as $r): ?>
<tr>
    <td><?php echo $r->id; ?></td>
    <td><?php echo $r->dni; ?></td>
    <td><?php echo $r->Nombre; ?></td>
    <td><?php echo $r->Apellido; ?></td>
    <td><?php echo $r->Correo; ?></td>
    <td><?php echo $r->Telefono; ?></td>
    <td>
    </td>
</tr>
<?php endforeach; ?>
</tbody>
</table> 
<form action="?c=cliente&a=buscame" method="post" >
<input type="text" name="dni" id="dni"/>
<input type="submit" name="boton" id="boton"/>
</form>
<?php 
$this->model->Buscarme($_POST['dni']);
$resultado =  $this->model->resultado;
print_r($resultado);
?> 
</tbody>
</body>
<script  src="assets/js/datatable.js">  
</script>
</html>

当打印模型中的数据显示在页面左上方时,数据应仅显示在搜索按钮下方

1 个答案:

答案 0 :(得分:0)

删除现有的表格数据 - 从 - $this->model->Listar() - 到 - $this->model->Buscarme($_POST['dni'])

修复了表格布局,以便页面正确显示,并在搜索表单下方显示记录。

更新了view/cliente/cliente.php

试试这个:

<h1 class="page-header">CRUD con el patrón MVC en PHP POO y PDO </h1>
<a class="btn btn-primary pull-right" href="?c=cliente&  =agregar">Agregar</a>
<a class="btn btn-primary pull-right" href="?c=cliente&a=ardila">Ardila</a>
<a class="btn btn-primary pull-right" href="?c=cliente&a=mateus">Mateus</a>
<br><br><br>
<table>
<form action="?c=cliente&a=buscame" method="post" >
<input type="text" name="dni" id="dni"/>
<input type="submit" name="boton" id="boton"/>
</form>
</table>

<table class="table  table-striped  table-hover" id="tabla">
<thead>
<tr>
<th style="width:180px; background-color: #5DACCD; color:#fff">ID</th>
<th style="width:120px; background-color: #5DACCD; color:#fff">DNI</th>
<th style="width:180px; background-color: #5DACCD; color:#fff">Nombre</th>
    <th style=" background-color: #5DACCD; color:#fff">Apellido</th>
    <th style=" background-color: #5DACCD; color:#fff">Correo</th>
 <th style="width:120px; background-color: #5DACCD;  color:#fff">Telefono</th>            
  </tr>
</thead>
<tbody>
<?php 
$this->model->Buscarme($_POST['dni']);
$resultado =  $this->model->resultado;
//print_r($resultado);
?> 
<?php foreach($resultado as $r): ?>
<tr>
    <td><?php echo $r['id']; ?></td>
    <td><?php echo $r['dni']; ?></td>
    <td><?php echo $r['Nombre']; ?></td>
    <td><?php echo $r['Apellido']; ?></td>
    <td><?php echo $r['Correo']; ?></td>
    <td><?php echo $r['Telefono']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table> 

</body>
<script  src="assets/js/datatable.js">  
</script>
</html>

我们将返回数据存储在函数变量而不是类变量中。固定它。计数器变量$ i也没有递增,每次重复增加1。

model/cliente.php

更新功能

public function Buscarme($dni){
        try {
            $stm = $this->pdo->prepare("SELECT * FROM cliente WHERE dni = :dni");
            $stm->bindParam(':dni', $dni, PDO::PARAM_INT);
            $stm->execute();    
            $res = $stm->fetchAll(PDO::FETCH_ASSOC);
            $this->resultado = array();
            $i = 0;
            foreach($res as $row){
                $this->resultado[$i]['id']       = $row['id'];
                $this->resultado[$i]['dni']      = $row['dni'];
                $this->resultado[$i]['Nombre']   = $row['Nombre'] ;
                $this->resultado[$i]['Apellido'] = $row['Apellido'];
                $this->resultado[$i]['Correo']   = $row['Correo'] ;
                $this->resultado[$i]['Telefono'] = $row['Telefono'] ;
                $i++;
            }
        } catch (Exception $ex) {
            die($e->getMessage());
        }
    }