I have an album with mobile phone themes. I upload theme screenshot to album and in image description field I put download link with bbcode. Because I wan't to know how many times the theme has been downloaded I have installed download counter. I would now like to show number of downloads in image description field but to do that I must use this code:
This file has been downloaded <?php echo showCount("testfile.zip"); ?> times.
But, it's not working. PHP doesn't get parsed. :(
What can I do to get this working?
what file did you edit? Where in the code? You'll have to post nore details.
I did not edit any files. I'm using this script as file download.php:
<?php
//CONFIGURATION SECTION
$FILES_DIR = "/folder/";
$MYSQL_USER = "user"; //The username used to connect to MySQL
$MYSQL_PASS = "pass"; //The MySQL Password for the user
$MYSQL_HOST = "localhost"; //The host to connect to
$MYSQL_DB = "db"; //The database in which the dl_count table is in
$cnt_sql = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS);
@mysql_select_db($MYSQL_DB, $cnt_sql);
if(isset($_GET['file'])) {
$file = urlencode($_GET['file']);
if(empty($file)) {
echo "No File Specified";
exit;
}
if(strpos($file, "..") !== FALSE) {
echo "HACK ATTEMPT!";
exit;
}
if(strpos($file, "://") !== FALSE) {
echo "Invalid File";
exit;
}
$cookie = urlencode(str_replace(".", "_", $file)); //cookie fix
$query = "SELECT * FROM cpg_dl_count WHERE file = '$file'";
$result = mysql_query($query, $cnt_sql);
if(!$result) {
echo mysql_error();
exit;
}
if(mysql_num_rows($result) == 0) {
//first use of this file
$query = "INSERT INTO cpg_dl_count VALUES('$file', 1)";
$result = mysql_query($query, $cnt_sql);
setcookie("dl_" . $cookie, "set", time() + 60*60*24*365);
} else {
if(!isset($_COOKIE['dl_' . $cookie])) {
$query = "UPDATE cpg_dl_count SET count = count + 1 WHERE file = '$file'";
$result = mysql_query($query);
setcookie("dl_". $cookie, "set", time() + 60*60*24*365);
}
}
header("Location: " . $FILES_DIR . $file);
}
function showCount($fileID)
{
global $cnt_sql;
$query = "SELECT count FROM cpg_dl_count WHERE file = '$fileID'";
$result = mysql_query($query, $cnt_sql);
if(mysql_num_rows($result) == 0) {
return 0;
} else {
$count = mysql_fetch_row($result);
return $count[0];
}
}
?>
In the image description field I have links for download created with bbcode that looks like this
download.php?file=testfile.zip
When someone click on that download link first time, entry testfile.zip is automatically added to database table cpg_dl_count that I have created. Database table has two columns: filename and count. Every time someone click to download some file count is updated.
In order to display number of downloads in image description filed I have to include download.php script to cpg and show results with this:
<?php echo showCount("testfile.zip"); ?>
I don't know how to show this results because I can't use php in image description field.
I have attached original script if someone is interested
You can see this download links in my Themes album at http://neville.x05host.com
I'm also using this script (downloadstats.php) to show results of all downloads. Maybe this can be somehow usefull.
<?php
//DATABASE CONFIGURATION
$SQL_HOST = "localhost";
$SQL_USER = "user";
$SQL_PASS = "pass";
$SQL_DB = "db";
//end configuration
$xmlcnt_link = mysql_connect($SQL_HOST, $SQL_USER, $SQL_PASS) or die("Could not connect to database. Reason: " . mysql_error());
mysql_select_db($SQL_DB, $xmlcnt_link);
if (isset($_GET['file'])) {
$count_query = "SELECT * FROM cpg_dl_count WHERE file = '{$_GET['file']}'";
$count_result = mysql_query($count_query);
} else {
$count_query = "SELECT * FROM cpg_dl_count ORDER BY file ASC";
$count_result = mysql_query($count_query);
}
echo "<table border=1>\n";
while ($data = mysql_fetch_array($count_result)) {
echo " <tr>\n"
." <td width=200>{$data['file']}</td>\n"
." <td width=50 align=center>{$data['count']}</td>\n"
." </tr>\n";
}
echo "</table>\n";
$count_query = "SELECT SUM(count) FROM cpg_dl_count";
$count_result = mysql_query($count_query);
$data = mysql_fetch_row($count_result);
echo "\n<br><b>Total downloads: {$data[0]}</b>\n";
?>
if you add the count to your cpg_pictures table you just can pull the count with already necessary queries. When you have added it just proceed as I've described here http://forum.coppermine-gallery.net/index.php?topic=20446.0
for the regular display just find the function get_pic_data and have a look a few lines below after // Regular albums
Tnanks for help, but this is not what I want to do. I can't add count to cpg_pictures because those download links have nothing to do with pictures in database. In my album I have screenshot of theme in jpg format and this picture is in database. Actual themes are not in database, there are uploaded as zip files in separate folder. I have put theme download links in image description beneath each intermediate screenshot. When user click on download link, download count for that theme is updated.
I just want to show this text in image description field:
This theme has been downloaded 5 times.
And to that I must use this code
This theme has been downloaded <?php echo showCount("sometheme.zip"); ?> times.
PHP is not supported in image description, only bbcode.
Does anyone know another way to add this text in image description field?
Ok! I found the way!!! :D :D
Aded to bb_decode function in functions.inc.php file
[y=file.zip] will become this:
preg_match("/\[y=(.+)\]/", "$text", $matches);
$dlcount = showCount($matches[1]);
if ($dlcount == 1) {
$text = preg_replace("/\[y=(.+)\]/", "This theme has been downloaded ".$dlcount." time", $text);
} else {
$text = preg_replace("/\[y=(.+)\]/", "This theme has been downloaded ".$dlcount." times", $text);
}
edit ... have written that while you posted ... nice solution btw. however not what I'd done, hehehe
Stramm, thank you for help.
I'll try explain why I'm doing this way. I like having big picture of my theme so people can see how theme looks 1:1 before download. I upload only screenshot in album to get thumbnail, intermediate pic and big bic of that screenshot because if I upload only zip in album and then assign thumbnail to that zip, I don't have resized and big picture.
My themes are in different folder and they don't appear in database.
In process of uploading screenshot I enter theme download URL in description field, and now I can also enter [y=themefile] to show number of times this theme is downloaded.
I'm sorry for bad english, but I hope you understand. :)
Yes, I understand... no problem at all...
just funny, you have a total different approach and it seems to work pretty well ;)
I'd uploaded the theme with the same name as the preview image. Pic's eg. MacOS-Theme.jpg, then the theme's MacOS-Theme.zip. This way I just pull the image name out of the db, do a preg_replace, check if the theme file's in its folder and then let the script display a download link. Cause theme and preview image are now bound together it's no problem to add the count to the pictures table. When the script now catches the thumbs title it can get in the same query the download count. If every picture in the db has a download link I'd even get rid of the check if a theme.zip is on the server.
You even can modify the upload.php to auto upload the theme.zip as well... or when you delete an image then the theme.zip gets deleted too.