Get user id logged in

2

How about, I'm trying to get the id of the logged in user within a function to be able to create a query (I do not know if the way I show it is correct).

What I've been up to now is this:

function obtener_notificaciones($conexion, $us){
    $notu = $conexion->query("SELECT COUNT(*) FROM postula WHERE du_id = $us AND seen = 0");
    $notu = $resultado->fetchAll();
    return ($notu);
    print_r($notu);
}

and I have another function to know if there is a session started which is the following:

   function comprobarSession(){
    if (!isset($_SESSION['usuario'])) {
        header('Location: ' .  RUTA . '/login.php');
    }
    else {
        $us = $_SESSION['usuario'];
    }
}

What I also want in case my function is correct, is to print the result of $notu within a div

<li><a href="notificaciones.html"> Notificaciones (5)</a></li>

so you can replace (5) with count of query

where I want to show the count is in lateral.php in which I only have HTML e-books:

<div class="block-section text-center ">
  <img src="images/<?php echo($_SESSION['usuario'][14])  ?>" class="img-rounded" alt="">
  <div class="white-space-20"></div>
  <h4><a href="perfil.php"><?php echo($_SESSION['usuario'][3])  ?></a></h4>
  <div class="white-space-20"></div>
  <ul class="list-unstyled">
    <li><a href="mensajes.php"> Mensajes </a></li>
    <li><a href="notificaciones.html"> Notificaciones (5)</a></li>
    <li><a href="cambio.php"> Cambiar contraseña</a></li>

  </ul>
  <div class="white-space-20"></div>
</div>
</div>
<div class="col-md-9 col-sm-9">
<!-- Block side right -->

in the tags PHP what I do is call the array of $_SESSION and print the values of the array as can be seen, so with the function created above I want to change the (5) by the result of the function that in this case if I'm not wrong would be $count

    
asked by Cesar Gutierrez Davalos 06.06.2017 в 04:49
source

2 answers

0

As simple as executing the function you have created before removing the li of the notifications and writing the result inside the html

<?php
// Reemplaza $us por el $_SESSION['usuario'][X] que corresponda
$notu = obtener_notificaciones($conexion, $us);
?>

<li><a href="notificaciones.html"> Notificaciones (<?=$notu;?>)</a></li>
    
answered by 07.06.2017 в 09:36
-1

Do a query to your user table and bring the data you want

$usuario = $_SESSION['usuario'];

$conexion = new PDO('mysql:host=localhost;dbname="tubd"', 'user', 'pass');

$statement = $conexion->prepare('SELECT * FROM usuarios WHERE user_name = :usuario'); 
$statement->execute(array(':usuario' => $usuario));

$resultados = $statement->fetchAll();

foreach ($resultados as $resultado) {
    $id = $resultado['id_usuario'];
    $nombre = $resultado['nombre'];
    $apellido = $resultado['apellido'];
}
    
answered by 05.10.2018 в 05:13