Jump to content

[SOLVED] Increase image views count


MaDSEGA

Recommended Posts

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  ;D ). 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

Link to comment
https://forums.phpfreaks.com/topic/115686-solved-increase-image-views-count/
Share on other sites

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

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.