2012-02-07 15 views
0

私は、この関数のアトラクションの種類にユーザーを指示し、このアプリケーションがあります。

public function index($type=null) { 
     $this->set('title','What to do when you visit Gulf Shores'); 

     $this->paginate['Attraction']=array(
       'limit'=>9, 
       'order'=>array('Attraction.id'=>'asc'), 
       'conditions'=>array(
         'active'=>1, 
         'attr_type'=>$type 
         ) 
       ); 
     $c=$this->paginate('Attraction'); 
     $this->set('attractions', $c); 

} 

をし、それは素晴らしい作品が、私は、ユーザーがまたdoesnのこと/フロントページ/アトラクションに行くことができるようにしたいのですがattr_typeでフィルターをかけないでください。この関数は、フロントページにはゼロ結果(明らかに$ type still = null)を示します。不足しているステップがありますか、またはコントローラにview.ctpファイルと機能が必要ですか?

答えて

0

声明場合は、条件を決定するために使用することができます

public function index($type = null) { 
    $this->set('title', 'What to do when you visit Gulf Shores'); 

    $conditions = array(); //create $conditions outside of the if statement 
    if ($type) { //if $type is equal to anything other than null or 0 
     $conditions = array(
      'active' => 1, 
      'attr_type' => $type 
     ); 
    } else { 
     $conditions = array(
      'active' => 1 
     ); 
    } 

    $this->paginate['Attraction'] = array(
     'limit' => 9, 
     'order' => array('Attraction.id' => 'asc'), 
     'conditions' => $conditions 
    ); 
    $c = $this->paginate('Attraction'); 
    $this->set('attractions', $c); 
} 

これは、PHPでif文の$conditions外を作成するために、実際には必要ありませんが、それが原因で範囲を他のプログラミング言語の多くです。

If you create a variable inside a if statement is it available outside the if statement?

+0

[OK]を、私は道があったことを考えていた(私は、私はループを使用してステートメントと、すべてあればできるということを忘れ何らかの理由でコントローラに入る一回!)ワイリーは、あなたはすぐに私になっています救世主。 – huzzah

関連する問題