39 users online (0 members and 39 guests)  


  Results 1 to 2 of 2

Related

  1. Replies: 9
  2. Replies: 2
  3. Site doesn't work properly on IE5    Forum: CSS Forum
    Replies: 3
  4. .htaccess doesn't work    Forum: HTML Forum
    Replies: 1
  5. js script that doesn't work ???    Forum: Javascript Forum
    Replies: 1
  1. #1
    userror's Avatar
    New User

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    1

    Unhappy template code doesn't work (me desperate)

    I know this post is of scary size, but I have no another way to represent the
    problem.
    Although it looks scary, it takes not more than 2 mins to get into the essence, so I beg you to help me out.

    I have a big templating problem, trying to make some Address Book to work.

    I'm working by the book "A Programmer’s Introduction to PHP 4.0" W. J. Gilmore, in which is that sample templating system.

    But the given code doesn't work, and I think I know why is that.

    book.php - template file 1
    PHP Code:
    <html
    <
    head
    <
    title>:::::{page_title}:::::</title
    </
    head
    <
    body bgcolor="white"
    <
    table cellpadding=2 cellspacing=2 width=600
    <
    h1>Address Book: {letter}</h1
    <
    tr><td
    <
    a href="index.php?letter=a">A</a> | <a href="index.php?letter=b">B</a> | 
    <
    a href="index.php?letter=c">C</a> | <a href="index.php?letter=d">D</a> | 
    <
    a href="index.php?letter=e">E</a> | <a href="index.php?letter=f">F</a> | 
    <
    a href="index.php?letter=g">G</a> | <a href="index.php?letter=h">H</a> | 
    <
    a href="index.php?letter=i">I</a> | <a href="index.php?letter=j">J</a> | 
    <
    a href="index.php?letter=k">K</a> | <a href="index.php?letter=l">L</a> | 
    <
    a href="index.php?letter=m">M</a> | <a href="index.php?letter=n">N</a> | 
    <
    a href="index.php?letter=o">O</a> | <a href="index.php?letter=p">P</a> | 
    <
    a href="index.php?letter=q">Q</a> | <a href="index.php?letter=r">R</a> | 
    <
    a href="index.php?letter=s">S</a> | <a href="index.php?letter=t">T</a> | 
    <
    a href="index.php?letter=u">U</a> | <a href="index.php?letter=v">V</a> | 
    <
    a href="index.php?letter=w">W</a> | <a href="index.php?letter=x">X</a> | 
    <
    a href="index.php?letter=y">Y</a> | <a href="index.php?letter=z">Z</a
    </
    td></tr
    {
    rows.addresses
    </
    table
    </
    body
    </
    html
    rows.addresses - template file 2
    PHP Code:
    <tr><td bgcolor="#c0c0c0"
    <
    b>{last_name},{first_name}</b
    </
    td></tr
    <
    tr><td
    <
    b>{telephone}</b
    </
    td></tr
    <
    tr><td
    <
    b><a href "mailto:{email}">{email}</a></b
    </
    td></tr
    index.php - executive(calling) file
    PHP Code:
    <? 
    include("template.class"); 
    $page_title "Address Book"
    // The default page will retrieve persons having last name beginning with 'a' 
    if (! isset($letter) ) : 
    $letter "a"
    endif; 
    $tpl = new template
    $tpl->register_file("book""book.php"); 
    $tpl->register_variables("book""page_title,letter"); 
    $tpl->address_sql("book""rows.addresses","$letter"); 
    $tpl->file_parser("book"); 
    $tpl->print_file("book"); 
    ?>
    I've attached template.class file

    Note that the third template variable {rows.addresses} has the dot in its name (not valid variable name).
    By the registration of this var. (in address_sql function), autor simply forwarded file name (rows.addresses - the second templ.file) to variables array [marked with aaaaaaaa in the attached code].

    Well, the sequence of events (index.php) is:
    1) file registration (book.php)
    2) registration of 2 variables ({page_title} and {letter}) - in my opinion the third var.{rows.addresses} can be registrated here too, but never mind.
    3) preparing the collective variable $complete_table by filling it with the sequence of the contents(html code) of rows.addresses templ. file, with the template vars {}, replaced with the corresponding sql query values.

    The problem here is twofold:
    a) rows.addresses is not a valid php var. name
    b) prepared $complete_table var. is not forwarded to file_parser function (at least, I can't see that)

    So, the result I get, is the first two templ. vars replaced, but not the third one (rows.addresses) which one actually represents the result of the query.
    I've attached the image of what I get - i_get.gif, and what should it be - should_be.gif

    To avoid these problems I made the following modifications:
    - I've replaced {rows.addresses} with {rows} in book.php

    - I've introduced a new variable $rows in template.class. This variable should get the value of $complete_table,

    PHP Code:
    VAR $rows "";             
    . . .
    . . .
    $complete_table .= $new_row
    endwhile; 

    $rows $complete_table;     //THIS IS THE ESSENCE OF MODIFICATION       

    // Assign table substitution string to SQL array key 
    $sql_array_key “rows”;                                   
    $this->sql[$sql_array_key] = $rows;
                     
    //CAN'T SEE THE PURPOSE OF THIS SQL ARRAY, but no matter
    // add the key to the variables array for later lookup 
    $this->variables[$file_id][] = “rows”//REGISTRATION OF THE THIRD(MY) VAR    
               

    // Close the filehandle 
    fclose($fh); 
    . . . 
    and after, participate in file_parser function (its WHILE loop) by replacement of template variable {rows}
    PHP Code:
    // from TEMPLATE.CLASS
    . . .
    GLOBAL $
    $string;                       
    // What exactly is to be replaced in the file contents? 
                
    $needle $this->opening_escape.$string.$this->closing_escape
    // Perform the string replacement. 
                
    $this->files[$file_id] = str_replace
                
    $needle// needle 
                
    $$string// string        
                
    $this->files[$file_id]); // haystack 
        // increment $x 
                
    $x++; 
                endwhile;
    . . . 
    Well, now, in the third (while)loop iteration will be $string='rows',
    subsequently GLOBAL $rows should represent the contents of
    collective $complete_table variable
    .

    But again the replacement of {rows} doesn't take place ???

    What the **** I'm doing wrong ?

    I would emphasize that my MySQl connection works ok, communication with the 'book'db through the command line is ok, 'addressbook' table filled out with several records (for "a" letter, among them), permissions for the 'book'db and all of php files are set to allowed to everyone.



    Thanks ALOT for your replies
    -
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by userror; 09-18-2005 at 06:06 PM.

  2. #2
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: template code doesn't work (me desperate)

    Your basic assumption is flawed, I believe.
    Note that the line you refer to is
    Code:
    $tpl->address_sql("book", "rows.addresses","$letter");
    and the implication from the address_sql part is that it refers to a database.
    This would mean that rows.addresses is not a variable, but a reference to the SQL table, and therefore valid.
    I'd recheck the book - it would be very unusual for the code in the book to be flawed ... not impossible, but very unusual.



Tags for this Thread