how to display counter immediately after update

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • malcolmk
    New Member
    • Sep 2010
    • 79

    #1

    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.
    Code:
    <?php
    $host="localhost";
    $user="root";
    $pass="";
    $conn=mysql_connect($host,$user,$pass);
    mysql_select_db("teg",$conn);
    ?>
    <html>
    <head><title>Downloads</title><base target="_self"></head>
    
    <body>
    <p align="center"><u><font size="5"><b>Downloads</b></font></u></p>
    <div align="center">
        <table border="1" width="74%" id="table1">
            <tr>
                <td align="center" width="86"><a href="nickint.php">Nickint</a></td>
                <td align="left" width="419" colspan="3"><font size="2">An interview with Nick Elizar, Grand 
                Master &amp; founder of World Nickelstick Eskrima Club
    <?php
    $sql="SELECT * FROM downs WHERE fname='nickint'";
    $result=mysql_query($sql,$conn) or die(mysql_error());
    $rows=mysql_num_rows($result);
    
    while($newarray=mysql_fetch_array($result))
    {
        $fname=$newarray['fname'];
        $count=$newarray['count'];
    
        echo 'Downloaded '.$count.' times.';
    }
    
    mysql_close($conn);
    ?>
    
                </font></td>
            </tr>
            
        </table>
    </div>
    <p align="center">&nbsp;</p>
    &nbsp;</body>
    
    </html>
    Clicking the file for download then calls filename.php (in this case nickint.php, each file has its own download page!).
    Code:
    <?php
    $host="localhost";
    $user="root";
    $pass="";
    $conn=mysql_connect($host,$user,$pass);
    mysql_select_db("teg",$conn);
    // now search for entry
    $sql="UPDATE downs SET count=count+1";
    $result=mysql_query($sql,$conn) or die(mysql_error());
    mysql_close($conn);
    header("Location:nickint.rar");
    include("downloads.php");
    ?>
    
    Database is called teg,has one table called downs.
    Table has two fields fname and count.
      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.
    Thanks in advance for any help.
    Last edited by malcolmk; Sep 14 '10, 09:35 AM. Reason: typos
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    Comment

    • malcolmk
      New Member
      • Sep 2010
      • 79

      #3
      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.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        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.

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          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

          Comment

          • malcolmk
            New Member
            • Sep 2010
            • 79

            #6
            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?

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              No one ever turns of JavaScript (unintentionall y). 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

              Comment

              • malcolmk
                New Member
                • Sep 2010
                • 79

                #8
                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.

                Comment

                Working...