2016-06-23 8 views
-2

私は怒鳴るように、テンプレートエンジンを使用して、私のテンプレートで同様にgetCount()関数の結果を示すに問題がある:なぜ間違った場所でshowデータエコー(私が意味することは、テンプレートの外に表示されます)

<?php 
class Template { 
    protected $file; 
    protected $values = array(); 
    public function __construct($file) { 
     $this->file = $file; 
    } 
    public function set($key, $value) { 
     $this->values[$key] = $value; 
    } 
    public function output() { 
     if (!file_exists($this->file)) { 
      return "Error loading template file ($this->file).<br />"; 
     } 
     $output = file_get_contents($this->file); 

     foreach ($this->values as $key => $value) { 
      $tagToReplace = "[@$key]"; 
      $output = str_replace($tagToReplace, $value, $output); 
     } 

     return $output; 
    } 
    static public function merge($templates, $separator = "\n") { 
     $output = ""; 

     foreach ($templates as $template) { 
      $content = (get_class($template) !== "Template") 
       ? "Error, incorrect type - expected Template." 
       : $template->output(); 
      $output .= $content . $separator; 
     } 

     return $output; 
    } 
    public function newConnection($table,$name,$w,$wt){    
     $Query = mysql_query("SELECT * FROM {$table} WHERE {$w} = {$wt} ORDER BY id DESC");    
$Arrays.$name = array();  
while($row = mysql_fetch_array($Query,MYSQL_ASSOC)){ 
    $Arrays.$name[] = $row; 
    } 
    } 
} 

と私は怒鳴るindex.phpを実行します。

<?php 

// Start POSTS 
$postQuery = mysql_query("SELECT * FROM posts ORDER BY id DESC");    
$postArrays = array();  
while($rows = mysql_fetch_array($postQuery,MYSQL_ASSOC)){ 
    $postArrays[] = $rows; 
    }      
foreach($postArrays as $post) { 
    $row = new Template("{$tpl_dir}/post.tpl");  
    foreach ($post as $key => $value) { 
     $row->set($key, $value); 
    } 
    $postsTemplates[] = $row; 
}  
$postsContents = Template::merge($postsTemplates); 
$header = new Template("{$tpl_dir}/header.tpl"); 
$header->set("title",$stitle); 
$header->set("stylesheet","{$tpl_dir}/style.css"); 
$sidebar = new Template("{$tpl_dir}/sidebar.php"); 
$sidebar->set("static",getCount()); 
$sidebar->set("category",getCat()); 
$ads = new Template("{$tpl_dir}/ads.tpl"); 
$ads->set("tempurl",$tpl_dir); 
$footer = new Template("{$tpl_dir}/footer.tpl"); 
$index = new Template("{$tpl_dir}/index.tpl"); 
$index->set("header", $header->output()); 
$index->set("ads", $ads->output()); 
$index->set("sidebar", $sidebar->output()); 
$index->set("posts", $postsContents);   
$index->set("footer", $footer->output());  

echo $index->output(); 

機能同様にgetCount()が正常に動作しますが、それがこの絵のようにテンプレートからのデータを示しています see problem as picture

functiコード:

<?php 
function getCat($level = 0) {  
    $sql = mysql_query("SELECT * FROM category WHERE cparent = 0 ORDER BY id DESC"); 
    if (mysql_num_rows($sql) > 0) { 
     echo "<ul class='cat-parent'>"; 
      while($row = mysql_fetch_assoc($sql)) { 
       echo "<li><a href='showpost.php?cat=".$row['id']."'>".$row['cname']."</a>"; 
        getsubCat($level, $row['id']); 
       echo "</li>"; 
      } 
     echo "</ul>"; 
    } 
} 
function getsubCat($level, $id) {   
    $sqlSubCat = "SELECT * FROM category WHERE cparent = ".$id.""; 
    $resultSubCat = mysql_query($sqlSubCat); 
    if (mysql_num_rows($resultSubCat) > 0) { 
     echo "<ul class='cat-child'>"; 
      while($rowSubCat = mysql_fetch_assoc($resultSubCat)) { 
       echo "<li><a href='showpost.php?cat=".$rowSubCat['id']."'>".$rowSubCat['cname']."</a>"; 
        getsubCat($level, $rowSubCat['id']); 
       echo "</li>"; 
      } 
     echo "</ul>";   
    } 
} 
?> 

どのようにこの問題を解決できますか?

答えて

0

出力バッファリングが有効になっていない場合、エコーが直接出力されます。あなたの場合は、テンプレートのレンダリングの前に。

あなたは2つの可能性があります。

  1. 変更getCat(およびgetsubCat)機能文字列を返す代わりに

  2. <?php 
    function getCat($level = 0) { 
        $output = '';  
        $sql = mysql_query("SELECT * FROM category WHERE cparent = 0 ORDER BY id DESC"); 
        if (mysql_num_rows($sql) > 0) { 
         $output .= "<ul class='cat-parent'>"; 
          while($row = mysql_fetch_assoc($sql)) { 
           $output .= "<li><a href='showpost.php?cat=".$row['id']."'>".$row['cname']."</a>"; 
           $output .= getsubCat($level, $row['id']); //with getsubCat also returning a string instead of using echo 
           $output .= "</li>"; 
          } 
         $output .= "</ul>"; 
        } 
        return $output; 
    } 
    

    エコーを使用する出力バッファリング

    ob_start(); 
    getCat(); 
    $sidebar->set("category", ob_get_clean()); 
    
+0

あなたはおそらく答えを受け入れることはできますか? – Mairu

関連する問題