0

I have got a database called usermovie where i have stored info about movie (like name, genre, pic, title, fav, etc). When i tried to echo movie pics which are user favourite using currently logged in user id as condition it displays all images perfectly in php page but when i tried to use session variable and echo it in div tag it shows only one pic why is that happening can ayone help me with this ?

my code

usermovie.php

<?php
require_once 'db.php';

$id = $_SESSION['id'];
$moviefav = "Yes";

$query = "SELECT movie_pic FROM usermovie WHERE id=? AND movie_fav=?";
$stmt = $conn->prepare($query);
$stmt->bind_param('is', $id, $moviefav);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
    echo "<img src=" . $row['movie_pic'] . ">";

$usermovieFav = "<img src=" . $row['movie_pic'] . ">"
 $_SESSION['usermovieFav'] = $usermovieFav;

}
?>

I have stored five movies and selected all five movies as fav above code perfectly displays all five pics when i open usermovie.php but when i use the above session variable and try to echo it in div tag it only shows one pic which is stored last in database why is that happening i want to display all five pics in index.php

index.php

<div>
<?php echo $_SESSION['usermovieFav']; ?>
</div>

Thank you

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Do all session arrays contain value? – Funk Forty Niner May 16 '20 at 14:09
  • @Funk Forty Niner, Thank you for response can you give me any reference on how can i do it ? – user12345678 May 16 '20 at 14:11
  • multi means arrays, so use `$_SESSION['usermovieFav'][] = $usermovieFav`, what are you trying to achieve with session var? – Lawrence Cherone May 16 '20 at 14:11
  • @Lawrence Cherone, Thank you for response ofcourse am trying to use session variable to display its value in another page for a particular logged in user – user12345678 May 16 '20 at 14:14
  • abstract the logic/call2db into a reusable model, and call it again where its needed, much nicer then storing in session.Though you can use a session var but if its multiple items you need store it as an array, like comment above.. then you need loop over it to display it, with a foreach. – Lawrence Cherone May 16 '20 at 14:18
  • That wasn't the response I was looking for, but here you go though https://stackoverflow.com/questions/5489365/how-to-use-store-and-use-session-variables-across-pages – Funk Forty Niner May 16 '20 at 14:21
  • @Funk Forty Niner Yeah i know that shoudn't ask much but am trying to code with basic knowledge of php – user12345678 May 16 '20 at 14:23
  • Can anyone explain to me why is it not displaying all images when placed in div tag and why it is working perfectly when i run it outside div tag – user12345678 May 16 '20 at 14:35
  • About that ^. You're most likely outputting before header. Enabling error reporting will tell you whether or not you are, but I am betting on the former. – Funk Forty Niner May 16 '20 at 14:49

0 Answers0