2012-11-14 66 views
1

好吧,我試圖驗證從數據庫訂閱(這似乎工作正常,因爲我在其他地方使用過這種模式)。但是,我試圖拿回返$數據,並在我的看法中使用它作爲IF聲明...但我試圖「如果」沒有出現的DIV。也不會出現任何錯誤。如果我刪除視圖中DIV周圍的IF,內容將會出現......我做錯了什麼?IF聲明在視圖 - 沒有出現

下面是我的控制器代碼:

// Check subscription and display appropriate content if "freebie" package 
$subscribe_result = $this -> subscribe_model -> upgrade_message($this->session->userdata('user_id')); // session sends user id to model 
$data['subscription'] = $subscribe_result; 

型號:

// Checks to see what package user has subscribed to 
public function upgrade_message($id) //$id is session id pulled from controller 
{ 
    $this -> db -> select('subscription'); // select the subscription column 
    $this -> db -> where('id', $id); //find id in table that matches session id 
    $query = $this -> db -> get("subscriptions"); // connect to this database 

    return $query->result_array(); //returns the result of the above 
} 

終於,在我試圖輸出的視圖我IF語句:

<? if($subscription == 'Freebie') : ?> 
    <div>some "freebie" content</div> 
<? endif ?> 
+0

如果你做<? var_dump($ subscription)?>在你的視圖中 - 你會看到變量包含的內容。我敢打賭,它不是「免費贈品」。 – Laurence

+0

'$ query-> result_array();'返回一個數組,你正在檢查'Freebie',這是問題所在。 –

+0

這裏是var dump:'array(1){[0] => array(1){[「subscription」] => string(7)「Freebie」}}' –

回答

1

您可以使用

if($subscription[0]['subscription'] == 'Freebie') 

代替

if($subscription == 'Freebie') 

更多Generating Query Results

+0

Works !!我發誓我第一次有這個 - AGH!今天進行了13小時的編碼,也許是時候小睡了。大聲笑:)乾杯謝赫! –

+0

歡迎您:-) –

+0

您可能希望在調用'$ subscription [0]'前先檢查'$ subscription'是否有值,否則如果沒有值,將導致錯誤...;) – Gavin