foreach中的php foreach

Posted

技术标签:

【中文标题】foreach中的php foreach【英文标题】:php foreach within foreach 【发布时间】:2012-01-27 12:28:40 【问题描述】:

我想要实现的是能够像这样打印结果:

Header 1
    Link 1
    Link 2
Header 2
    Link 3
Header 3
    Link 4
    Link 5

但是下面的代码打印的结果是这样的:

Header 1
    Link 1
    Link 2
Header 2
    Link 1
    Link 2
    Link 3
Header 3
    Link 1
    Link 2
    Link 3
    Link 4
    Link 5

使用php pdo wrapper class,据我所知,我别无选择,只能使用“foreach”而不是“while”来循环结果,这是我的代码:

$bind = array(":users_id" => $id);
$categoryQry = $db->select("category","users_id=:users_id", $bind);
//loop Headers
foreach($categoryQry AS $CatRes)
    $category_name = $CatRes['category_name'];

    $bind = array(
        ":users_id" => $id,
        ":link_category" => $CatRes['category_id']
    );
    $linkQry = $db->select("links", "users_id=:users_id AND link_category=:link_category", $bind);
    //loop Links under each header
    foreach($linkQry AS $LinkRes)
        $link_url = $LinkRes['link_url'];
        //$link_row outputs the links under each header, this is where the problem is        
        $link_row .= DisplayLayout("link_row.html","\$link_url");
    

    //$header outputs the result OK (3 headers as stated in above example)    
    $header .= DisplayLayout("header.html","\$category_name,\$link_row");

//note that DisplayLayout is my template function which is used for outputting variables using an HTML file.

我一直在尝试其他几种方法,但都没有成功,希望有人能在我的代码中发现问题并引导我走向正确的方向。

提前谢谢你,圣诞快乐。

【问题讨论】:

$link_row 只被添加到,你需要在每个外循环之后清空它 【参考方案1】:

您需要在此行之前重置 $link_row:foreach($linkQry AS $LinkRes)

【讨论】:

这是我忽略的逻辑方面。感谢您的指点,解决了我的问题。【参考方案2】:
$bind = array(":users_id" => $id);
$categoryQry = $db->select("category","users_id=:users_id", $bind);
//loop Headers
foreach($categoryQry AS $CatRes)
$category_name = $CatRes['category_name'];

$bind = array(
    ":users_id" => $id,
    ":link_category" => $CatRes['category_id']
);
$linkQry = $db->select("links", "users_id=:users_id AND link_category=:link_category", $bind);
//loop Links under each header
$link_row='';
foreach($linkQry AS $LinkRes)
    $link_url = $LinkRes['link_url'];
    //$link_row outputs the links under each header, this is where the problem is        
    $link_row .= DisplayLayout("link_row.html","\$link_url");


//$header outputs the result OK (3 headers as stated in above example)    
$header .= DisplayLayout("header.html","\$category_name,\$link_row");

//note that DisplayLayout is my template function which is used for outputting     variables using an HTML file.

【讨论】:

【参考方案3】:
//reset linkrow
$link_row = '';


foreach($linkQry AS $LinkRes)
    $link_url = $LinkRes['link_url'];
    //$link_row outputs the links under each header, this is where the problem is        
    $link_row .= DisplayLayout("link_row.html","\$link_url");

【讨论】:

【参考方案4】:

在第二个循环之前为空 $link_row

【讨论】:

以上是关于foreach中的php foreach的主要内容,如果未能解决你的问题,请参考以下文章

foreach循环php中的PDOStatement

foreach中的php foreach

foreach 相当于 jquery 中的 php?

php foreach循环中的变量

PHP中的foreach对象数组

php 获得foreach中的第一个元素