Php for / switch

0

What the function does is show a warning on the screen with the user name and a link to your profile, after a few seconds the notice is closed.

What the switch should do is show the information of the user 1 and at the end of the animation show the information of the user 2 but what it does in fact is to show the information of the first user and when the animation ends, show the information again from the first instead of the second.

What am I doing wrong?

function notificacion($connection, $usuario_id){
    $usuario_sql = "SELECT * FROM usuario WHERE usuario_id = '$usuario_id'";
    $usuario_resultado = mysqli_query($connection, $usuario_sql);

    if ($usuario = $usuario_resultado->fetch_assoc()){
        echo '
        <a href="../usuario.php?id=' . $usuario['usuario_id'] . '">
        <div id="notificacion2" title="Ver usuario">

        <span class="usuario"><i class="fa fa-user ' . $usuario['usuario_nombre'] . '"></i></span>
        </div><!-- notificacion --></a>

        <script>
        $("#notificacion2").delay(1000).animate({width: "toggle"}).delay(3000).fadeOut(1000);
        </script>';
    }
}


for ($i=0;$i<3;$i++){

switch ($i){

    case 1:
    // 1
        notificacion($connection, 1);
        break;

    case 2:
    // 2
        notificacion($connection, 2);
        break;      
}
}
    
asked by Kakotas7 05.02.2018 в 21:29
source

2 answers

1

The error is that you are thinking that when you print a script with animation code with a delay interval or things like that belong to the client side echo '<script></script>' , the execution of script % will stop or stop PHP , which does not happen like this. the execution of the script is executed indistinctly of these operations.

Which will not have the result you want, you could use sleep but it is also not advisable, instead you could make a call Ajax and return all users, or a call for id , and already with those data that the call returns, in the client it will be able to realize the animation that it requires.

    
answered by 05.02.2018 в 22:38
0

I'm not used to procedural mode, but I think these two lines are giving you problems:

$usuario_resultado = mysqli_query($connection, $usuario_sql);

    if ($usuario = $usuario_resultado->fetch_assoc()){

Try this way, and by the way we use it in all object-oriented mode. In your code you are mixing the two styles and that is not recommended, because you will be writing confusing programs for yourself and for others.

function notificacion($connection, $usuario_id){
    $usuario_sql = "SELECT * FROM usuario WHERE usuario_id = '$usuario_id'";

    if ($usuario_resultado = $connection->query($usuario_sql)) {
        /* obtener un array asociativo */
        while ($usuario = $usuario_resultado->fetch_assoc()) {
            echo '
            <a href="../usuario.php?id=' . $usuario['usuario_id'] . '">
            <div id="notificacion2" title="Ver usuario">

            <span class="usuario"><i class="fa fa-user ' . $usuario['usuario_nombre'] . '"></i></span>
            </div><!-- notificacion --></a>

            <script>
            $("#notificacion2").delay(1000).animate({width: "toggle"}).delay(3000).fadeOut(1000);
            </script>';

        }
    }
}
    
answered by 05.02.2018 в 22:16