2013-03-14 76 views
0

我仍然試圖圍繞將db查詢結果從模型傳遞迴控制器並最終傳遞到視圖來包裝頭部。我似乎正在將數據傳送到正確的位置,但我不確定如何最佳地訪問視圖中所得到的對象數組。Codeigniter:引用模型結果中返回的特定數組索引

具體而言,我試圖查詢我的數據庫的最近7個不同的日期,有人提交了一個鏈接。我得到一個日期數組,然後對於每個日期,我會對該日期提交的所有鏈接進行查詢,並將結果存儲在一個數組中。然後在視圖中,對於每個不同的日期,我都會顯示一個標題(日期),緊跟其後的是相關聯的鏈接。

是來自我的不同的日期查詢($ link_headers)數組:

Array (
    [0] => stdClass Object([added_date] => 2011-08-11) 
    [1] => stdClass Object([added_date] => 2011-05-03) 
    [2] => stdClass Object([added_date] => 2011-04-21) 
    [3] => stdClass Object([added_date] => 2011-04-10) 
    [4] => stdClass Object([added_date] => 2011-03-04) 
    [5] => stdClass Object([added_date] => 2011-02-28) 
    [6] => stdClass Object([added_date] => 2011-02-22) 

) 

是來自我的查詢提交實際鏈接($ links_result)數組:

Array 
(
    [0] => Array 
     (
      [0] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1178 
        [link_url] => http://www.amazon.com/Silicone-Rubber-CASSETTE-Design-IPHONE/dp/B004YDJWOY 
        [link_name] => Silicone Skin BLACK CASSETTE TAPE 
        [link_notes] => iPhone case... probably won't fit in my dock. 
        [added_date] => 2011-08-11 
        [flag_new] => 1 
        [rating] => 4 
        [public] => 1 
       ) 

     ) 

    [1] => Array 
     (
      [0] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1177 
        [link_url] => http://snorby.org/ 
        [link_name] => Snorby - Snort front-end 
        [link_notes] => 
        [added_date] => 2011-05-03 
        [flag_new] => 1 
        [rating] => 4 
        [public] => 1 
       ) 

     ) 

    [2] => Array 
     (
      [0] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1176 
        [link_url] => http://www.nytimes.com/2011/04/17/business/17excerpt.html?_r=4&pagewanted=1&ref=business 
        [link_name] => Corner Office - The 5 Habits of Highly Effective C.E.O.s 
        [link_notes] => Sounds a lot like what Nathanial said... 
        [added_date] => 2011-04-21 
        [flag_new] => 1 
        [rating] => 4 
        [public] => 1 
       ) 

     ) 

    [3] => Array 
     (
      [0] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1175 
        [link_url] => http://chezlarsson.com/myblog/2010/06/panduro-concrete-challenge-3.html 
        [link_name] => Concrete book-ends 
        [link_notes] => Cool look... 
        [added_date] => 2011-04-10 
        [flag_new] => 1 
        [rating] => 4 
        [public] => 1 
       ) 

      [1] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1174 
        [link_url] => http://themeforest.net/item/reciprocity-photo-blog-gallery/154590 
        [link_name] => Site Templates - Reciprocity - Photo Blog 
        [link_notes] => 
        [added_date] => 2011-04-10 
        [flag_new] => 1 
        [rating] => 5 
        [public] => 1 
       ) 

     ) 

    [4] => Array 
     (
      [0] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1173 
        [link_url] => http://lifehacker.com/#!5771943/the-always-up+to+date-guide-to-jailbreaking-your-ios-device 
        [link_name] => The Always Up-to-Date Guide to Jailbreaking Your iOS Device 
        [link_notes] => 
        [added_date] => 2011-03-04 
        [flag_new] => 1 
        [rating] => 4 
        [public] => 1 
       ) 

     ) 

    [5] => Array 
     (
      [0] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1172 
        [link_url] => http://lifehacker.com/#!5754463/how-to-jailbreak-your-ios-421-device 
        [link_name] => How to Jailbreak Your iOS 4.2.1 Device 
        [link_notes] => 
        [added_date] => 2011-02-28 
        [flag_new] => 1 
        [rating] => 4 
        [public] => 1 
       ) 

     ) 

    [6] => Array 
     (
      [0] => stdClass Object 
       (
        [user_id] => 2 
        [link_id] => 1171 
        [link_url] => http://www.bitplumber.net/2010/10/a-cassandra-hardware-stack-dell-c1100s-ocz-vertex-2-ssds-with-sandforce-arista-7048s/ 
        [link_name] => A Cassandra Hardware Stack 
        [link_notes] => 
        [added_date] => 2011-02-22 
        [flag_new] => 1 
        [rating] => 3 
        [public] => 1 
       ) 

     ) 

) 

..一切似乎都不錯。但我的問題來自我的觀點,我正在試圖按照上面所述構建HTML。我試圖去工作的代碼的簡化圖如下:

foreach ($link_headers as $header) { 

echo "INDEX: ". $links_headers .", ADDED DATE: ". $header->added_date ."<BR>"; 

    foreach ($links_result[$link_headers] as $result){ 
     echo $result->added_date ."<BR>"; 
     echo $result->link_name ."<BR><BR>"; 
    } 
} 

所以,我嘗試使用的第一個索引告訴我的foreach循環的第二陣列的該指數循環播放並獲取內容。很明顯,我濫用$ links_result [$ link_headers]變量,但是我把它留給了我想要做的事情。

任何幫助非常感謝!

邁克爾

回答

0

我不使用笨,但我是否內個框架或PHP只想抓住一切一氣呵成,然後用該指標的問題變得毫無意義:

SELECT * FROM model_table mt WHERE mt.added_date IN (
    SELECT DISTINCT md.added_date from model_table md 
    ORDER BY md.added_date DESC 
    LIMIT 7 
) ORDER BY mt.added_date DESC 

這應該讓您將按日期排序的一組模型限制爲最近的7個日期。那麼它只是選擇何時顯示標題的問題:

$current = null; 
foreach($links as $link) { 
    if($link->added_date !== $current) { 
     // show the header and set current to the current date 
     $current = $link->added_date; 
     echo 'HEADER: Added on ' . $current . '<br />'; 
    } 

    echo $row->added_date ."<BR>"; 
    echo $row->link_name ."<BR><BR>"; 
} 
相關問題