473,405 Members | 2,141 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,405 software developers and data experts.

how to display counter immediately after update

Hi again, I am trying to keep a track of times a file is downloaded and present the count alongside the download link.For example

I have a download page which shows a table containing downloadable files like this.
----------------------------------------------
|filename | description | times downloaded |
----------------------------------------------

I have a datbase storing the filename(fname) and times downloaded (count). Embedded in a table cell is php code to lookup the filename, get the count and echo the count to display times downloaded.
When clicking the download link in the table (filename) I call another page (filename.php), this page opens the db, looks for the filename and increments the count by 1 then calling the file for download (using header)and finally including the original download page.
The problem is that although count updates properly and header does indeed call the download dialogue box, when redisplaying the downloads page either by using include or using the back button on browser, the count shown for the filename is not updated. Strangely refreshing the page via browser button does display the correct count?
My code is below.

My choose file to download page.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost";
  3. $user="root";
  4. $pass="";
  5. $conn=mysql_connect($host,$user,$pass);
  6. mysql_select_db("teg",$conn);
  7. ?>
  8. <html>
  9. <head><title>Downloads</title><base target="_self"></head>
  10.  
  11. <body>
  12. <p align="center"><u><font size="5"><b>Downloads</b></font></u></p>
  13. <div align="center">
  14.     <table border="1" width="74%" id="table1">
  15.         <tr>
  16.             <td align="center" width="86"><a href="nickint.php">Nickint</a></td>
  17.             <td align="left" width="419" colspan="3"><font size="2">An interview with Nick Elizar, Grand 
  18.             Master &amp; founder of World Nickelstick Eskrima Club
  19. <?php
  20. $sql="SELECT * FROM downs WHERE fname='nickint'";
  21. $result=mysql_query($sql,$conn) or die(mysql_error());
  22. $rows=mysql_num_rows($result);
  23.  
  24. while($newarray=mysql_fetch_array($result))
  25. {
  26.     $fname=$newarray['fname'];
  27.     $count=$newarray['count'];
  28.  
  29.     echo 'Downloaded '.$count.' times.';
  30. }
  31.  
  32. mysql_close($conn);
  33. ?>
  34.  
  35.             </font></td>
  36.         </tr>
  37.  
  38.     </table>
  39. </div>
  40. <p align="center">&nbsp;</p>
  41. &nbsp;</body>
  42.  
  43. </html>
  44.  
Clicking the file for download then calls filename.php (in this case nickint.php, each file has its own download page!).
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost";
  3. $user="root";
  4. $pass="";
  5. $conn=mysql_connect($host,$user,$pass);
  6. mysql_select_db("teg",$conn);
  7. // now search for entry
  8. $sql="UPDATE downs SET count=count+1";
  9. $result=mysql_query($sql,$conn) or die(mysql_error());
  10. mysql_close($conn);
  11. header("Location:nickint.rar");
  12. include("downloads.php");
  13. ?>
  14.  
  15. Database is called teg,has one table called downs.
  16. Table has two fields fname and count.
  17.   Can anyone please point me to how to get the update to display. the user needs to be returned to the download screen, perhaps to download another file. I guess that the next time the user returns to the site as a whole the corredct count will be displayed but why does it not display during the same viewing session.
  18. Thanks in advance for any help.
  19.  
  20.  
Sep 14 '10 #1

✓ answered by dlite922

You can use JavaScript to update the count on the page or AJAX to query the DB if you like.

I'm curious as to why is it so important for the count to go up live.

If a file has been downloeaded 143 times. Does the user really care if this didn't change to 144 times?

The next user going to the page (almost immediately after DB updated) will see 144. Does it really change his mind about downloading if its 143 vs 144?


Dan

7 1836
Markus
6,050 Expert 4TB
There's nothing strange about it - the 'back' button on a browser returns you to a cached page, that is, how the page appeared when you left it last.
Sep 14 '10 #2
so if thats the case, using the reload button on the browser causes a fetch from the server?
But that does's not help with my situation.
Anyone got an answer to the problem please?
Us beginners must seem a little slow to you guys who have been doing this awhile but please reply with some useful pointers.
Sep 14 '10 #3
Markus
6,050 Expert 4TB
What I'm saying is, you're making an issue out of something that shouldn't be an issue. If the user hit's the back button, it's typical behaviour that they'll get an old, cached version of the page. To update the page, it must be reloaded. Either way, the user experience really isn't impaired by this. You don't have (much) control over the browser and what it caches.
Sep 14 '10 #4
dlite922
1,584 Expert 1GB
You can use JavaScript to update the count on the page or AJAX to query the DB if you like.

