hoponhiggo Posted April 17, 2011 Share Posted April 17, 2011 Hi Guys Hoping somebody can help me again please? I have some code which calls and displays all images in the file structure on my webpage. <?php //to display image from source $dir = "profpics/"; //opening directory if ($opendir = opendir($dir)) { //reading directory while (($file = readdir($opendir)) !== FALSE) { if ($file!="."&&$file!="..") echo "<img src = '$dir/$file' img width='38' img height='38'><br>"; } } ?> This works fine, but what i want is for only the image that relates to the logged in user to be displayed. Both the user id and image id are stored in the same row of my database Sorry if this is confusing. Can anybody help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233979-php-echo-image/ Share on other sites More sharing options...
Skewled Posted April 17, 2011 Share Posted April 17, 2011 set the user id in a $_SESSION['userid'] variable and then query using the session for a match to the image id. Quote Link to comment https://forums.phpfreaks.com/topic/233979-php-echo-image/#findComment-1202633 Share on other sites More sharing options...
hoponhiggo Posted April 17, 2011 Author Share Posted April 17, 2011 Hello and thanks for the reply! I'm sorry but I'm a beginner with php an don't quite fully understand your answer. Could you please Ellaborate a little? I have tried to set a variable with a statement to say something like "select * from profile_pics where username = $session [mm_username] but as you probably k ow this didn't work! Quote Link to comment https://forums.phpfreaks.com/topic/233979-php-echo-image/#findComment-1202651 Share on other sites More sharing options...
Skewled Posted April 17, 2011 Share Posted April 17, 2011 Both the user id and image id are stored in the same row of my database The code you posted isn't working with a database. All I can see is that your opening a directory of images and making sure the file isn't . or .. upon $dir returning TRUE. So where is that code? Do you establish cookies or sessions for the logged in users? If you do then you can add a session variable for the userid.. and anywhere you want to use that the first line in your script will be session_start(); THEN you can query the db for the userid THEN you can save a value to the session: $_SESSION['userid'] = $row['userid'] or you could simply store the user's image path into a session variable and echo it to display the image, using the same method as above. Quote Link to comment https://forums.phpfreaks.com/topic/233979-php-echo-image/#findComment-1202653 Share on other sites More sharing options...
hoponhiggo Posted April 18, 2011 Author Share Posted April 18, 2011 Do you establish cookies or sessions for the logged in users? Yes, the user must be logged in to see the page. Could you be so kind as to provide an example of the code i could use? Quote Link to comment https://forums.phpfreaks.com/topic/233979-php-echo-image/#findComment-1202903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.