Jump to content

Methods for conting downloads


Recommended Posts

Here is link to sql insert file.

This is Foxpro code I used to convert, just in case you are interested...

fh = Fopen("c:\list.txt")
strout = ""
Do While !FEOF(fh)
  strtmp = "INSERT INTO dlls (file, size, version) VALUES ('"
  strtmp = strtmp + Fgets(fh) + "', '"
  strtmp = strtmp + Fgets(fh) + "', '"
  strtmp = strtmp + Fgets(fh) + "');" + Chr(13) + CHR(10)
  strout = strout + strtmp
Enddo
Fclose(fh)
Strtofile(ALLTRIM(strout),"c:\dllinsert.sql")

Edited by dman
Link to comment
Share on other sites


what are you using to administer your db? I am using phpmyadmin. Just open the file and select all/copy and paste into sql window and run, same way as you are creating the tables. or use "browse" button and select the file and run.

Edited by dman
Link to comment
Share on other sites

It all looks very complicated

Start Small... Evolve. It all becomes very simple, you just need to reach critical mass and it will all make sense.

Now look at the count.php page. Your list code will be very similar to this... open db, get recordset, loop through records creating a table row once per loop. that is basics, then we add the links for each letter, finally the paging code to show limited number of records per page...

Edited by dman
Link to comment
Share on other sites

Just one quick note...

You do not want to put any of this code on your live server until it is hardened up. Functionality first, then we harden it up, both the database and the PHP. Yes, believe it or not there are evil people out there that would love nothing better than to submit a SQL Injection attack and erase your database just for laughs, so test on local machine until it's done! If you do put it live make sure to back up often.

Edited by dman
Link to comment
Share on other sites

This should get you started...

<head>
<title>Freedll.xyz</title>
</head>
<body>
<h1>Freedll.xyz</h1>
<?php
// Connecting, selecting database
$link = mysql_connect('XXX', 'XXX', 'XXX')
  or die('Could not connect: ' . mysql_error());

mysql_select_db('XXXX') or die('Could not select database');

// Performing SQL query
$query = 'SELECT file, version, size FROM dlls order by file';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num=mysql_numrows($result);

// Printing results in HTML
$i=0;
echo "<table border=\"1\"><tbody>\n";
echo "<tr><th>File</th><th>Version</th><th>Size in kb</th></tr>\n";
while ($i < $num) {
$file=htmlspecialchars(mysql_result($result,$i,"file"));
$version=trim(mysql_result($result,$i,"version"));
$size=trim(mysql_result($result,$i,"size"));
echo "\t<tr>\n";
echo "\t\t<td>$file</td>\n";
echo "\t\t<td>$version</td>\n";
echo "\t\t<td>$size</td>\n";
echo "\t</tr>\n";
   $i++;
}
echo "</tbody></table>\n";
// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
</body>
</html>

look at the code and read the docs about the various functions, soon you will see how to extend it.

EDIT: don't know why the code always loses it's indents when I paste into forum code tag?

Edited by dman
Link to comment
Share on other sites

it returns this when i open it;

\n"; echo "FileVersionSize in kb\n"; while ($i < $num) { $file=htmlspecialchars(mysql_result($result,$i,"file")); $version=trim(mysql_result($result,$i,"version")); $size=trim(mysql_result($result,$i,"size")); echo "\t\n"; echo "\t\t$file\n"; echo "\t\t$version\n"; echo "\t\t$size\n"; echo "\t\n"; $i++; } echo "\n"; // Free resultset mysql_free_result($result); // Closing connection mysql_close($link); ?>

i`ve been trying to find info on the functions to figure out if i have to configure something. the info in the link will help a lot i think. got to go for bout half hour, will check it out then.

Link to comment
Share on other sites

You shouldn't have to configure anything (assuming PHP is working) except change XXX to correct values and name the file "something.php"

seems like it is not processing the PHP and just returning text. I just tried copying from forum and pasting into new file and it is working OK.

Edited by dman
Link to comment
Share on other sites

you are right it works, i was running it localy.

but, the Version is showing under Size in kb and vice-versa. Also the versions are showing like this

6

instead of

6.0.7.1709

think it stops reading after the full stop.

Edited by Neanderthal
Link to comment
Share on other sites

sorry, got the columns backwards. The version is getting cut off because it is trying to go into an integer column which doesn't accept decimal values. Fixed and reposted.

Link to comment
Share on other sites

OK, hopefully you got the db straightened out and had a chance to peek at the code a little. Here is next phase, alphabetical menus and page menus. I tried to put a lot of comments in the code, but if something is unclear I will add more.

<head>
<title>Freedll.co.uk</title>
</head>
<body>
<?php
// Create letter links
// we are going to build a string that consists of the HTML needed to make our alphabet menu, then we are going
// to output it to the HTTP stream using the PHP "echo" command
$alphamenu="<table align=\"center\">\n<tr>\n";

// this loop will repeat 26 times, one for each letter of the alphabet. each time through we use the chr() function
// to create a letter, starting with chr(65), which is the ASCII code for capital "A".
// The ".=" means append the string to the current string, $i++ tells the $i variable to increment once each loop
// the "\t" and "\n" in the strings are not actually part of the code, they are used
// to add a tab or newline to the output so the HTML output code looks pretty.
for ($i = 1; $i <= 26; $i++)
{
$letter=chr($i+64);
$alphamenu .= "\t<td><a href=\"index.php?folder=" . $letter . "&pidx=1\">" . $letter . "</a> | </td>\n";
}
// lastly we add the link for 0-9
$alphamenu .= "\t<td><a href=\"index.php?folder=0&pidx=1\">0-9</a></td>\n";
$alphamenu .= "</tr>\n</table>\n";

// and finally write the string to the HTTP stream
echo $alphamenu;

// this value sets the number of rows we want to show on each page
$pagesize=25;

// now we make a connection to our MySQL database server
$link = mysql_connect('XXX', 'XXX', 'XXX')
 or die('Could not connect: ' . mysql_error());

// and open our database
mysql_select_db('XXX') or die('Could not select database');

// next line checks if a "get" parameter was passed to the script. If it was, we set a variable named $folder to the value
// of the dlls folder, and $pageindex to our starting point
// If not it means someone has just entered the site and we set the page to "A-1"
if (isset($_GET['folder']))
{
$folder=$_GET['folder'];
$pageindex=$_GET['pidx'];
} else {
$folder="A";
$pageindex=1;
}

// now we see if dll starts with a letter or 0-9 and set our SQL query to match.
// We have set the parameter for 0-9 to "0" in our menu link
// the query will give us a count of how many rows match the letter or 0-9 we use this to determine how
// many pages worth there are to display
if ($folder=="0")
{
$whereclause = " LEFT(file,1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')";
} else {
$whereclause = " LEFT(file,1)='" . $folder . "'";
}
// and now we set up our query with the proper where clause inserted
$query = "SELECT file FROM dlls WHERE " . $whereclause . " ORDER BY file";

// now we run the query on the database
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// get the count from the recordset for the total number of rows
$num=mysql_numrows($result);

// now we divide the number of rows by our page size and round it up with the ceiling function to get number of pages
$numpages=ceil($num / $pagesize);

// here we start to build our output HTML string for our page menu
// we loop here once for each page. each time through the loop we add the first three characters
// of the file name at the top of the page to our menu.
// the first line tells us not to create a page menu if there is only one page
if ($num > $pagesize)
{
$menucap = substr(mysql_result($result,1,"file"),0,3);

$pagemenu="<table align=\"center\">\n<tr>\n";
$pagemenu .= "\t<td><a href=\"index.php?folder=" . $folder . "&pidx=1" . "\">" . $menucap . "</a></td>\n";

for ($i = 1; $i < $numpages; $i++)
{
 $pos=($i*$pagesize)+1;
 if ($pos>=$num)
 {
 break;
 }
 $menucap = substr(mysql_result($result,$pos,"file"),0,3);
 $pagemenu .= "\t<td><a href=\"index.php?folder=" . $folder . "&pidx=" . $pos . "\">" .
 $menucap . "</a></td>\n";
}
$pagemenu .= "</tr>\n</table>";
// write the page menu to HTTP stream
echo $pagemenu;
}

// release the result set so we can use it for next query
mysql_free_result($result);

// now we set up the main query to retrieve our page worth of dlls
$query = "SELECT file, version, size FROM dlls WHERE " . $whereclause . " ORDER BY file LIMIT " . $pageindex . "," . $pagesize;

// now we run the main query on the database
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// and get the count
$num=mysql_numrows($result);

// create our main output string for the list table
$i=0;
$htmlout = "<table align=\"center\" border=\"1\"><tbody>\n";
$htmlout .= "<tr>\n<th>File</th><th>Version</th><th>Size in kb</th>\n</tr>\n";
while ($i < $num) {
$file=htmlspecialchars(mysql_result($result,$i,"file"));
$version=trim(mysql_result($result,$i,"version"));
$size=trim(mysql_result($result,$i,"size"));
$htmlout .= "<tr>\n";
$htmlout .= "\t<td>$file</td>\n";
$htmlout .= "\t<td>$version</td>\n";
$htmlout .= "\t<td>$size</td>\n";
$htmlout .= "\t</tr>\n";
$i++;
}
$htmlout .= "</tbody></table>\n";

// write the table to HTTP stream
echo $htmlout;

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);

// write the menus at the bottom of the page
echo $pagemenu;
echo $alphamenu;
?>
</body>
</html>

Next we will add the links and the download code, then harden it up and finally make it all pretty.

This page will then replace all of your HTML pages, along with the nightmare of keeping them updated and sorted correctly. All you need to do is make sure the data in your DB is correct and the code will take care of building the pages. :D

EDIT: the CODE tag is stripping the indents out of the code making it much harder to read.

BUG FIX: added break out of loop when last page is a full size page of data to prevent eof condition

Edited by dman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...