Codeigniter:引用模型结果中返回的特定数组索引



我仍然试图把我的头从一个模型返回到控制器,最后到一个视图传递db查询结果。我似乎得到的数据到正确的地方,我只是不确定如何最好地访问视图中对象的结果数组。

具体来说,我试图查询我的数据库最近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]变量,但我把它留在里面,以显示我想做什么。

任何帮助都非常感谢!

迈克尔

我不使用CodeIgniter,但无论是在框架内还是从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>";
}

相关内容

  • 没有找到相关文章

最新更新