Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Template Class error, expecting ";"

    Okay. A couple things. First off I used to do paid work, but suddenly realizing I had very little real talent in doing certain things on my own (such as making a template class, or file uloading etc...) without a tutorial or something I've decided to take step back and try to make my own fairly simple template class. Now I'm getting an error, and I see nothing line 19 or the lines generally below or above it. I'm wanting to learn theory, and how to code on my own, not how to copy a template Code, so please if you write something it's greatly appreciated, and I WILL NOT copy it, but instead use it too learn! , so any time spent to help me is greatly appreciated!

    My Error:
    Parse error: parse error, expecting `','' or `';'' in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 19
    template_inc.php:
    PHP Code:
    <?php
    /*
    ************************************************************************
    ** Programmed By:  Jonathon M.                                        **
    ** Programmed On:  Jan 03, 2005 to Jan 04, 2005                       **
    ** Program Purpose:                                                   **
    ** Simply put, it's to allow users to select different templates,     **
    ** allow myself to easily change my own templates and to keep my PHP  **
    ** seperate from the HTML                                             **
    ** COPYRIGHT 2005                                                     **
    ************************************************************************
    */

    //Initiate class
    class template {
        var 
    $rarr = array();
        var 
    $arr = array();
        var 
    $n;
        var 
    $this->file;
        
        function 
    template$file$folder ) {
            if( 
    file_exists('./Templates/' $folder '/' $file '.tpl') ) { 
                
    $this->file = include('./Templates/' $folder '/' $file '.tpl');
            } else {
                die(
    "ERROR:  TEMPLATE FILE DOESN'T EXIST");
            }
        }
        
        function 
    setVar$rvar$var ) {
            if( !isset( 
    $this->) ) {
                
    $this->0;
            }
            
    $this->rarr[$n] = $var;
            
    $this->arr[$n] = $var;
            
    $this->n++;
        }
        
        function 
    replaceVar( ) {
            
    $num count$this->rarr );
            
    ksort$this->rarr );
            
    ksort$this->arr );
            foreach( 
    $i 0;  $i $num$i++; ) {
                
    str_replace('{' $this->rarr[$i] . '}'$this->arr[$i], $this->file );
            }
        }
        
        function 
    dispTemp( ) {
            echo( 
    $this->file; );
        }
    }
    ?>
    EDIT: If I am doing this the hard way and adding in too much in, do not hesitate to tell me!
    Last edited by Jonathon; 01-04-2006 at 06:53 AM.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    change var $this->file; to just var $file;
    The $this pointer is used elsewhere to signify the class scope and avoid confusion with a method scope $file.

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Doh. Stuipid error. Thanks!

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay I'm back with another question. At first I thought the parse error was me using the count function wrong but I'm not. Am I using the for() loop wrong? I'll look it up on php.net to see.

    ERROR:
    Parse error: parse error in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 42

    Here is the modified code:

    PHP Code:
    <?php
    /*
    ************************************************************************
    ** Programmed By:  Jonathon M.                                        **
    ** Programmed On:  Jan 03, 2005 to Jan 04, 2005                       **
    ** Program Purpose:                                                   **
    ** Simply put, it's to allow users to select different templates,     **
    ** allow myself to easily change my own templates and to keep my PHP  **
    ** seperate from the HTML                                             **
    ** COPYRIGHT 2005                                                     **
    ************************************************************************
    */

    //Initiate class
    class template {
        var 
    $rarr = array();
        var 
    $arr = array();
        var 
    $n;
        var 
    $file;
        
        function 
    template$file$folder ) {
            if( 
    file_exists('./Templates/' $folder '/' $file '.tpl') ) { 
                
    $this->file = include('./Templates/' $folder '/' $file '.tpl');
            } else {
                die(
    "ERROR:  TEMPLATE FILE DOESN'T EXIST");
            }
        }
        
        function 
    setVar$rvar$var ) {
            if( !isset( 
    $this->) ) {
                
    $this->0;
            }
            
    $this->rarr[$n] = $var;
            
    $this->arr[$n] = $var;
            
    $this->n++;
        }
        
        function 
    replaceVar( ) {
            
    $num count$this->rarr );
            
    ksort$this->rarr );
            
    ksort$this->arr );
            for( 
    $i 0;  $i $num$i++ ) {
                
    str_replace('{' $this->rarr[$i] . '}'$this->arr[$i], $this->file );
            }
        }
        
        function 
    dispTemp( ) {
            echo( 
    $this->file; );
        }
    }
    ?>

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    You're welcome. Templating classes are a very good practice kinda thing to do in PHP, and it can seem like you're taking an unnecessarily long route to the same end as lumping all your display and logic together, but trust me when I say you will save so many headaches in the end. Ask anytime if you need help with any shorter-term headaches.

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    for( $i 0;  $i <= $num$i++ ) { 
                
    str_replace('{' $this->rarr[$i] . '}'$this->arr[$i], $this->file ); 
            } 

  • #7
    New Coder
    Join Date
    Dec 2005
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bleh ignore last post. just me making stupid mistakes again, however I got an error I've never seen:


    Notice: Undefined variable: n in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 33

    Notice: Undefined variable: n in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 34

    Notice: Undefined offset: 1 in c:\program files\easyphp1-8\www\Libs\template_inc.php on line 43



    PHP Code:
    <?php
    /*
    ************************************************************************
    ** Programmed By:  Jonathon M.                                        **
    ** Programmed On:  Jan 03, 2005 to Jan 04, 2005                       **
    ** Program Purpose:                                                   **
    ** Simply put, it's to allow users to select different templates,     **
    ** allow myself to easily change my own templates and to keep my PHP  **
    ** seperate from the HTML                                             **
    ** COPYRIGHT 2005                                                     **
    ************************************************************************
    */

    //Initiate class
    class template {
        var 
    $rarr = array();
        var 
    $arr = array();
        var 
    $n;
        var 
    $file;
        
        function 
    template$file$folder ) {
            if( 
    file_exists('./Templates/' $folder '/' $file '.tpl') ) { 
                
    $this->file = include('./Templates/' $folder '/' $file '.tpl');
            } else {
                die(
    "ERROR:  TEMPLATE FILE DOESN'T EXIST");
            }
        }
        
        function 
    setVar$rvar$var ) {
            if( !isset( 
    $this->) ) {
                
    $this->0;
            }
            
    $this->rarr[$n] = $var;
            
    $this->arr[$n] = $var;
            
    $this->n++;
        }
        
        function 
    replaceVar( ) {
            
    $num count$this->rarr ) or die("ERROR");
            
    ksort$this->rarr );
            
    ksort$this->arr );
            for( 
    $i 0;  $i $num$i++ ) {
                
    str_replace('{' $this->rarr[$i] . '}'$this->arr[$i], $this->file );
            }
        }
        
        function 
    dispTemp( ) {
            echo( 
    $this->file );
        }
    }
    ?>

  • #8
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    I pasted the code here and got a parse error from the duplicate semicolon in this line (48):

    echo( $this->file; );

    echo is not a function, btw, and it's not standard to use parentheses, although it won't hurt anything.

    edit: late :/

  • #9
    New Coder
    Join Date
    Dec 2005
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay i've fixed the variable problem...but the offset, never seen it before don't know what it is?

    PHP Code:
    <?php
    /*
    ************************************************************************
    ** Programmed By:  Jonathon M.                                        **
    ** Programmed On:  Jan 03, 2005 to Jan 04, 2005                       **
    ** Program Purpose:                                                   **
    ** Simply put, it's to allow users to select different templates,     **
    ** allow myself to easily change my own templates and to keep my PHP  **
    ** seperate from the HTML                                             **
    ** COPYRIGHT 2005                                                     **
    ************************************************************************
    */

    //Initiate class
    class template {
        var 
    $rarr = array();
        var 
    $arr = array();
        var 
    $n;
        var 
    $file;
        
        function 
    template$file$folder ) {
            if( 
    file_exists('./Templates/' $folder '/' $file '.tpl') ) { 
                
    $this->file = include('./Templates/' $folder '/' $file '.tpl');
            } else {
                die(
    "ERROR:  TEMPLATE FILE DOESN'T EXIST");
            }
        }
        
        function 
    setVar$rvar$var ) {
            if( !isset( 
    $this->) ) {
                
    $this->0;
            }
            
    $this->rarr[$this->n] = $var;
            
    $this->arr[$this->n] = $var;
            
    $this->n++;
        }
        
        function 
    replaceVar( ) {
            
    $num count$this->rarr ) or die("ERROR");
            
    ksort$this->rarr );
            
    ksort$this->arr );
            for( 
    $i 0;  $i $num$i++ ) {
                
    str_replace('{' $this->rarr[$i] . '}'$this->arr[$i], $this->file );
            }
        }
        
        function 
    dispTemp( ) {
            echo( 
    $this->file );
        }
    }
    ?>

  • #10
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    the problem is that count($array) returns one more key than is actually in $array, because the first key is 0. Change your for loop to loop to $num - 1.

  • #11
    New Coder
    Join Date
    Dec 2005
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I've got to thank you for your help so far! You've been great, and I feel like I'm learning a lot (even if the script isn't anything advanced for you guru coders).

    But... {Hi} isn't being replaced....

    In other words the script returns no errors or anything, yet it just says {Hi}. It doesn't replace "{Hi}" and make it "Hey".

    Here is all the code:
    index.php
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <?php
    require_once("./Libs/template_inc.php");
    //let's go

    $template = new template"index""index" );
    $template->setVar"Hi""Hey");
    $template->replaceVar( );
    $template->dispTemp( );

    ?>
    </td>
    </tr>
    </table>
    </body>
    </html>
    (Don't worry about the HTML lol i know it's screwed up right now...)


    index.tpl
    PHP Code:
    {Hi
    lol really simple ^^

    template_inc.php
    PHP Code:
    <?php 
    /* 
    ************************************************************************ 
    ** Programmed By:  Jonathon M.                                        ** 
    ** Programmed On:  Jan 03, 2005 to Jan 04, 2005                       ** 
    ** Program Purpose:                                                   ** 
    ** Simply put, it's to allow users to select different templates,     ** 
    ** allow myself to easily change my own templates and to keep my PHP  ** 
    ** seperate from the HTML                                             ** 
    ** COPYRIGHT 2005                                                     ** 
    ************************************************************************ 
    */ 

    //Initiate class 
    class template 
        var 
    $rarr = array(); 
        var 
    $arr = array(); 
        var 
    $n
        var 
    $file
         
        function 
    template$file$folder ) { 
            if( 
    file_exists('./Templates/' $folder '/' $file '.tpl') ) {  
                
    $this->file = include('./Templates/' $folder '/' $file '.tpl'); 
            } else { 
                die(
    "ERROR:  TEMPLATE FILE DOESN'T EXIST"); 
            } 
        } 
         
        function 
    setVar$rvar$var ) { 
            if( !isset( 
    $this->) ) { 
                
    $this->0
            } 
            
    $this->rarr[$this->n] = $var
            
    $this->arr[$this->n] = $var
            
    $this->n++; 
        } 
         
        function 
    replaceVar( ) { 
            
    $num count$this->rarr ) or die("ERROR"); 
            
    ksort$this->rarr ); 
            
    ksort$this->arr ); 
            for( 
    $i 0;  $i $num 1$i++ ) { 
                
    str_replace('{' $this->rarr[$i] . '}'$this->arr[$i], $this->file ); 
            } 
        } 
         
        function 
    dispTemp( ) { 
            echo( 
    $this->file ); 
        } 

    ?>

  • #12
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $this->file = include('./Templates/' $folder '/' $file '.tpl'); 
    You can't set the value of a variable with an include...all include returns is a status, the rest is sent directly to the output. To do so you'd have to capture the output with an output buffer, but that's not worth it. You can get the contents of the file and set it to a variable like so:

    PHP Code:
    $this->file file_get_contents('./Templates/' $folder '/' $file '.tpl'); 
    Last edited by Velox Letum; 01-04-2006 at 07:48 AM.
    "$question = ( to() ) ? be() : ~be();"

  • #13
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    What Velox said, also a few minor notes:
    Quote Originally Posted by Jonathon
    PHP Code:
    //...
        
    function setVar$rvar$var ) { 
     
    //...
            
    $this->rarr[$this->n] = $var// this should be assigned to $rvar I think
    //...
        
    function replaceVar( ) { 
    //...
            
    for( $i 0;  $i $num 1$i++ ) { // the second argument needs to be a comparison, you have assignment.  Use $i < $num; or $i <= $num - 1; 


  •  

    Posting Permissions

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