RAH Posted August 14, 2007 Share Posted August 14, 2007 Hi, I'm new here! Looks like a great forum - hope to learn lots here. I'm currently trying to get a media script to display the most viewed image. I have worked out a query to do this: 'SELECT * FROM `files` WHERE `category_id` = 5 ORDER BY `files` . `views` DESC LIMIT 0, 1 '; However I'm not sure how to get the appropriate values I need into variables. The above query returns 1 entire row and I have to grab several values from that row (eg. filename). When I get the necessary data into variables I can do things like the following: <p>Top Viewed Image</p> <p> <img src="http://mydomain.com/' . $top_viewed_image . 'jpg '" width="276" height="110" /></p> I'm just sort of lost a bit, would appreciate any help. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/ Share on other sites More sharing options...
suttercain Posted August 14, 2007 Share Posted August 14, 2007 Hello, Just a suggestion and maybe not the most efficient, but you could store the page views (that contains the image) into a database. Each time a user visits the page view is incremented by one. You could also add functionality using the $_SERVER array to ensure that the same person who visits that same image only counts as one. Then in your MYSQL select statement, you can order by page view. Just a thought. Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-323866 Share on other sites More sharing options...
RAH Posted August 14, 2007 Author Share Posted August 14, 2007 The page views are stored in the database, see my query above. I can find what I'm looking for via SQL query but then I need to turn the result values into variables and so on. Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-323870 Share on other sites More sharing options...
emehrkay Posted August 14, 2007 Share Posted August 14, 2007 are you limiting your display to only one image? that is what your query is doing... if not, you could run your qurey then loop through the results displaying an image on each loop $query = "SELECT * FROM files WHERE catageory_id = 5 ORDER BY views DESC"; $result = mysql_query($query) or die (mysql_error()); $count = mysql_num_rows($result); $images = ''; for($i = 0; $i < $count; $i++){ $row = mysql_fetch_assoc($result); $images .= "<img src =\"". $row['columnWithSrc'] ."\" ... />"; } echo "<p>Top Viewed images</p>". $images; something like that should work Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-323872 Share on other sites More sharing options...
RAH Posted August 14, 2007 Author Share Posted August 14, 2007 Hi, I just need the most viewed image displayed (a single image). Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-323876 Share on other sites More sharing options...
emehrkay Posted August 14, 2007 Share Posted August 14, 2007 $query = "SELECT * FROM files WHERE catageory_id = 5 ORDER BY views DESC LIMIT 0, 1"; $result = mysql_query($query) or die (mysql_error()); $count = mysql_num_rows($result); $row = mysql_fetch_assoc($result); $images = "<img src =\"". $row['columnWithSrc'] ."\" ... />"; same code, no loop Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-323877 Share on other sites More sharing options...
RAH Posted August 14, 2007 Author Share Posted August 14, 2007 OK, that makes sense for the most part - how do I go about displaying the actual image in the browser? Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-323889 Share on other sites More sharing options...
suttercain Posted August 14, 2007 Share Posted August 14, 2007 This is the image $images = "<img src =\"". $row['columnWithSrc'] ."\" ... />"; you can now echo the variable that contains that string: <?php echo $images; ?> Obviously you need to replace columnWithSrc' with the actual column name that contains the image name. So which ever column has image.jpg is the column you put. SC Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-324038 Share on other sites More sharing options...
RAH Posted August 14, 2007 Author Share Posted August 14, 2007 OK, here's the current code: $query = "SELECT * FROM files WHERE category_id = 5 ORDER BY views DESC LIMIT 0, 1"; $result = mysql_query($query) or die (mysql_error()); $count = mysql_num_rows($result); $row = mysql_fetch_assoc($result); $images = "<img src =\thumbnails\"". $row['filename'] .png."\" ... />"; echo $images This is outputting the following HTML: <img src = humbnails"91b440ebfbda475a3fd198d608adb449png" ... /> This needs to be thumbnails/91b440ebfbda475a3fd198d608adb449.png Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-324059 Share on other sites More sharing options...
lightningstrike Posted August 14, 2007 Share Posted August 14, 2007 $query = "SELECT * FROM files WHERE category_id = 5 ORDER BY views DESC LIMIT 0, 1"; $result = mysql_query($query) or die (mysql_error()); $count = mysql_num_rows($result); $row = mysql_fetch_assoc($result); $images = "<img src =\"thumbnails/". $row['filename'] .png."\" ... />"; echo $images; Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-324097 Share on other sites More sharing options...
RAH Posted August 14, 2007 Author Share Posted August 14, 2007 That is outputting the following HTML which is still not quite right: <img src ="thumbnails/91b440ebfbda475a3fd198d608adb449png" ... /> Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-324105 Share on other sites More sharing options...
AndyB Posted August 14, 2007 Share Posted August 14, 2007 Should be: $images = "<img src='thumbnails/". $row['filename']. ".png'/>"; Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-324120 Share on other sites More sharing options...
RAH Posted August 14, 2007 Author Share Posted August 14, 2007 Thanks very much! You're all very helpful here. How would I go about hyperlinking this image to the appropriate file in the database? Would I do another query/result/row combo and then edit the $images variable, adding a hyperlink? I have a rough idea... Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-324125 Share on other sites More sharing options...
RAH Posted August 16, 2007 Author Share Posted August 16, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-325301 Share on other sites More sharing options...
suttercain Posted August 16, 2007 Share Posted August 16, 2007 Rah, Do you know HTML or XHTML? Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-325390 Share on other sites More sharing options...
RAH Posted August 16, 2007 Author Share Posted August 16, 2007 Hi, I can do both, just not sure about this particular PHP task. Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-325627 Share on other sites More sharing options...
RAH Posted August 20, 2007 Author Share Posted August 20, 2007 Hi, I'm using the following code: $image = "<a href='http://domain.com/files/". $row['id']. "'/". $row['permalink']. "''><img src='thumbnails/". $row['thumbnail']. "'/></a>"; For some reason all the row variables are being correctly output apart from permalink - why would this be? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-328447 Share on other sites More sharing options...
itsmeArry Posted August 20, 2007 Share Posted August 20, 2007 this is because you are closing ' i.e. quote use this $image = "<a href='http://domain.com/files/". $row['id']. "/". $row['permalink']. "'><img src='thumbnails/". $row['thumbnail']. "'/></a>"; Quote Link to comment https://forums.phpfreaks.com/topic/64909-solved-displaying-most-viewed-image/#findComment-328710 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.