I'm curious as to why is it so important for the count to go up live.

If a file has been downloeaded 143 times. Does the user really care if this didn't change to 144 times?

The next user going to the page (almost immediately after DB updated) will see 144. Does it really change his mind about downloading if its 143 vs 144?


Dan
Sep 14 '10 #5
Ok , so what you guys are saying is that it is accepted practice for such counts not to update live? I agree that the user experience does not suffer as a consequence but, as I am new with php and wish to take as much advantage as I can of it(because it is offered by my isp may as well use it)I am just trying to implement the most complete use of it. It woud be nice if things were updated live but if its not to be then its not to be. I don't want to use javascript because some folks may not have it turned on! plus I am supposed to be learning php.
It seems then that using the include function also causes the browser to load from its cache? Even the header(location:????) function?
Well thanks for the heads up and whilst we are talking, is it a good idea for a beginner to start out by using a framework (such as codeigniter) to establish good coding practices, before too many bad habits are learnt?
Sep 15 '10 #6
dlite922
1,584 Expert 1GB
No one ever turns of JavaScript (unintentionally). Those who do know they're practically browsing the text based internet. They might as well get the internet in the mail like a magazine. I stopped worrying about this 10 years ago.

Learn PHP and good programming practices before you starting using frameworks. There are coding standards and security "gotcha"s you should learn and discover on your own.

In my opinion, if you learn PHP through a framework then you'll just truly know it through a framework. Although I do like frameworks, they can be abused by beginners and most of their advantages can go out the window.

Back to your problem, JavaScript makes the web feel more alive and dynamic. If you want a site that truly feels like a web application, you'll do almost everything in the front-end in JavaScript, or a JavaScript framework like prototype or jQuery.

Dan
Sep 20 '10 #7
If I write a prog in VB or c then pass variables between subs and modules etc but the whole (90% anyhow) thing would be in one file. Because my php would be spread across maybe many pages and obviously a website cannot be written on just one page(suppose it could but hell to update / edit) do I use session variables and post to pass around my data all around the site? Just want to be clear before I embark on doing it this way and then find there is a better way.
Sep 21 '10 #8

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

Similar topics

7
by: jason | last post by:
Is there a way - possibly a disconnected rs? - to update the contents of an existing pulldown on a page without having to re-submit the page for the user to see the pulldown populated with an...
5
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN'...
4
by: Paul LeBlanc | last post by:
I have some images that move positions fine using onmousedown or onmousemove to feed a function the coordinates. It doesn't work when I plug it into conditional loops with setTimeout to set the...
5
by: sparks | last post by:
After trying a combo box to do this. its tied to a lookup table name value yes 1 no 0 they wanted to be able to type in a 1 but display a yes in the field. and store a 1 in the...
3
by: kaiser | last post by:
Hello, Am new to C# and am playing around trying to get a while loop to display a int on a windows console using label. private void btnGo_Click(object sender, System.EventArgs e) { int Counter...
4
by: Ian Davies | last post by:
Hello I have seen some tutorials to put a update a counter field in a record. I have the counter field in a table that also has a field for a path to file. I display the records in a table on...
19
by: jimatqsi | last post by:
I'm using Access 2000 (and also 2003 with same result) in a Windows XP Professional environment on a peer-to-peer network. Files in this system are relatively small. There are about 1,200 rows in the...
12
by: devospice | last post by:
Hi, I'm trying to create a download counter for individual files on a web site and I'm not sure how to do this. Right now I'm using Webalizer to just read the log files and see how many times...
1
by: FordPrefect | last post by:
I am using a 3rd party dll (not COM). One of the dll functions requires a callback function. I have used either DllImport or Declare to properly (I think) declare the function. The function...
7
by: sajit | last post by:
Hi Folks, Right now I am working on eMail Marketing System. There I have a module which sends news letters to subscribers. The problem is I want to put a counter on a page to display sent mails...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.