473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is this script OK?

155 100+
[PHP]<?php
$query = "SELECT viewed FROM $j where id='$file'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$viewed = $row['viewed'];
}

if ($viewed == '0')
{
$addone = '1';
}
else
{
$addone = $viewed + 1;
}

mysql_query("UPDATE $j SET viewed='$addone' WHERE id=$file");
?>[/PHP]

I want to know how many times an ad is viewed on my website. So I want the number stored in the row - $viewed - to increase by one each time the ad it's associated with is viewed. The default value is "0" so when the ad is viewed for the first time "0" is changed to "1" and then it increases by one each time it's viewed.

I just want to make sure that I have this right, because I wonder if sometimes the number isn't being increased by 2, 3 or more.
Jul 19 '08 #1
5 1243
Markus
6,050 Expert 4TB
Looks fine to me.

The checking of whether the 'viewed' is 0 is pointless, though.

You can increment a number, instead of using '+ 1', by doing $var++ or ++$var.
Jul 19 '08 #2
DavidPr
155 100+
The checking of whether the 'viewed' is 0 is pointless, though.
Yes, I guess it is.

You can increment a number, instead of using '+ 1', by doing $var++ or ++$var.
So all I really need to do is implement the update query.
[PHP]mysql_query("UPDATE $j SET viewed='$viewed+1' WHERE id=$file");[/PHP]How would I write the update query above to change the +1 to $var++?
Jul 20 '08 #3
DavidPr
155 100+
How about this:
[PHP]function increment(&$viewed, $amount = 1)
{
$viewed = $viewed +$amount;
}[/PHP]

And then the update process:
[PHP]<?php
$query = "SELECT viewed FROM $j where id='$file'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$viewed = $row['viewed'];
}

increment($viewed);
$viewed = $viewed;

mysql_query("UPDATE $j SET viewed='$viewed' WHERE id=$file");
?>[/PHP]

By creating the increment function I can use it on other pages where I want to increase a database entry by one.
Jul 20 '08 #4
Markus
6,050 Expert 4TB
For simplicity's sake, you could just do (using $var++):

[php]
mysql_query("UPDATE $j SET viewed='$viewed++' WHERE id=$file");
[/php]
Jul 20 '08 #5
DavidPr
155 100+
Yes, that does simplify things. Thank you.
Jul 20 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java...
1
by: bayouprophet | last post by:
Cant get menu script to to put linked page in the same frame. I am new to Java and I am wondering what am I doing wrong? below are my java applet file, frame.html file, and my text file and one...
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
2
by: bilaribilari | last post by:
Hi all, I am using Tidy (C) for parsing html pages. I encountered a page that has some script as follows: <script> .... var abc = "<script>some stuff here</" + "script>"; .... </script>
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
3
by: rsteph | last post by:
I have a script that shows the time and date. It's been working on my site for quite a while now. Suddenly it stops showing up, after getting my drop down menu to work. If I put text between the...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
7
by: imtmub | last post by:
I have a page, Head tag Contains many Scripts and style sheet for Menu and Page. This code working fine and displaying menus and page as i wanted. Check this page for reference....
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.