MaDSEGA Posted July 20, 2008 Share Posted July 20, 2008 Hi there, if someone could help, it would be much appreciated I'm trying to make a script to increase the views count of an image and I just can't seem to get around it (i'm a noob ). I tried to search the forum but i cannot find any relevant solutions. Here it goes: mysql_select_db(); $query_Banners = "SELECT * FROM bannners_image ORDER BY im_id DESC"; $Banners = mysql_query($query_Banners, $username) or die(mysql_error()); $totalRows_Banners = mysql_num_rows($Banners); and when i click the image link, should increase the views count (im_count) while ($row_Banners2=mysql_fetch_array($Banners)) $div_output.=<<<END <div> <a href="images/{$row_Banners2['im_image']}" rel="lightbox" title="{$row_Banners2['im_description']}" target="_blank"> <img src=images/thumbnail/{$row_Banners2['im_thumbnail']} alt="" /></a> <p align="middle">Views:{$row_Banners2['im_count']}</p> </div> END; echo $div_output; Thank you in advance The page is http://www.madeofstars.org/banners.php Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/ Share on other sites More sharing options...
JasonLewis Posted July 20, 2008 Share Posted July 20, 2008 Look up UPDATE for MySQL. $update = mysql_query("UPDATE `bannners_image` SET `im_count`=(`im_count`+1) WHERE `im_id`='{$idvar}'"); Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594754 Share on other sites More sharing options...
MaDSEGA Posted July 20, 2008 Author Share Posted July 20, 2008 Well, i tried every possible combination, put the line in the <a href> still nothing Someone suggested using javascript. But that's another dead end Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594768 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 so you want it for when they on the image to add a count? not when it loads the page with the image? Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594770 Share on other sites More sharing options...
MaDSEGA Posted July 20, 2008 Author Share Posted July 20, 2008 Exactly. When the image is clicked, it should increase the count. Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594771 Share on other sites More sharing options...
JasonLewis Posted July 20, 2008 Share Posted July 20, 2008 You would need to use AJAX, if you want it done client-side. You'll end up using the code I've supplied above. Search around for AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594776 Share on other sites More sharing options...
MaDSEGA Posted July 20, 2008 Author Share Posted July 20, 2008 Thanks guys... i'll try and figure it out :-\ Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594779 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 you don't need ajax, you just need javascript, something like <script type="text/javascript"> function AddCount() { ?> $i++; mysql_query("UPDATE table SET count='{$i}'"); <?php } </script> <img src="" onClick="AddCount()"> that should do the trick Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594787 Share on other sites More sharing options...
wildteen88 Posted July 20, 2008 Share Posted July 20, 2008 no it wont, You cannot mix PHP and Javascript like that. The only way is AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594803 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 i've mixed php and js before and it works fine. Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594817 Share on other sites More sharing options...
wildteen88 Posted July 20, 2008 Share Posted July 20, 2008 I fail to see how. PHP is long done before JavaScript is called. If the following is your PHP script: <script type="text/javascript"> function AddCount() { <?php $i++; mysql_query("UPDATE table SET count='{$i}'"); ?> } </script> <img src="" onClick="AddCount()"> This will be the output (when you view the source code): <script type="text/javascript"> function AddCount() { } </script> <img src="" onClick="AddCount()"> When you click the image, it'll call the AddCount javascript function but nothing else will happen. Yes you can use PHP set JavaScript variables, but you cannot get JavaScript to run snippets of PHP code. The only way is AJAX. PHP is parsed server side, JavaScript is client side (web browser). Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594873 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 yer, what i realised was that the php i use in javascript are static variables. like echo date(); to get the time . Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594909 Share on other sites More sharing options...
MaDSEGA Posted July 20, 2008 Author Share Posted July 20, 2008 Guys nevermind. It's too much trouble. I don't want to see view count anymore ) Thank you all Quote Link to comment https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/#findComment-594995 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.