imperium2335 Posted March 26, 2009 Share Posted March 26, 2009 Hi guys, I'm trying to make something that remembers what the user has viewed previously (using an image id list that builds as the user rates each new picture). But all its doing is showing the current image id, here is my code: <?PHP session_start() ; if (!file_exists("php/dbconnect.php")) { die("Database settings not found, administrator intervention required.") ; } else { require("php/dbconnect.php") ; //Must connect to the database. } $_SESSION['idview'] = NULL ; $countq = ("SELECT image_id FROM image_bank") ; //Count how many cakes are in the base first. $count = mysql_query($countq) ; $numrows = mysql_num_rows($count) ; $image_select = rand(1,$numrows) ; //Assign the max random number based on how many results there are. $query = "SELECT * FROM image_bank WHERE image_id = $image_select" ; $result = mysql_query($query) ; while($row = mysql_fetch_array($result)) { $dbtitle = $row['title'] ; $imgsrc = $row['image_url'] ; if(empty($_SESSION['idview'])) { $_SESSION['idview'] = $row['image_id'] ; } else { $_SESSION['idview'] .= " " ; $_SESSION['idview'] .= $row['image_id'] ; //Add the image id to what the user has viewed. } } echo $_SESSION['idview'] ; include("php/dbdisconnect.php") ; ?> Thanks in advance! Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 [EDIT] Instead of: $countq = ("SELECT image_id FROM image_bank") ; //Count how many cakes are in the base first. $count = mysql_query($countq) ; $numrows = mysql_num_rows($count) ; $image_select = rand(1,$numrows) ; //Assign the max random number based on how many results there are. you can do this which is lighter and faster query: $countq = ("SELECT COUNT(image_id) as imgCount FROM image_bank") ; //Count how many cakes are in the base first. $count = mysql_query($countq) ; $numrows = mysql_fetch_assoc($count) ; $image_select = rand(1,$numrows['imgCount']) ; //Assign the max random number based on how many results there are. [EDIT] Or much better... since what you want to do is get the random image from the database... you can randomly get one using only query nothing more: $query = "SELECT * FROM image_bank ORDER BY RAND() LIMIT 1;" ; With this... you could eliminate a lot of codes with one query and get everything you need. Hope this helps. Jay, Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted March 26, 2009 Author Share Posted March 26, 2009 Thanks Jay, I will try this out tomorrow. My question was more specifically aimed at the bit im trying to make that prevents the user from being shown an image they have already seen/voted for. For that I need to get the image_ids stuck together in the session var, then i can explode them and pass that into something else. Thanks. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted March 27, 2009 Author Share Posted March 27, 2009 Hi, I tried your idea but I am unable to count what ids were previously viewed. Anyway i discovered and fixed my problem, I had the session var = NULL every time the page loaded hehe Thanks for your help. Quote Link to comment 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.