SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Jack@dwd.com's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>");
        }
        
    }
    ?>
    Thats the class file, below is an example how to use it.

    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(=> array("verbose" => "Clan Initials""database" => "claninitials"),
                
    => 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($newheaders2);

    // 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();
    ?>
    This generates the following code in the case of my database:

    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>
    Hope this is of some use to someone.

    Jack

  2. #2
    SitePoint Enthusiast Jack@dwd.com's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 well
    Last edited by Nicky; Apr 16, 2001 at 14:03.

  3. #3
    SitePoint Enthusiast nguip's Avatar
    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

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •