2013-01-05 20 views
3

我試圖修改投票腳本(Thumbsup)我需要這個查詢只能返回在子陣的「貓」 =>「1」僅數組項(適當的期限?)如何修復此查詢以僅返回其子數組中具有特定值的項目?

<?php $items = ThumbsUp::items()->get() ?> 

這裏是我的數據庫中生成的實時數組的示例

array (
    0 => 
    array (
    'id' => 1, 
    'name' => 'a', 
    'cat' => '1', 
), 
    1 => 
    array (
    'id' => 2, 
    'name' => 'b', 
    'cat' => '2', 
), 
    2 => 
    array (
    'id' => 3, 
    'name' => 'c', 
    'cat' => '2', 
), 


) 

這是可能的,只需修改查詢?

編輯:繼承人功能的get()

public function get() 
{ 
    // Start building the query 
    $sql = 'SELECT id, name, url, cat, closed, date, votes_up, votes_down, '; 
    $sql .= 'votes_up - votes_down AS votes_balance, '; 
    $sql .= 'votes_up + votes_down AS votes_total, '; 
    $sql .= 'votes_up/(votes_up + votes_down) * 100 AS votes_pct_up, '; 
    $sql .= 'votes_down/(votes_up + votes_down) * 100 AS votes_pct_down '; 
    $sql .= 'FROM '.ThumbsUp::config('database_table_prefix').'items '; 

    // Select only either open or closed items 
    if ($this->closed !== NULL) 
    { 
     $where[] = 'closed = '.(int) $this->closed; 
    } 

    // Select only either open or closed items 
    if ($this->name !== NULL) 
    { 
     // Note: substr() is used to chop off the wrapping quotes 
     $where[] = 'name LIKE "%'.substr(ThumbsUp::db()->quote($this->name), 1, -1).'%"'; 
    } 

    // Append all query conditions if any 
    if (! empty($where)) 
    { 
     $sql .= ' WHERE '.implode(' AND ', $where); 
    } 

    // We need to order the results 
    if ($this->orderby) 
    { 
     $sql .= ' ORDER BY '.$this->orderby; 
    } 
    else 
    { 
     // Default order 
     $sql .= ' ORDER BY name '; 
    } 
    // A limit has been set 
    if ($this->limit) 
    { 
     $sql .= ' LIMIT '.(int) $this->limit; 
    } 

    // Wrap this in an try/catch block just in case something goes wrong 
    try 
    { 
     // Execute the query 
     $sth = ThumbsUp::db()->prepare($sql); 
     $sth->execute(array($this->name)); 
    } 
    catch (PDOException $e) 
    { 
     // Rethrow the exception in debug mode 
     if (ThumbsUp::config('debug')) 
      throw $e; 

     // Otherwise, fail silently and just return an empty item array 
     return array(); 
    } 

    // Initialize the items array that will be returned 
    $items = array(); 

    // Fetch all results 
    while ($row = $sth->fetch(PDO::FETCH_OBJ)) 
    { 
     // Return an item_id => item_name array 
     $items[] = array(
      'id' => (int) $row->id, 
      'name' => $row->name, 
      'url' => $row->url, 
      'cat' => $row->cat, 
      'closed' => (bool) $row->closed, 
      'date' => (int) $row->date, 
      'votes_up' => (int) $row->votes_up, 
      'votes_down' => (int) $row->votes_down, 
      'votes_pct_up' => (float) $row->votes_pct_up, 
      'votes_pct_down' => (float) $row->votes_pct_down, 
      'votes_balance' => (int) $row->votes_balance, 
      'votes_total' => (int) $row->votes_total, 
     ); 
    } 

    return $items; 
} 

感謝。

+1

什麼 '查詢'?你能詳細說一下那部分嗎? – Niko

+0

我的意思是這句話 <?php $ items = ThumbsUp :: items() - > get()?> – user1949940

+1

功能'get'在問題中未定義。引用腳本'ThumbsUp'在問題中未定義。嗯..爲什麼腳本語言說未定義,應該沒有定義。未定義的建議表明它已被定義,現在尚未定義。 /放在 – Popnoodles

回答

4

您可以輕鬆地修改 「獲得()」 中增加所需的功能:

public function get($cat = null) 
{ 
    $where = array(); 
    if ($cat !== null) { 
     $where[] = 'cat = '. (int) $cat; 
    } 

    // ... original code ... 
} 

用法:

$items = ThumbsUp::items()->get(1); 
相關問題