PDA

View Full Version : [PHP] Tutorial: Making a basic Download CMS (uses mysql)



мϊкэ
06-08-2005, 10:39 PM
Hey,
I put these codes together using various other php scripts using mysql
This tutorial will explain how to make a basic download cms, this will allow you to view the downloads (obviously), add downloads with a form, have a click counter to show how many times the file was downloaded, anyway...

First of all you will need a file to connect to your mysql DB

<?php
$username = "";//your username
$password = "";//your password
$host = "localhost";//your mySQL server
$database = "";//The name of the database your table is in

mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error());//connect, but if there is a problem, display an error message telling why there is a problem
mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error());//Choose the database from your mySQL server, but if there is a problem, display an error telling why
?>
save that as dbconnect.php
next you will need to install the tables to your DB create a file called install.php and add this code:

<?php
include ('dbconnect.php');
//create the table, customize it if you want
$query = 'CREATE TABLE downloads(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
title VARCHAR(100) NOT NULL,
avatar VARCHAR(100) NOT NULL,
url VARCHAR(100) NOT NULL,
categorie VARCHAR(100) NOT NULL,
descrip VARCHAR(100) NOT NULL,
date varchar(50) NOT NULL default "0000-00-00",
author VARCHAR(100) NOT NULL,
added VARCHAR(100) NOT NULL,
total VARCHAR(100) NOT NULL)';
$result = mysql_query($query);
echo "Table Created!";
?>
this will create the required tables needed for the downloads system.
Creating the click counter,
The click counter will record how many times the download has been clicked, this will record this in the mysql table 'total' and display it with the download description heres the code:

<?php
include ('dbconnect.php');

$id = $_GET['id']; //Gets the id from the browser, ie, count.php?id=2
if (!is_numeric($id)) //if the id is NOT a number exits the script
{
exit;
}
mysql_query ("UPDATE downloads SET total = total + 1 WHERE id = '$id'"); //makes a database query and updates the field named total with the previous total +1(for the click)
$result = mysql_query("SELECT * FROM downloads WHERE id = '$id'"); //makes another query and selects all from the table affiliates where field id equals the variable id(which we defined as the GET function)
$row = mysql_fetch_object ($result);
header("Location: " . $row->url); //makes a redirect to the specified URL of the link found in the database.
?>
Now the next thing is diplaying the download create a file called downloads.php and add this code to it:

<?php
include ('dbconnect.php');

$result = mysql_query("SELECT * FROM downloads ORDER BY total DESC"); // makes a query to database to table "affiliates" and orders it by total descending
while ($row = mysql_fetch_object($result)) { // while loop While you are connected to the database do this...
echo "<table width='100%' cellspacing='2' class='downloads' cellpadding='0'>
<tr>
<td width='40' height='40' valign='top'><a href='downloads/count.php?id=".$row->id."'><img src='".$row->avatar."' border='0' width='40' height='40'></a></td>
<td class='description' valign='top'><b><a href='downloads/count.php?id=".$row->id."'>".$row->title."</a></b><br>
Category: ".$row->categorie."<br>
".$row->descrip."<br>
Author: ".$row->author." // Added by: ".$row->added." // Date Added: ".$row->date."<br>
Downloads: ".$row->total."
</td>
</tr>
</table><br>
";
}
?>
This will display all of the downloads and the newest download will appear at the top. Now then adding the download using a form, this form will insert the details into your mysql DB, if you get anything wrong you will have to edit it via phpmyadmin or whatever you are using for now, Here is the code to add the downloads:


<?php
include ('dbconnect.php');

if(!empty($title)) {//if the title is not empty, than do this function
$title = addslashes($title);
$avatar = addslashes($avatar);
$url = addslashes($url);
$categorie = addslashes($categorie);
$descrip = addslashes($descrip);
$author = addslashes($author);
$added = addslashes($added);
$total = addslashes($total);

$date = date("F j, Y");//set up the date format
$date2 = mktime();

$sql = "INSERT INTO downloads (id, title, avatar, url, categorie, descrip, date, author, added, total) VALUES ('NULL', '$title','$avatar','$url', '$categorie', '$descrip', '$date', '$author', '$added', '$total')";//Insert all of your information into the mynews table in your database
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
echo "Download Added.";
} else {
?>
<form name='news' method='post' action='index.php?show=adddl'>
<p>Please fill out all of the following fields:</p>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='117'>Download Title*:</td>
<td width='577'>
<input type='text' name='title' size='50'>
</td>
</tr>
<tr>
<td width='117'>Avatar (40x40)*:</td>
<td width='577'>
<input type='text' name='avatar' size='50'>
</td>
</tr>
<tr>
<td width='117'>Download URL*:</td>
<td width='577'>
<input type='text' name='url' size='50'>
</td>
</tr>
<tr>
<td width='117'>Categorie*:</td>
<td width='577'>
<select name='categorie'>
<option selected>Choose a Category</option>
<option>PS Brushes</option>
<option>Fonts</option>
<option>Scripts</option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td width='117'>Description*:</td>
<td width='577'>
<textarea name='descrip' cols='50' rows='20'></textarea>
</td>
</tr>
<tr>
<td width='117'>Author*:</td>
<td width='577'>
<input type='text' name='author' size='50'>
</td>
</tr>
<tr>
<td width='117'>Added by*:</td>
<td width='577'>
<input type='text' name='added' size='50'>
</td>
</tr>
<tr>
<td width='117'>Total Downloads*:</td>
<td width='577'>
<input type='text' name='added' value='0' onFocus='this.blur()' size='50'>
</td>
</tr>
</table>
<p>
<input type='submit' name='Submit' value='Submit'>
</p>
</form>
<?php
}
?>

This code should work, ive just been testing it :D, any problems PM me :D

Coming soon...
- Edit your downloads
- Delete Downloads
- Sort out the categories
- Password protecting the page

This concludes my tutorial, this tutorial will be added to my site at www.xenigma.co.uk :D, and is also stickied in my sub-forum

Lysine
06-08-2005, 11:02 PM
10/10 VERY GOOD :p

Tomm
07-08-2005, 08:44 AM
Hehehe *makes better 1*

splintercell!
07-08-2005, 08:49 AM
+rep well done dude :D

мϊкэ
07-08-2005, 10:55 AM
Hehehe *makes better 1*

:O no chance ¬_¬ :D

how do you get these things stickied :P?

:Woof
07-08-2005, 10:58 AM
By getting anderman to bug the mods ;)

мϊкэ
07-08-2005, 11:08 AM
rofl! sounds like fun >:~D

:Woof
07-08-2005, 11:22 AM
It can be ;) Want somthing done ask Anderman! :) He is soooooo famous with them mods :p

мϊкэ
07-08-2005, 03:51 PM
is there any other way without involving anderman :rolleyes: (rofl)

T0X!C-uk
07-08-2005, 04:50 PM
how do you get these things stickied :P?

By waiting untill i see it ;)
:)STICKIED:) (Moved to tutorials)

мϊкэ
07-08-2005, 05:34 PM
w00t! i am good at this tutorial thing :s 3 tutorials pinned :D!

Tomm
07-08-2005, 07:25 PM
Must make tutorial to beat him :) Muhahahahahaha

:Woof
08-08-2005, 10:21 AM
Anything like Dunko (Cr) then dont bother :l

Tomm
12-08-2005, 12:48 PM
I am still working on the CMS :)

.Lesley.
23-09-2005, 05:18 PM
did you make it up your self?

lmao im a blond lmao jkz but did u?

matt9875
06-10-2005, 07:13 PM
hmm i might try it

Want to hide these adverts? Register an account for free!