SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to simplfy some table building using functions.

    I have the following variables, say $cellspacing, $cellpadding, $border, $trcolor1, $trcolor2 sort of thing.

    Now these are got from my global.php type include that gets all the settings.

    What i want is a function that i send an array of data... it then creates the table (these are fixed column tables) with the values above and alternates through the colours for the <tr> codes.

    That is all fine but my problem is i don't know the best way to get cellspacing... etc into the function which will be in say functions.php. is it just a matter of doing global ($cellspacing, $cellpadding, $border); etc.. and are there any problems with doing globalising of lots of variables like this?

  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)
    Normally if I have to use php to create tables, like as part of a class or something I do this

    PHP Code:
    function table_start($width$border 0$cellspacing ""$cellpadding ""$extrahtml "") {
    return 
    sprintf('<table width="%s" border="%s", cellspacing="%s", cellpadding="%s" %s>%s'$width$border$cellspacing$cellpadding$extrahtml"\n");
    }
    function 
    table_end() {
    return 
    "</table>\n";
    }

    //Usage
    print table_start(600,0,0,0);
    print 
    "<tr><td>Hello World</td></tr>\n";
    print 
    table_end(); 

    Is this what you are after?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried:

    PHP Code:
    $table table("hello my name is chris");
    echo 
    $table;


    function 
    table($value) {
    global 
    $cellspacing$cellpading$border;
    $table "<table cellspacing=\"$cellspacing\" cellpading=\"$cellpading\" border=\"$border\">";
    $table .="<tr><td>$value</td></tr>";
    $table .="</table>";
    return 
    $table;

    this is very basic version, there is going to be a lot more to it than that..

    but on your code i have a couple of questions:

    basiclly, what are the %s etc variables? i don't understand that line . The first thing i ever tried programming years ago was for a psion 3a, OPL i think it was called and % was for numbers don't think that is what it is here though or is it? Is the sprintf variable replacing the two or something?

    also, what is the difference between print and echo?

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the reason why i am not wanting to pass the cellspacing etc variables is because this is going to be for a very specific table (the main one on each page basically) and the variables will always be the same (from the settings) and the class will be used to just make one table.. but it will be a big one with loops and things going on but the cellspacing etc will always be the value from the "user settings" in the database which are already known.

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    return sprintf('<table width="%s" border="%s", cellspacing="%s", cellpadding="%s" %s>%s'$width$border$cellspacing$cellpadding$extrahtml"\n"); 

    Originally posted by Padders

    basiclly, what are the %s etc variables?

    If you look at the line, the variables that are used in the line have been put after the stuff to be printed. Each %s is replaced by one of those variables. The first occurence of %s in the string is replaced by $width as it is the first variable on the list, the second by $border and so on. It's just a different way of doing echo(); or print(); statements.

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    arr, i see. thanks James.

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding the difference between echo and print. Print is a function and returns a
    boolean true if the function was able to output successfully. I don't think capturing
    this return value is very relevent in sending data to Apache ?!? echo is a language
    construct (not a function). What does that mean? Buggered if I know I think it
    means that it is slightly more efficient at some hair-splitting subatomic level.

    IMHO, printf and sprintf functions are most useful for formating floats (set
    preceision), currency values, setting the width of strings, left or right justify, etc.
    I don't like these functions for more simple output where print or echo would suffice.
    I know that freddydoesphp finds printf and sprintf more readable, or neat or
    whatever, rather than throwing the variable names directly into the string. There
    you are, cats certainly are unique creatures . I say, each to their own. Just thought
    I'd thow that in though to let you know that you can do it either way.

  8. #8
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the explanation i am used to echo now so will use that i think but i was seeing a lot of printf/print in books and example scripts i was wondering if i was doing it wrong for some reason

    thanks.


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
  •