2012-10-17 25 views
0

我對OOPS和Zend相當陌生。我將一部分Web應用程序代碼轉換爲Zend框架,我不能在這裏發佈所有代碼,因爲它相當長。我在下面的代碼中創建了一個模型(我只能發佈兩個函數),我想從控制器訪問函數內部的變量,然後在HTML表格中打印出來(我想我必須使用「視圖」認爲」,下面是我的模型:在Zend框架中從控制器訪問模型中聲明的函數中的變量

public function getVenueTypes() 
{ 
$poiTypes = array(); 
    if($this->_cache) 
    { 

     $key = $this->getCacheKey()."poiTypes_stats:{$this->language}"; 
    } 
    if(!$poiTypes) 
    { 
     $sql = "SELECT poi_type_id, poi_type_name_".$this->language." 
       AS 
       poi_type_name 
       FROM 
       poi_types 
       ORDER BY 
       poi_type_name_".$this->language." ASC"; 
     $result = mysql_query($sql); 
     if(!$result) 
     { 
      $message = 'Invalid query: ' . mysql_error() . "\n"; 
      $message .= "Whole query: \n\n$sql\n"; 
      die($message); 
     } 
     while($row = mysql_fetch_array($result)) 
     { 
      $poiTypes[] = array('id' => $row['poi_type_id'] ,'name' => $row['poi_type_name']); 
     } 
     print_r($poiTypes); 
     if ($this->_cache) 
     { 
      $this->_cache->save($key, $poiTypes);    

     }  
    } 
foreach($poiTypes as $poiType) 
    { 
     $count_type = null; 
     if($this->_cache) 
     { 
    $key = $this->getCacheKey()."poiTypeCount:{$poiType['id']}:{$this->metro_id}"; 
      $count_type = $this->_cache->load($key); 
     } 
     if(!$count_type) 
     { 
    $sql = "SELECT COUNT(*) FROM 
       poi 
       WHERE 
       find_in_set('{$poiType['id']}', poi_type_id_array) 
       AND poi_status = 1 AND poi_address_prefecture_id = '$this->metro_id'"; 
      $result = mysql_query($sql) or die(mysql_error()); 
     } 
     if(!$result) 
      { 
       $message = 'Invalid query: ' . mysql_error() . "\n"; 
       $message .= "Whole query: \n\n$sql\n"; 
       die($message); 
      } 

     while($count = mysql_fetch_array($result)) 
     { 
      $count_type[$poiType['id']][$this->metro_id] = $count['COUNT(*)']; 
     } 
     if($this->_cache) 
     { 
    $this->_cache->save($key, $count_type); 
     } 
    } 
} 

任何幫助表示高度讚賞,我希望我是不夠清楚,並在此先感謝

回答

0

我諾伊知道這是否會幫助你,但你有幾個選項來訪問這些值。

選項1:返回一個包含所需變量的數組

return array($myVar1, $myVar2, ..., $myVarN); 

選項2(較少模塊化):在該函數內創建html字符串,然後回顯或返回字符串。

0

林假設你有Zend的

Zend框架的基本概念的基本知識BlaBla.php控制器,模型和視圖

模型文件之間的鏈接,

Class Namespace_models_BlaBla extends Zend_Db_Table_Abstract{ 

    public function foo(){ 
     return 'hello world'; 
    } 
} 

你的控制器說barcontroller和一個動作說測試

class BarController extends extends Zend_Controller_Action{ 

     public function testAction(){ 
      $class = new Namespace_models_BlaBla(); 
      $this->view->string = $class->foo(); 

     } 

    } 

你的HTML側// test.phtml

<?php echo $this->string; ?> /**this will output hello world **/ 
+0

是的,我有基本的瞭解,所以可能是我不夠明確,我只是發佈一個功能,而不在模型和控制器類聲明,使之短。我如何從控制器訪問變量,如$ count_type [$ poiType ['id']] [$ this-> metro_id](來自上面的代碼)?如果我的問題不成熟,我很抱歉,我想學習OOPS和Zend。感謝您的回覆,希望能聽到更多。 – Uday

相關問題