Hi,

suppose I have the script follows:

index.php

// select 10 records containing 'name', 'email', 'url', 'location', 'message'
$qid = $DB->query("SELECT * FROM guestbook LIMIT 0, 10");

I want to loop through $qid and display the records, how do I code the script and template file? I try to use '{foreach}{/foreach}', but it can't loop through a data object.

The template format can be simple as:

{$name}<br>
{$email}<br>
{$url}<br>
{$location><br>
{$message}<br>

Thanks:rolleyes:

    Put that info in a template file of its own and do the following:

    $qid = $DB->query("SELECT * FROM guestbook LIMIT 0, 10");
         while($gqid = $DB->fetch_array($qid)){
              $name = $gqid['name'];
              $email = $gqid['email'];
              $location = $gqid['location'];
              $url = $gqid['url'];
              $message = $gqid['message'];
          eval ("\$loopeach .= \"".gettemplate("that_template")."\";");
       }
    

    Now, just put $loopeach in for where you want it to appear.
    Sorry, thats using a vbulletin format as well. Is that workable for you, or did you want an echo out instead?

      You could fetch your DB query as an array that looks like

      $result[0]['name'] = 'foo0';
      $result[0]['email'] = 'bar0';
      $result[1]['name'] = 'foo1';
      $result[1]['email'] = 'bar1';

      Then assign this to a smarty variable

      $smarty->assign_by_ref('dbresult', $result);

      Then you could pass this array in your template

      {section name=loop loop=$dbresult}
      {$dbresult[loop].name}
      <br><br>
      {$dbresult[loop].email}
      {/section}

      The output would be:

      foo0
      <br><br>
      bar0
      foo1
      <br><br>
      bar1

        well, I found that vbulltine's template's method to be useful and easy as Smarty too. So I totally changed my mind to implement vbulltine's template method instead of Smarty. Thanks for the hint.

        BTW: any security concern on use of eval()?

        Thanks!:rolleyes:

          6 days later

          Hey people, this is very interesting, Im glad I stumbled across this post. I am currently starting to develop a new site, and I decided I wanted to use a template engine. Now I have started lookign at smarty using a PEAR DB class, but if it is possible I would love to use the vBulliten template engine as one of my other sites runs vBulletin/vBportal and I have done alot of modifications so am quite used to the vb template engine.

          Is there anywhere I can find details on using this template engine in my own developments, and on using template files with this rather than havign the templates stored in the DB (as vBulletin does)?

          Many Thanks,

          Justin.

            Hi, here is my getTemplate function which pulls template from file instead of DB:

            function getTemplate($templateName, $escape = true)
            {
                if (@file_exists("template/".$templateName.".tpl"))
                  die("Template file '$templateName'.tpl could not be found.");
            
            $template = @implode("", @file("template.".$templateName.".tpl));
            if (empty($template))
              die("Template '$templateName' is empty.");
            
             if ($escape) {
                $template = addslashes($template);
                $template = str_replace("\\'", "'", $template);
             }
            
             return $template;
            }
            

            Examples:

            // escape is true
            eval("\$MESSAGE_TRS .= \"".getTemplate("message_tr")."\";");
            // set escape to false
            $MESSAGE_TABLE = getTemplate("first_sign_link", false);
            

            Hope it helps.

              Thanks, is there somewhere I can just get the template engine rather than just ripping it from my VB install?

                Write a Reply...