Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.