Doubt of foreach in php

1

I have a question about filling a table through an array with a foreach.

If I do it without a function, it does show me the expected result, but what I want is to use it as a function. For the moment I left it like this:

<?php require_once "../controller/usuarios_controller.php"; ?>
<table align="center" border="1">
        <thead>
            <td align="center"><strong>ID</strong></td>
            <td align="center"><strong>USUARIO</strong></td>
            <td align="center"><strong>CONTRASEÑA</strong></td>
            <td align="center"><strong>DEPARTAMENTO</strong></td>
            <td align="center"><strong>PRIVILEGIO</strong></td>
        </thead>
        <?php $datos = get_usuarios(); ?>
        <?php foreach ($datos as $dato):?>
        <tr>
            <td align="center"><?php echo $dato["id"]; ?></td>
            <td align="center"><?php echo $dato["usuario"]; ?></td>
            <td align="center"><?php echo $dato["contrasena"]; ?></td>
            <td align="center"><?php echo $dato["departamento"]; ?></td>
            <td align="center"><?php echo $dato["privilegio"]; ?></td>
            <td style="width:150px;">
                <a href="adm/editar.php?id=<?php echo $dato["id"];?>">Editar</a>
                <!-- <a class="<?php //echo $pagina == 'editar' ? 'active' : ''; ?>" href="views/editar.php?p=editar&id=<?php //echo $dato["id"];?>" >Editar</a>-->
                <a class="<?php echo $pagina == 'editar' ? 'active' : ''; ?>" href="?p=editar&id=<?php echo $dato["id"];?>">EDITAR</a>
                <a href="#" id="del-<?php echo $dato["id"];?>">Eliminar</a>
                <script>
                $("#del-"+<?php echo $dato["id"];?>).click(function(e){
                    e.preventDefault();
                    p = confirm("¿Está seguro que desea eliminar al Usuario?");
                    if(p){
                        window.location="?p=eliminar&id="+<?php echo $dato["id"];?>;
                    }
                });
                </script>
            </td>
        </tr>
        <?php endforeach;?>
        </table>

Note that I'm calling the controller that has the function and% $datos assigned get_usuarios(); .

But nothing happens, still does not show me the table filled. The file where I have the function get_usuarios(); is like this:

<?php
//Llamada al modelo
require_once("../model/usuarios_model.php");

class usuarios_controller{


    function get_usuarios(){
        $per=new usuarios_model();
        $datos=$per->get_usuarios();

        //Llamada a la vista
        require_once("../view/adm/usuarios_view.phtml");
    }
}
?>

The model does not have any faults since I have tried it with different cases, it is only the controller that does not return anything in sight. Even putting the return $datos; still does not send anything.

Could you explain it to me?

    
asked by A. Hernández 17.12.2016 в 10:21
source

1 answer

1

There are the following errors:

  • In the view, you have not created an instance of usuarios_controller , that is, you need to do:

    $obj = new usuarios_controller();
    
  • In the view, the function get_usuarios is a method of the class usuarios_controller , so it should be invoked like this:

    $datos = $obj->get_usuarios()
    
  • In the controller, get_usuarios should return the variable $datos , that is:

    return $datos; 
    

Recommended reading: Class methods and properties visibility

    
answered by 18.12.2016 / 13:07
source