SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Sorry for bothering you guys again.

    How do I create a function which will print some html code everytime it is called?

    say:
    function form() {

    echo "";

    }

    right? But the hassle of putting the \ character behing every " is very tedious..and the code I have is very long.

    Anyway I can print this normally?
    Thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are some basic html functions I use in my classes but you can use them in regular old php scripts as well

    <?
    function form_start($action, $method = "POST") {
    return sprintf('<form action="%s" method="%s">', $action, $method);
    }
    function form_end() {
    return "</form>";
    }
    function table_start($width, $spacing = 0, $padding = 0, $border = 0) {
    return sprintf('<table width="%s" cellspacing="%s" cellpadding="%s" border="%s">%s', $width, $spacing, $padding, $border, "\n");
    }
    function table_end() {
    return "</table>";
    }
    function text_box($name, $value = "", $size, $max) {
    return sprintf('<input type="text" name="%s" value="%s" size="%s" maxlength="%s">%s', $name, stripslashes($value), $size, $max, "\n");
    }
    function hidden_input($name, $value) {
    return sprintf('<input type="hidden" name="%s" value="%s">%s', $name, $value, "\n");
    }
    function text_area($name, $value = "", $cols, $rows) {
    return sprintf('<textarea name="%s" rows="%s" cols="%s" wrap="virtual">%s</textarea>%s', $name, $rows, $cols, stripslashes($value), "\n");
    }
    function make_button($name, $value, $type) {
    return sprintf('<input type="%s" name="%s" value="%s">%s', $type, $name, $value, "\n");
    }
    ?>


    Example usage
    <?
    print form_start($PHP_SELF);
    print table_start(500, 0, 0, 0);
    printf('<tr><td>Hello World</td></tr>');
    printf('<tr><td>Name: %s</td></tr>', text_box("name", "", 40, 100));
    printf('<tr><td>Email: %s</td></tr>', text_box("email", "", 40, 100));
    print table_end();
    print form_end();
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow!
    Okay . I get your idea..


    Thanks!! Freddy once more!
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice library there Freddy - I've been giving things like that a try - functions like "printLink" and "bold" can be useful - hadn't even thought of tables and forms though.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris, it really helps me organize my ocde when doing large classes, I suppose Icould add a fe more to that list but those seemed like the most used.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Same here - 'cept for the classes part...I don't use classes much - a big pain, and I find I can do most of the same things with functions - I just store them in one big global file and call on it.

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    classes can be good for some things say you want to create something that you want to be able to port to nay website. One class one page that calls the class you're done. I use them alot.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you do the same with functions? One page, one function that calls it.

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know this is super long and maybe someone can use it, but I wanted to show you how classes can be beneficial. Here is a class for a guestbook and the page that calls it, all in one package
    guestbook.php
    <?

    /*
    CREATE TABLE Guestbook (
    id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name varchar(100),
    email varchar(100),
    comments text
    );

    */

    //Include the slightly modified mysql_db class from phplib.
    include("../db.php");

    //Instantiate the db class
    $db = new DB;

    class Guestbook {


    function Guestbook() {
    global $action;

    //This switch statement determines where to go form the start
    //either to the default method or to the readbook() or addnew() methods
    switch ($action) {
    case "readbook":
    print $this->readbook();
    break;

    case "addnew":
    print $this->addnew();
    break;

    default:
    print $this->mainpage();
    break;

    }

    }


    //This is the mainpage() method which is the default.
    //It creates the front of the guestbook with links to read it and add to it.
    function mainpage() {
    global $PHP_SELF;
    $return .= sprintf('%s%s', $this->table_start("600"), "\n");
    $return .= sprintf('<tr><td><h2>Welcome to my sample Guestbook</h2></td></tr>%s', "\n");
    $return .= sprintf('<tr><td><a href="%s?action=readbook">Click here</a> to read the guestbook</td></tr>%s', $PHP_SELF, "\n");
    $return .= sprintf('<tr><td><a href="%s?action=addnew">Click here</a> to sign the guestbook</td></tr>%s', $PHP_SELF, "\n");
    $return .= sprintf('%s%s', $this->table_end(), "\n");
    return $return;
    }


    //The readbook() method reads the entries from the guestbook limiting them to 5 to a page
    function readbook() {
    global $action, $db, $offset, $PHP_SELF;
    //Set Number of recordfs to show per page
    $limit = 5;
    $return .= sprintf('%s%s', $this->table_start("600"), "\n");

    //Query the Guestbook table for total number of entries
    $db->query("SELECT * from Guestbook ORDER by name");
    $totalnum = $db->num_rows();

    //Query with offset and limit functions
    $db->query("SELECT * from Guestbook ORDER by name LIMIT $offset, $limit");
    //If entries exist show them
    if ($db->num_rows() > 0) {
    if ($action != "addnew") {
    $return .= sprintf('<tr><td colspan=2><h2>My sample Guestbook</h2></td></tr>%s', "\n");
    }
    while($db->next_record()) {
    $return .= sprintf('<tr><td colspan=2>&nbsp;</td></tr>%s', "\n");
    $return .= sprintf('<tr><td><b>Name/b></td><td>%s</td></tr>%s', $db->f(name), "\n");
    $return .= sprintf('<tr><td><b>Email/b></td><td><a href="mailto:%s">%s</a></td></tr>%s', $db->f(email), $db->f(email), "\n");
    $return .= sprintf('<tr><td><b>Comments/b></td><td>%s</td></tr>%s', stripslashes($db->f(comments)), "\n");
    $return .= sprintf('<tr><td colspan=2><hr></td></tr>%s', "\n");
    $return .= sprintf('<tr><td colspan=2>&nbsp;</td></tr>%s', "\n");
    }
    $return .= sprintf('<tr><td colspan=2>%s', "\n");
    $return .= sprintf('%s%s', $this->table_start("600"), "\n");
    $return .= sprintf('<tr valign="top"><td align="center">');
    //Create next/prev links
    if ($offset >= $limit) {
    $newoffset = $offset - $limit;
    $return .= sprintf('%s', $this->get_nav($newoffset, "prev"));
    }
    $return .= sprintf('&nbsp;&nbsp&nbsp;');
    if ($totalnum > $limit) {
    $newoffset = $offset + $limit;
    $return .= sprintf('%s', $this->get_nav($newoffset, "next"));
    }
    $return .= sprintf('</td></tr>%s', "\n");
    $return .= sprintf('%s%s', $this->table_end(), "\n");
    $return .= sprintf('&nbsp;</td></tr>%s', "\n");
    $return .= sprintf('<tr><td colspan=2><a href="%s?action=addnew">Click here</a> to add your entry.</td></tr>%s', $PHP_SELF, "\n");

    }

    //Or else show the error message if none exist
    else {
    if ($action != "addnew") {
    $return .= sprintf('<tr><td><h2>My sample Guestbook</h2></td></tr>%s', "\n");
    }
    $return .= sprintf('<tr><td>&nbsp;</td></tr>%s', "\n");
    $return .= sprintf('<tr><td>There are no entries, <a href="%s?action=addnew">click here</a> to add one.</td></tr>%s', $PHP_SELF, "\n");
    }
    $return .= sprintf('%s%s', $this->table_end(), "\n");

    return $return;
    }


    //The addnew() method adds entries to the database
    function addnew() {
    global $action, $db, $PHP_SELF, $HTTP_POST_VARS;


    //Check for submission and if yes extract the $HTTP_POST_VARS array
    if ($HTTP_POST_VARS) extract($HTTP_POST_VARS);

    //Id submission process submission or print form with error showing
    if ($submit) {
    //Check for name being present if its not set error flag to 1
    if (!$name) {
    $error["name"] = 1;
    }
    //If no error enter the record in the db and then display thank you message
    else {
    $db->query(sprintf("INSERT into Guestbook (name, email, comments) VALUES ('%s', '%s', '%s')", $name, $email, addslashes($comments)));
    $return .= sprintf('%s%s', $this->table_start("600"), "\n");
    $return .= sprintf('<tr><td colspan=2><h2>Add an entry to sample Guestbook</h2></td></tr>%s', "\n");
    $return .= sprintf('<tr><td colspan=2><b>Thank you your submission was successful.</b></td></tr>%s', "\n");
    $return .= sprintf('%s%s', $this->table_end(), "\n");
    $return .= sprintf('%s%s', $this->readbook(), "\n");
    }
    }

    if (!$submit || ($error["name"] == 1)) {
    //Show the form for adding entries
    $return .= sprintf('%s%s', $this->form_start($PHP_SELF), "\n");
    $return .= sprintf('%s%s', $this->table_start("600"), "\n");
    $return .= sprintf('<tr><td colspan=2><h2>Add an entry to sample Guestbook</h2></td></tr>%s', "\n");
    $return .= sprintf('<tr><td colspan=2>Required fields are preceded by an <b>*</b></td></tr>%s', "\n");
    $return .= sprintf('<tr><td colspan=2>&nbsp;</td></tr>%s', "\n");

    //If they didn't specify a name make the asterisk red
    $fcolor = "#000000";
    if ($error["name"] == 1) $fcolor = "#ff0000";

    $return .= sprintf('<tr><td><b><font color="%s">*</font> Name: </b></td><td>%s</td></tr>%s', $fcolor, $this->text_box("name", $name, 40, 100), "\n");
    $return .= sprintf('<tr><td><b> Email: </b></td><td>%s</td></tr>%s', $this->text_box("email", $email, 40, 100), "\n");
    $return .= sprintf('<tr><td><b> Comments: </b></td><td>%s</td></tr>%s', $this->text_area("comments", $comments, 40, 10), "\n");
    $return .= sprintf('<tr><td colspan=2>%s%s</td></tr>%s', $this->hidden_input("action", $action), $this->make_button("submit", "Submit", "submit"), "\n");
    $return .= sprintf('%s%s', $this->table_end(), "\n");
    $return .= sprintf('%s%s', $this->form_end(), "\n");
    }
    return $return;
    }



    function get_nav($offset, $direction) {
    global $PHP_SELF;
    if ($direction == "prev") {
    $return .= sprintf('<a href="%s?action=readbook&offset=%s">&lt;&lt; prev</a>%s', $PHP_SELF, $offset, "\n");
    }
    if ($direction == "next") {
    $return .= sprintf('<a href="%s?action=readbook&offset=%s">next &gt;&gt;</a>%s', $PHP_SELF, $offset, "\n");
    }
    return $return;
    }



    //Utility methods ie make forms, html tables, etc..

    //Start form take two args - action and method and defaults to POST
    function form_start($action, $method = "POST") {
    return sprintf('<form action="%s" method="%s">', $action, $method);
    }

    function form_end() {
    return "</form>";
    }

    //Start table method takes width, spacing, padding and border the last three default to 0
    function table_start($width, $spacing = 0, $padding = 0, $border = 0) {
    return sprintf('<table width="%s" cellspacing="%s" cellpadding="%s" border="%s">%s', $width, $spacing, $padding, $border, "\n");
    }

    function table_end() {
    return "</table>";
    }
    //Text box method takes name, value, size and maxlength
    function text_box($name, $value = "", $size, $max) {
    return sprintf('<input type="text" name="%s" value="%s" size="%s" maxlength="%s">%s', $name, stripslashes($value), $size, $max, "\n");
    }
    //Hidden input takes name and value
    function hidden_input($name, $value) {
    return sprintf('<input type="hidden" name="%s" value="%s">%s', $name, $value, "\n");
    }
    //Text area input takes name and value, rows and cols
    function text_area($name, $value = "", $cols, $rows) {
    return sprintf('<textarea name="%s" rows="%s" cols="%s" wrap="virtual">%s</textarea>%s', $name, $rows, $cols, stripslashes($value), "\n");
    }
    //Make button takes type, name and value
    function make_button($name, $value, $type) {
    return sprintf('<input type="%s" name="%s" value="%s">%s', $type, $name, $value, "\n");
    }


    }


    ?>


    Page that calls it.

    <?
    include("guestbook.php");
    ?>
    <html>
    <head>
    <title>Untitled</title>
    </head>

    <body>
    <?
    //Set offset to 0 if it doesn't have a value
    if (!isset($offset)) $offset = 0;

    //INstaniate the guestbook class
    $guestbook = new Guestbook;
    ?>

    </body>
    </html>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not denying the usefulness of classes and methods, just saying that they seem like a pain, and right now I havn't hit any gaps using functions.

    Impressive code, Freddy: mind if I save it and learn from it? I won't use it or anything.

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bah! use it if you want, I am not making any money from it, I wrote it for a resume. It's all yours or anyone else's who wants it, its all about the karma(not the sp karma either) Eventually somehow you will be paid back for all the good you do, not like this was any good just some code my cat wrote!
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a similar method to freddy for forms, but I store all the info about the form in a large array that sets out the type of input, it's name, size or other options - I then pass the array to a function that reads it in and generates the correct HTML.

    I will tidy it up and post it after the weekend (If anyone is interested).

    Also PEAR comes with a library for generating forms too.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.


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
  •