SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Simple Download count

    Hi All,

    Im try to incorparate a count, so evertime someone clicks a link the database is updated and shows on the display page how many times a file has been clicked/downloaded.

    Here's what i have so far and things are not working how I would like them to, if anyone could offer any suggestions to a newbie that would be great !

    PHP Code:
    $dlc mysql_query("UPDATE kms_dloads SET downloadcount = downloadcount + 1 WHERE downloadcount"); 
    Any help with this would be a great help

    Best Regards
    Deano

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, if you have all of the links stored in the database, you can setup a script which accepts the ID, then go like so:
    PHP Code:
    if (isset($_GET['id']) && ($id = (int) $_GET['id']) && $id!=0)
    {
        
    $url = @mysql_fetch_array(@mysql_query("select url from downloads where id=$id limit 1" ));
        if (
    $url['url'])
        {
            
    mysql_query("update downloads set downloadcount=downloadcount + 1 where id=$id");
            
    header("Location: $url[url]);
        }

    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cyborg, Thanks for the rapid reply !

    Cound you be a little more specific please, you kinda lost me, here's the script so far :
    PHP Code:
    <?php
      
    // includes
    include("conf.php");

    // open database connection
    $connection mysql_connect($host$user$pass) or die ("Unable to connect!");

    // select database
    mysql_select_db($db) or die ("Unable to select database!");

    $kms_dloads = @mysql_query('SELECT ID, name, description, name, url, size, downloadcount FROM kms_dloads');
    $dlc mysql_query("UPDATE kms_dloads SET downloadcount = downloadcount + 1 WHERE downloadcount");
    if (!
    $kms_dloads) {
      die(
    '<p><font color="red" size="2" face="Arial, Helvetica, sans-serif">Error retrieving Links from database!</font><br />'.
          
    'Error: ' mysql_error() . '</p>');
    }
    echo(
    "
    <table width='768' border='0' cellpadding='0' cellspacing='0' bgcolor='#1A1A34'>
      <tr> 
        <td width='205' height='19' valign='top'><font color='#FFFFFF' size='2' face='Arial, Helvetica, sans-serif'><strong><img src='images/spacer.gif' width='10' height='10'>File 
          Name</strong></font></td>
        <td width='244' valign='top'><font color='#FFFFFF' size='2' face='Arial, Helvetica, sans-serif'><strong>Description</strong></font></td>
        <td width='91' valign='top'><font color='#FFFFFF' size='2' face='Arial, Helvetica, sans-serif'><strong>Size</strong></font></td>
        <td width='91' valign='top'><font color='#FFFFFF' size='2' face='Arial, Helvetica, sans-serif'><strong>Download</strong></font></td>
        <td width='94' valign='top'><font color='#FFFFFF' size='2' face='Arial, Helvetica, sans-serif'><strong>Downloaded</strong></font></td>
      </tr>
    </table>"
    );

    while (
    $name mysql_fetch_array($kms_dloads)) {
      
    $id   $name['ID'];
      
    $url htmlspecialchars($name['url']);
      
    $size htmlspecialchars($name['size']);
      
    $downloadcount htmlspecialchars($name['downloadcount']);
      
    $description htmlspecialchars($name['description']);
      
    $name htmlspecialchars($name['name']);
      echo(
    "<table width='768' border='0' cellpadding='0' cellspacing='0'>
      <tr> 
        <td width='205' height='19' valign='top'><font size='2' face='Arial, Helvetica, sans-serif'><img src='images/spacer.gif' width='10' height='10'>
    $name</font></td>
        <td width='244' valign='top'><font size='2' face='Arial, Helvetica, sans-serif'>
    $description</font></td>
        <td width='91'  valign='top'><font size='2' face='Arial, Helvetica, sans-serif'>
    $size</font></td>
        <td width='91'  valign='top'><font size='2' face='Arial, Helvetica, sans-serif'><a href=\"http://
    $url\"> download</a></font></td>
        <td width='94'  valign='top'><font size='2' face='Arial, Helvetica, sans-serif'><center>
    $dlc</center></font></td>
      </tr>
    </table>
    Best Regards
    Deano

  4. #4
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi All,
    I've played with a few ideas and for some reason I can't get it to work.

    If anyone could offer a little assistance I would very much appreciate it.

    This is what I have so far :

    PHP Code:
    // start of download count
    $kms_dloads = @mysql_query('SELECT ID, name, description, name, url, size, downloadcount FROM kms_dloads');
    if (isset(
    $_GET['id']) && ($downloadcount = (int) $_GET['id']) && $id!=0)
    {
        
    $downloadcount = @mysql_fetch_array(@mysql_query("select downloadcount from kms_dloads where id=$id limit 1" ));
        if (
    $downloadcount['downloadcount'])
        {
            
    mysql_query("update downloadcount set downloadcount=$downloadcount + 1 where id=$id);
            
    header("Location: $downloadcount[downloadcount]);
        }

    // end of download count 
    Thx for your time
    Deano

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    header("Location: $downloadcount[downloadcount]" );

    it should be Location :$downloadcount[url], you'll need to select that from the DB as well.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  6. #6
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx Cyborg, I'll give it a try !

    Best Regards
    Deano

  7. #7
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this look something like ?

    PHP Code:
    mysql_query("SELECT ID, FROM kms_dloads UPDATE downloadcount SET downloadcount=$downloadcount + 1 WHERE id=$id);
            
    header("Location: $downloadcount[url] 
    hopeim on the right track

  8. #8
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (isset($_GET['id']) && ($downloadcount = (int) $_GET['id']) && $id!=0

        
    $downloadcount = @mysql_fetch_array(@mysql_query("select url from kms_dloads where id=$id limit 1" )); 
        if (
    $downloadcount['url']) 
        { 
            
    mysql_query("update downloadcount set downloadcount=downloadcount + 1 where id=$id); 
            
    header("Location: $downloadcount[url]); 
        } 

    $kms_dloads = @mysql_query('SELECT ID, name, description, name, url, size, downloadcount FROM kms_dloads');

    I have no idea why that line is in there, it does nothing as far as I can tell....
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  9. #9
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the code above selects the dtabase table and displays the following columns

  10. #10
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought this may help matters if i posted the table in the database :
    PHP Code:
    CREATE TABLE kms_dloads (
     
    ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
     
    downloadcount int(11),
     
    name TEXT,
     
    description TEXT,
     
    size TEXT,
     
    url TEXT 
    Any ideas ?

  11. #11
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well you'll need to make sure you use ID=$id in my examples, because it is uppercase...

    Other than that, it should work properly with what I posted before.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  12. #12
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No luck I'm afraid

    PHP Code:
    // pull the data from the database table
    $kms_dloads = @mysql_query('SELECT ID, name, description, name, url, size, downloadcount FROM kms_dloads');

    while (
    $name mysql_fetch_array($kms_dloads)) {
      
    $id   $name['ID'];
      
    $url htmlspecialchars($name['url']);
      
    $size htmlspecialchars($name['size']);
      
    $downloadcount = ($name['downloadcount']);
      
    $description htmlspecialchars($name['description']);
      
    $name htmlspecialchars($name['name']);
      
      if (isset(
    $_GET['ID']) && ($downloadcount = (int) $_GET['ID']) && $id!=0

        
    $downloadcount = @mysql_fetch_array(@mysql_query("SELECT downloadcount FROM kms_dloads WHERE ID=$id limit 1" )); 
        if (
    $downloadcount['url']) 
        { 
            
    mysql_query("UPDATE downloadcount SET downloadcount=downloadcount + 1 WHERE ID=$id); 
            
    header("Location: $downloadcount[url]); 
        } 

    Why doesn't it work, it doesnt display anything ?

  13. #13
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the code I mentioned is for the actual file you link to, not for display at all....

    example, my code goes into download.php, and you call it like this:

    <a href="download.php?id=2">Download File</a>
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  14. #14
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh ! I see ok I need to make a download.php file to pull the file into ..

    ok here goes again !

  15. #15
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edited*
    Last edited by Deano; May 29, 2003 at 13:04.

  16. #16
    SitePoint Evangelist Deano's Avatar
    Join Date
    Mar 2003
    Location
    Derbyshire, UK
    Posts
    487
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have created the download.php script here's all thats included :

    PHP Code:
    <?php
      
    // includes
    include("conf.php");

    // open database connection
    $connection mysql_connect($host$user$pass) or die ("Unable to connect!");

    // select database
            
    mysql_select_db($db) or die ("Unable to select database!");
            
    if (isset(
    $_GET['ID']) && ($downloadcount = (int) $_GET['ID']) && $id!=0

        
    $downloadcount = @mysql_fetch_array(@mysql_query("SELECT url FROM kms_dloads WHERE ID=$id limit 1" )); 
        if (
    $downloadcount['url']) 
        { 
            
    mysql_query("UPDATE downloadcount SET downloadcount=downloadcount + 1 WHERE ID=$id); 
            
    header("Location: $downloadcount[url]); 
        } 
    }   


    ?>
    and here's the line I use in the display page to call the url:

    PHP Code:
    <font size='2' face='Arial, Helvetica, sans-serif'><a href='download.php?id=$url'><img src='images/download.gif' width='14' height='14' border='0' alt='Download File'></a></font
    but now for some reason the download.php page opens and the $download count column doesnt get updated ?

    Any Ideas

    Thx Deano


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •