Cannot use object of type templateParserNoCache as array

Discussion in 'Web Design and Development' started by Cabbit, Nov 14, 2009.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    I am trying to print out a feed of posts into a html template that gets pushed into the page and cached. However i seem to only know how to print out one record into the html file instead of being able to built it up.

    Best way i can explain it is i want posts to be some kind of array that keeps adding the $posts->display() of every record in the table to the end of the last one so posts would end up like.
    HTML:
    <h2>Title</h2>
    <p>Body text bla bla bal</p>
    <h2>Tittle2</h2>
    <p>body 2</p>
    <!-- etc. -->
    
    where just now the output is
    HTML:
    <h2>Tittle2</h2>
    <p>body 2</p>
    
    PHP:
    /* Connect to the table */
            
    $task "dbmysql";
            
    $query_select "select `id`, `body`, `title`, `author`, `date` from `homePage`";
            
    $database_name "abcomfor_groupproject";

            
    /* Initialise the database */
            
    $dbC = new DbControl($task);
            
    $dbC->selectDb($database_name);
            
    $dbC->setQuery($query_select);
            
    $dbR $dbC->initiate();
            
            
    // Cycles though the results to see if the user exists //
            
    while ($dbR->next())
            {
                 
    /* Feed the post into the template */
                
    $postFeed=array
                (
                    
    'title'=>$dbR->get("title"),
                    
    'body'=>$dbR->get("body"),
                    
    'author'=>$dbR->get("author"),
                    
    'date'=>$dbR->get("date"),
                    
    'body'=>$dbR->get("body")
                );
                
                
    $posts =&new templateParserNoCache('../application/modules/bootstrap/views/posts.tpl.htm');
                
    $posts->parseTemplate($postFeed);
        
                
    // Echos the product //
                
    $posts $posts->display();
            }
            echo 
    $posts;
     
  2. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #2
    Been banging my head against the wall all weekend now, i had some success with a foreach loop but i got errors back saying i could not store the $post->display as part of an array.
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    PHP:
    $i 0;
            
    // Cycles though the results to see if the user exists //
            
    while ($dbR->next())
            {    
                
    $i $i 1;
                 
    /* Feed the post into the template */
                
    $postFeed=array
                (
                    
    'title'=>$dbR->get("title"),
                    
    'body'=>$dbR->get("body"),
                    
    'author'=>$dbR->get("author"),
                    
    'date'=>$dbR->get("date"),
                    
    'body'=>$dbR->get("body")
                );
                
                
    $posts =&new templateParserNoCache('../application/modules/bootstrap/views/posts.tpl.htm');
                
    $posts->parseTemplate($postFeed);
        
                
    // Echos the product //
                
    $posts[$i] = $posts->display();
            }
            echo 
    $posts;
    By my logic this should take each post and build and array of them which is then echo'ed out as $posts. However i get a error message i have not seen before.

    [16-Nov-2009 21:51:11] PHP Fatal error: Cannot use object of type templateParserNoCache as array in /nanashi/application/modules/bootstrap/bootstrap.php on line 66
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Is templateParserNoCache something you created or is it a library? That seems to be where you need to look for getting this to work. If it's a library it should have some documentation to figure out how to use it as an array.
     
  5. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #5
    PHP:
    <?php
    /*
        templateParserNoCache
        KittenBunny 2.0
        David Roy
    */

    // uncached version
    class templateParserNoCache
    {
        
    // Variable for output
        
    public $output;
        
        
    // Checks for existance of the template file. //
        
    public function templateParserNoCache($templateFile='default_template.htm')
        {
            (
    file_exists($templateFile))?$this->output=file_get_contents($templateFile):die('Error:Template file '.$templateFile.' not found');
        }
        
        
    // Gets the tag array. //
        
    public function parseTemplate($tags=array())
        {
            if(
    count($tags)>0)
            {
                foreach(
    $tags as $tag=>$data)
                {
                    
    $data=(file_exists($data))?$this->parseFile($data):$data;
                    
    $this->output=str_replace('{'.$tag.'}',$data,$this->output);
                   }
            }
            else 
              {    
                
    // If no tags are found return a error. //
                  
    die('Error: No tags were provided for replacement.');
            }
        }
        
        
    // Injects the tags into the template. //
        
    public function parseFile($file)
          {
            
    ob_start();
            include(
    $file);
              
    $content=ob_get_contents();
               
    ob_end_clean();
               return 
    $content;
         }

        
    // Returns the HTML page. //
        
    public function display()
        {
             return 
    $this->output;
        }

    }

    ?>
    this is it here.
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    I don't completely follow all of the code, but something I think I'm seeing is that when you're outputting the posts (using ->display()) it's doing that through the $output variable. Each templateParserNoCache object though has its own $output variable. So I think you're only seeing one post because it's only referencing the last post created. Again, that's a rough guess based on what I'm looking at.

    I'm not really sure what
    PHP:
    $posts =&new...
    does either. I've never used that syntax (the =& part).
     
  7. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #7
    If i change it to

    PHP:
            // Cycles though the results to see if the user exists // 
            
    while ($dbR->next()) 
            {     
                 
    /* Feed the post into the template */ 
                
    $postFeed=array 
                ( 
                    
    'title'=>$dbR->get("title"), 
                    
    'body'=>$dbR->get("body"), 
                    
    'author'=>$dbR->get("author"), 
                    
    'date'=>$dbR->get("date"), 
                    
    'body'=>$dbR->get("body"
                ); 
                 
                
    $posts =&new templateParserNoCache('../application/modules/bootstrap/views/posts.tpl.htm'); 
                
    $posts->parseTemplate($postFeed); 
         
                
    // Echos the product // 
                
    echo $posts->display(); 
            } 
    All the results are printed out so i know this bit works, however i need to be able to build up an array so i can print all the results in another template.
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    Try this out. Note that a the top and near the bottom I have "or" comments where you should only use one or the other line.
    PHP:
    $posts_out = array();
    // or
    $posts_out '';
            
    // Cycles though the results to see if the user exists //
            
    while ($dbR->next())
            {    
                 
    /* Feed the post into the template */
                
    $postFeed=array
                (
                    
    'title'=>$dbR->get("title"),
                    
    'body'=>$dbR->get("body"),
                    
    'author'=>$dbR->get("author"),
                    
    'date'=>$dbR->get("date"),
                    
    'body'=>$dbR->get("body")
                );
                
                
    $posts =&new templateParserNoCache('../application/modules/bootstrap/views/posts.tpl.htm');
                
    $posts->parseTemplate($postFeed);
        
                
    // Echos the product //
                
    $posts_out[] = $posts->display();
                
    // or
                
    $posts_out .= $posts->display();
            }
            echo 
    $posts_out;
     
  9. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #9
    ^_^ fanks, working braw using

    PHP:
    $posts_out ""
            
    // Cycles though the results to see if the user exists // 
            
    while ($dbR->next()) 
            {     
                 
    /* Feed the post into the template */ 
                
    $postFeed=array 
                ( 
                    
    'title'=>$dbR->get("title"), 
                    
    'body'=>$dbR->get("body"), 
                    
    'author'=>$dbR->get("author"), 
                    
    'date'=>$dbR->get("date"), 
                    
    'body'=>$dbR->get("body"
                ); 
                 
                
    $posts =&new templateParserNoCache('../application/modules/bootstrap/views/posts.tpl.htm'); 
                
    $posts->parseTemplate($postFeed); 
         
                
    // Echos the product // 
                // or 
                
    $posts_out .= $posts->display(); 
            } 
    I do however have a question, can you explain or point me in the direction to how this method works and where its use is appropriate to a the type of array i was trying to use.
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    I don't quite get what you're asking. The .= is just a string concatenation. So the entire output is kept as a big long string. When you echo out an array object, it only says Array, it doesn't actually give the content of the array. You would need a loop to iterate through it. You also may have been reusing $posts too much. I think part of it was getting overwritten.
     
  11. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #11
    This was the bit i did not understand, but from my gathering you say it just makes no matter what you put into it a string.
     
  12. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #12
    Well, strings are what you were working with. The $output seems to be just a string, and that's what comes back from display(). I've only ever used .= for strings. I don't know how it reacts to other data types.
     

Share This Page