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

时间:2013-03-14 19:31:16

标签: php codeigniter multidimensional-array

我仍然试图将模型中的数据库查询结果传递回控制器,最后传递给视图。我似乎把数据放到了正确的位置,我只是不确定如何最好地访问视图中生成的对象数组。

具体来说,我正在尝试查询我的数据库,查看有人提交链接的最近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]变量,但是我把它留下来展示我想要做的事情。

非常感谢任何帮助!

迈克尔

1 个答案:

答案 0 :(得分:0)

我不使用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>";
}