代码之家  ›  专栏  ›  技术社区  ›  Ives Rodriguez

数组中存储了列,但无法显示数据

  •  -1
  • Ives Rodriguez  · 技术社区  · 7 年前

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

    这是型号/客户。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());
            }
        }
    }
    

    此视图视图视图/客户端/客户端。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 回复  |  直到 7 年前
        1
  •  0
  •   Karlo Kokkak    7 年前

    已删除现有表数据-来自- $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());
            }
        }