Jump to content

Counter Help


Kitty

Recommended Posts

Hello everyone! I have a code that displays my graphics and I'd like to add a downloads/previews counter. Here's the code:

[code=php:0]
<?php

//MySQL connection variables
$user="blahblah";
$host="localhost";
$password="blahblah";
$database="blahblahblah";
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server"); $db = mysql_select_db($database,$connection)
or die ("Couldn't select database");

//Which mySQL table to select from and in what order
$query = "select * from `layouts` order by id desc";

$result = mysql_query($query, $connection) or die ("Could not execute query : $query ." . mysql_error());
$rows = mysql_num_rows($result);
if ($rows=="0") { echo "No layouts found."; }


//Gets and assigns variables to array
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$title=$row["title"];
$artist=$row["artist"];
$artisturl=$row["artisturl"];
$type=$row["type"];
$size=$row["size"];
$date=$row["date"];
$previewimage=$row["previewimage"];
$download=$row["download"];
$preview=$row["preview"];
$dcounter=$row["dcounter"];
$pcounter=$row["pcounter"];

//Prints out results
echo "<table id=\"box_top\"><tr>
<tr><td>
<b>$title</b>
</td>
</tr>
</table>

<table id=\"box\">
<tr>
<td valign=\"top\">
<img src=\"$previewimage\" width=\"160\" height=\"110\" border=\"0\" alt=\"\">
</td>
<td valign=\"top\" width=\"170\">
<b>Added</b>: $date
<br><b>Type</b>: $type
<br><b>Size</b>: $size
<br><b>Designer</b>: <a href=\"$artisturl\" target=\"_blank\">$artist</a>
<br><b>Previews</b>: $pcounter
<br><b>Downloads</b>: $dcounter
<br><br>
<a href=\"$preview\"><b>Preview</b></a> &nbsp; <a href=\"$download\"><b>Download</b></a>
</td>
</tr>
</table><br>";
}

?>
[/code]

$pcounter and $dcounter should display the number of the downloads, but the thing is I don't have the code that adds 1 to the database when I link is clicked. Sorry if this is confusing! Can someone help me with this?
And sorry if I'm posting this in the wrong forum.
Link to comment
Share on other sites

You might need to write a function to display the image when someone clicks on it. So that PHP (javascript i guess could do it too) pass a variable to the database to increment the counter.

I have not really done one for images but I have done one similar for downloads.  I created a download.php script that would take the file's id as an argument.

[code]
<a href="downloadFile.php&id=234">Download File</a>
[/code]

Then I would need to set up a function for presenting the file to the user and handle the header and database items

[code]
<?php
if(isset($_GET['uid'])) {
// Filename are stored by their file_id in the fw/ directory
$uid = $_GET['uid'];
}
$message = "";
$qry = "SELECT file_name, file_type, file_size, downloads FROM support_files WHERE file_id = '{$uid}'";
$result = mysql_query($qry) or die ("MySQL Error: <br />{$qry} <br />". mysql_error());

list($fn, $ft, $fs, $dl) = mysql_fetch_array($result);
$extension = explode('.', $fn);
$the_file = "fw/{$uid}.{$extension[1]}";

//echo "<p>{$the_file}</p>";
if(file_exists($the_file)) {
if(filesize($the_file) == $fs) {
$dl++;  // This is what I use to increment the number of downloads
$qry = "UPDATE support_files SET downloads = '{$dl}' WHERE file_id = '{$uid}'";
$query = mysql_query($qry) or die ("MySQL Error: <br />{$qry}<br />". mysql_error());

                        // Need to pass the proper header to serve the file to the client
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: {$ft}");
header("Content-disposition: attachment; filename={$fn}");
header("Content-Length: {$fs}");
header("Content-Transfer-Encoding: binary");

                        // Send the file to the user!
readfile($the_file);

                        // Exit the rest of script
exit();
} else {
$message .= "<p>The file size is incorrect. Please notify the
<a href=\"mailto:support@.com\">Support</a> team about this.</p>\n";
}
} else {
$message .= "<p>That file could not be located.  Please notify the
<a href=\"mailto:support@.com\">Support</a> team about this.</p>";
}
?>
[/code]

Not a great explaination but I can try and explain aspects of if for you. It should give you a bit of an idea though :)

Link to comment
Share on other sites

Thank you so much! I have another question though, I'm using fclick and my download link looks like this: fclick/fclick.php?1 (1 is the id of the link) and fclick gives me good statistics so I'd like to keep it, but to show the number of the downloads it uses javascript: <script type="text/javascript" src="fclick/show.php?js&id=1"></script> and I don't know to insert this in the code, so I'm wondering if I can somehow keep the link fclick/fclick.php?1 and have another script that will count the downloads? Sorry, if this is confusing!
Link to comment
Share on other sites

On the show.php script you still pass the id of the image. 

You can modify the show.php script to use [code=php:0] $_GET['id'][/code] to pull the value from it and then query for how many downloads have been done:
[code]
SELECT downloads FROM myImages WHERE id = '{$_GET['id']}'
[/code]

Increment the value it returns and update the database
[code]
UPDATE myImages SET downloads = '{$newDownloadValue}' WHERE id = '{$_GET['id']}'
[/code]

:)
Link to comment
Share on other sites

Oh, I guess I forgot to say that I'm very new to php. *blush* Alright, I saved the first code you posted as download.php, now what should I add to my links?

Also, show.php, should I put the same code in that file and save it as show.php? And where do I put these lines of code:

[code]SELECT downloads FROM myImages WHERE id = '{$_GET['id']}'[/code]

and

[code]UPDATE myImages SET downloads = '{$newDownloadValue}' WHERE id = '{$_GET['id']}'[/code]

I guess all of you here are professional php programmers and I'm such a pest. Sorry! I just want to get this freakin' code to work! >.<
Link to comment
Share on other sites

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.