Today I started writing the back end page classes for a site that I am currently creating. With 12 tables, all that need full addition, modification and deletion options for the 6 different access levels available, you might imagine this is a somewhat boring and tedious task, extremely repetitive to say the least.
I have been playing with Java recently and have just discovered the power of classes and Object-Oriented programming, so I thought I would use the skills I have acquired to make my job just a little bit easier.
I don't know if anyone will find these useful, I just thought I would post them and see if they did. If people do take an interest I'll post up the others I'll be making tommorow which will generate the forms etc. to add/modify records.
I've tried to comment the code quite a lot to make it clear the steps. If anyone has any suggestions please feel free to make them.
Thats the class file, below is an example how to use it.PHP Code:<?php
/*
File Name: pagegen.inc.php
*****************************************************
* Class written for www.tfc-league.co.uk on 16th *
* April 2001. Makes it dead easy to create those *
* rather dull and boring Administration pages *
* Code written by Jack Hale aka Elmo, but may be *
* modified and redistributed as needed. *
* www.discowebdesign.com. *
*****************************************************
*/
class pagegenerator
{
var $sql;
var $recordset;
var $row;
var $headers;
var $number;
function executesql($newsql)
{
/* This gets in the SQL which you wish to generate the table from */
$this->sql = $newsql;
$this->recordset = mysql_query($this->sql);
}
function tableheaders($newheaders, $newnumber)
{
/* This makes the table headers out of the 'verbose' array */
$this->headers = $newheaders;
$this->number = $newnumber;
// Define a temporary counter variable
$i = 0;
// Print the top level table HTML tag
echo("<table width=\"90%\" border=\"0\" class=\"normal\">\n");
echo("<tr>\n");
// Loop through to create table headers. $number is brought into function
while ($i < $this->number)
{
echo("<td>\n");
echo($this->headers[$i]["verbose"]);
echo("</td>\n");
$i++;
next($this->headers);
}
// Finish up top row of headers
echo("</tr>");
}
function content($url, $uniqueid)
{
/* This creates the content below the table headers */
// Create another counter
$i = 0;
// Cycles through the recordset brought in to class earlier from executesql()
while ($row = mysql_fetch_array($this->recordset))
{
// Reset the internal pointer from previous usage
reset($this->headers);
echo("<tr>\n");
// Similar to loop in tableheaders() function
while ($i < $this->number)
{
echo("<td>\n");
// If this is the first column of the row, print the
// url to the modification/deletion page
if ($i == 0)
{
echo("<a href=\"$url?$uniqueid=".$row[$uniqueid]."\">");
}
echo($row[$this->headers[$i]["database"]]);
if ($i == 0)
{
echo("</a>");
}
echo("</td>\n");
$i++;
next($this->headers);
}
echo("</tr>\n");
// VERY Important. Must reset counter
$i = 0;
}
}
function finishup()
{
/* This function just finishes up and closes the HTML tags.
Mainly to make code more readable and expandable */
echo("</table>");
}
}
?>
This generates the following code in the case of my database:PHP Code:<?php
// Include any files that you normally need, in this case MySQL connections and HTML
// and footers, and the page generator class
include("../connection.inc.php");
include("pagegen.inc.php");
// This creates a new page generator class in the variable $clans
$clans = new pagegenerator;
// This executes the SQL and creates the recordset inside the class variable $clans
$clans->executesql("SELECT * FROM clans ORDER BY claninitials DESC");
/*
Quick explanation of array: This basically consists of 3 elements, the index, which should
always start from 0, then a verbose (wordy) user friendly name for the table headers, then
finally the database name for the field
*/
$newheaders = array(0 => array("verbose" => "Clan Initials", "database" => "claninitials"),
1 => array("verbose" => "Clan Full Name", "database" => "clanname"));
/*
This is the call to the table headers function. First value to be passed is the array
declared above, then the number of headers there actually are. I'm sure there is a way
of counting how many there are, but I couldn't be arsed to find it :P
*/
$clans->tableheaders($newheaders, 2);
// The array and number of columns has already been stored within the class so this is
// argument-less
$clans->content("modifyclan.php","cid");
// The currently uneeded finish up function. Just prints one line
$clans->finishup();
?>
Hope this is of some use to someone.Code:<table width="90%" border="0" class="normal"> <tr> <td> Clan Initials</td> <td> Clan Full Name</td> </tr><tr> <td> <a href="modifyclan.php?cid=4">TPC</a></td> <td> The Poo Crew</td> </tr> <tr> <td> <a href="modifyclan.php?cid=1">PVC</a></td> <td> Personal Vendetta Corporation</td> </tr> <tr> <td> <a href="modifyclan.php?cid=5">MDB</a></td> <td> MDB</td> </tr> <tr> <td> <a href="modifyclan.php?cid=3">KASAP</a></td> <td> KASAP</td> </tr> </table>
Jack



. If people do take an interest I'll post up the others I'll be making tommorow which will generate the forms etc. to add/modify records.



Bookmarks