SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
-
Apr 16, 2001, 10:18 #1
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.
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>");
}
}
?>
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();
?>
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
-
Apr 16, 2001, 14:00 #2
I assume this means either
a) Your busy copying and pasting my code or
b) You don't know what I'm going on about
Ah wellLast edited by Nicky; Apr 16, 2001 at 14:03.
-
Apr 16, 2001, 18:55 #3
- Join Date
- Apr 2001
- Location
- Malaysia
- Posts
- 95
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yah
I'm newbies... so I'd like to copy your code for my reference and also use it whenever it's relevent to me
Thanks!Ngu I.P.
Web Developer
-
Apr 17, 2001, 00:30 #4
- Join Date
- Jun 2000
- Location
- Sydney, Australia
- Posts
- 3,798
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the code. people probably won't appreciate it until they need it
I've added it to my folder of code snippets and will enjoy perusing it with a pipe and a tawny port.
Bookmarks