For Version 11 I decided to make a program to add, display and remove templates from the free template section of my site. So I am going to write this tutorial so that others may use this basic system to manage templates, or anything else they want, it can easily be adapted to have uses other than template managing.
First off, we are going to create our Database. Open up phpMyAdmin and go to the tab that says SQL and run the following query:
CREATE DATABASE `templates`
On your left you should be able to click the templates database. On it's page go to the SQL query tab and run the following:
CREATE TABLE IF NOT EXISTS `template` (
`id` mediumint(9) NOT NULL auto_increment,
`author` varchar(255) NOT NULL,
PRIMARY KEY (`id`) )
That will get your database setup and ready. Now click on the template table under the templates database on your right and go to the Insert tab. For id put 1 and for author put the author of the template.
Now in order to use this system exactly like I do you need to have the following:
- A thumbnail for the template
- A .zip file to download the template
- And A folder with the templates files, to let people preview it
Now you need to make the page where the templates will be displayed. Make a new file and name it whatever you want, Mine is named templates.php
I am going to go through the code bit by bit and explain what it does, then at the end give the code as a whole.
<?php
$con = mysql_connect("MySQL Server (Usually localhost)","MySQL username","MySQL password");
if (!$con)
{
die('Could not connect: ' .mysql_error());
}
mysql_select_db("templates", $con);
The above code connects to your MySQL database. Change the server, username and password parts on the second line to your's.
$result = mysql_query("SELECT * FROM template ORDER BY id DESC");
This selects all the entrys from the template table, and orders them by their id, newest ones first.
while($row = mysql_fetch_assoc($result))
{
echo "<strong>Template ".$row['id']."</strong><br /><a href='templates/".$row['id']."/'><img src='templates/thumbnails/".$row['id'].".png'/></a><br /><strong>Author:</strong> ".$row['author']."<br /> <a href='templates/".$row['id']."/'>Download</a><br /><a href='templates/downloads/".$row['id'].".zip'>Download</a><br /><br />\n";
}
?>
The above echos a title, a thumbnail, a link to preview and a link to download. You may need to change up some of the links,
And as promised, here is the full code:
<?php
$con = mysql_connect("MySQL Server (Usually localhost)","MySQL username","MySQL password");
if (!$con)
{
die('Could not connect: ' .mysql_error());
}
mysql_select_db("templates", $con);
$result = mysql_query("SELECT * FROM template ORDER BY id DESC");
while($row = mysql_fetch_assoc($result))
{
echo "<strong>Template ".$row['id']."</strong><br /><a href='templates/".$row['id']."/'><img src='templates/thumbnails/".$row['id'].".png'/></a><br /><strong>Author:</strong> ".$row['author']."<br /> <a href='templates/".$row['id']."/'>Download</a><br /><a href='templates/downloads/".$row['id'].".zip'>Download</a><br /><br />\n";
}
?>
My templates.php is in my root directory, and there is a templates folder, and all of this links to the templates folder.
Now you have a working, basic template system. You might notice that mine are in a three column table, while the code I just gave you echos a single column. I am going to go on and show you how to do this since I have the code and you might not want your templates to be in a single column, if you have the extra width, why not save yourself some page height?
First I am going to show you the php code to have three columns, then explain the added code.
<?php
$con = mysql_connect("MySQL Server (Usually localhost)","MySQL username","MySQL password");
if (!$con)
{
die('Could not connect: ' .mysql_error());
}
mysql_select_db("templates", $con);
$result = mysql_query("SELECT * FROM template ORDER BY id DESC");
$x = 0; // Sets $x to 0, you will understand why later.
while($row = mysql_fetch_assoc($result))
{
if ($x == 0) //Checks if x = 0 and if it does...
{
echo "<tr>"; //It echos a <tr>
}
echo "<td><center>"; //Puts each template in a <td>
echo "<strong>Template ".$row['id']."</strong><br /><a href='templates/".$row['id']."/'><img src='templates/thumbnails/".$row['id'].".png'/></a><br /><strong>Author:</strong> ".$row['author']."<br /> <a href='templates/".$row['id']."/'>Download</a><br /><a href='templates/downloads/".$row['id'].".zip'>Download</a><br /><br />\n";
echo "</center></td>"; //Ends the <td> we put the template in.
$x++; //Adds 1 to x
if ($x==3) //Check if x is eqaul to 3* and if it is...
{
echo "</tr>"; //ends the <tr>
$x=0; // and sets x back to zero, creating a new row to the html table.
}
}
/* the following code keeps the html clean */
while($x<3) //checks if x is less than 3 (If it is, we need to echo an empty cell to make table right.*
{
echo "<td></td>"; //empty cell
$x++; //adds to x, then it runs the $x>3 check again to see if this one cell did it.
}
echo "</tr>";
?>
I have commented the added code.
*If you want 2 or more than 3 columns, change the 3 to whatever number you want, so the code will wait till it has reached that many to start a new row.*
And that's it, if you are having trouble Contact Me.
Credit to my friend Dave for helping me get the columnns to work (:.

