将foreach的值从控制器传递到视图

时间:2015-08-20 04:03:00

标签: php cakephp foreach

我正在尝试将foreach值从控制器传递到视图,但我只收到一个空数组。在下面基于foreach中的值的代码中,我执行了一个查询,我想设置值,以便我可以在视图中使用它。

foreach($logresponse as $row1){
    $customerid = $row1['customerid'];
    $topicid = $row1['topicid'];
    $todolistid = $row1['todolistid'];
    $sqlQry="select leadid,customerid as leadcustid,formname from lead_frontend where customerid='$customerid' order by leadid DESC limit 1";
    $leadInfo[] = $this->execute_flatten($sqlQry);
    if($topicid == 1){
        $query2="SELECT a.*,b.username,b.user_id,b.group_id,b.email as salesmanemail,d.email,d.phone1,e.subject,e.subject1,e.content,e.embed_template FROM todolist_frontend as a LEFT JOIN users_backend as b ON a.salesmanid=b.user_id LEFT JOIN customer as c ON c.customerid=a.customerid LEFT JOIN customer_address as d ON d.customerid=a.customerid LEFT JOIN admin_email_action as e ON a.combination_id=e.combination_id where a.todolistid='$todolistid'";
    }
    else if($topicid == 2){
        $query2="SELECT a.*,b.username,b.user_id,b.group_id,b.email as salesmanemail,d.email,d.phone1,e.subject,e.subject1,e.call_info as content FROM todolist_frontend as a INNER JOIN users_backend as b ON a.salesmanid=b.user_id INNER JOIN customer as c ON c.customerid=a.customerid INNER JOIN customer_address as d ON d.customerid=a.customerid INNER JOIN admin_call_action as e ON a.combination_id=e.combination_id where a.todolistid='$todolistid'";
    }
    else if($topicid == 18){
        $query2="SELECT a.*,b.username,b.user_id,b.group_id,b.email as salesmanemail,d.email,d.phone1,e.subject,e.subject1,e.call_info as content FROM todolist_frontend as a INNER JOIN users_backend as b ON a.salesmanid=b.user_id INNER JOIN customer as c ON c.customerid=a.customerid INNER JOIN customer_address as d ON d.customerid=a.customerid INNER JOIN admin_managercall_action as e ON a.combination_id=e.combination_id where a.todolistid='$todolistid'";
    }
    else{
        $query2="SELECT a.*,b.username,b.user_id,b.group_id,b.email as salesmanemail,d.email,d.phone1,e.content,e.subject1,f.emailname as subject,g.mailername AS mailersubject FROM todolist_frontend as a LEFT JOIN users_backend as b ON a.salesmanid=b.user_id LEFT JOIN customer as c ON c.customerid=a.customerid LEFT JOIN customer_address as d ON d.customerid=a.customerid LEFT JOIN admin_mailer_action as e ON a.combination_id=e.combination_id LEFT JOIN emailtype_backend as f ON e.subject=f.emailid LEFT JOIN mailer_backend AS g ON e.subject = g.mailerid where a.todolistid='$todolistid'";
    }
    $result = $this->execute_flatten($query2);

    $subjecttodo = array();
    foreach ($result as $res1){
        if($res1['topicid'] == 1 && !empty($res1['subject'])){
            $subject1 = $res1['subject'];
            $sub_qry="select emailname from emailtype_backend where emailid=$subject1";
            $subject_result=$this->execute_flatten($sub_qry);
            $subjecttodo=isset($subject_result['0']['emailname']) ? $subject_result['0']['emailname'] : "";
        }
        if($res1['topicid'] == 2 || $res1['topicid'] == 18){
            $subjecttodo=$res1['subject'];
        }
        if($res1['topicid'] == 8){
            $subjecttodo=$res1['mailersubject'];
        }
    $this->set('subjecttodo', $subjecttodo);   
    }
    $this->set('leadInfo', $leadInfo);
}

1 个答案:

答案 0 :(得分:2)

首先检查您是否在foreach循环中获取变量$subjecttodo$leadInfo的值。然后将这些值分配给Array。

 $subjecttodoArray[] = $subjecttodo; 
 $leadInfoArray[] = $leadInfo;     

之后在Controller函数中尝试像你一样传递这个数组

$this->set('subjecttodo', $subjecttodoArray); 
$this->set('leadInfo', $leadInfoArray);

然后尝试在视图中打印数组值,如此

echo "<pre>";
print_r($subjecttodo);
echo "</pre>";

echo "<pre>";
print_r($subjecttodo);
echo "</pre>";

希望这有帮助。