mvc中循环遍历分配的代码

控制器:

$infos = $this->db->listinfo(array('siteid'=>$this->get_siteid(),'themeid'=>$themeid),'subjectid ASC',$page, '14');
        $newarr = array();
        foreach ($infos as $value)
        {
            $t1=$value['subject'];
            $subjectid = intval($value[subjectid]);
            $newarr[$subjectid]['title'] = $t1;
            if(!$subjectid){
                showmessage(L('illegal_operation'));
            }
            //$show_validator = $show_scroll = $show_header = true;
             //获取投票信息
            $sdb = pc_base::load_model('vote_data_model'); //加载投票统计的数据模型
            $subject_infos = $sdb->select("subjectid = $subjectid",'data');    
              //新建一数组用来存新组合数据
            $total = 0;
            $vote_data =array();
            $vote_data['total'] = 0 ;//所有投票选项总数
            $vote_data['votes'] = 0 ;//投票人数
            //循环每个会员的投票记录
            foreach($subject_infos as $subjectid_arr) {
                    extract($subjectid_arr);
                     $arr = string2array($data);
                     foreach($arr as $key => $values){
                         $vote_data[$key]+=1;
                    }
                      $total += array_sum($arr);
                    $vote_data['votes']++ ;
            }
             $vote_data['total'] = $total ;
             //取投票选项
            $options = $this->db2->get_options($subjectid);
            foreach($options as $v2){
                //echo $v2[option],"@@";
                $newarr[$subjectid]['option'][] = $v2['option'];
                $newarr[$subjectid]['nums'][] = $vote_data[$v2['optionid']];
                //echo $vote_data[$v2['optionid']],"<br>";
            }
        }


视图:

<?php
if(is_array($newarr)){
    $i=1;
    foreach($newarr as $info){
        ?>
    <tr>
        <th style="text-align:left;padding:5px 30px;border-bottom:1px dashed;"><?php echo $i; ?>,<?php echo $info['title'];?><br />
            <span>
                <?php
                        foreach ($info['option'] as $k=>$info2){
                            $info['nums'][$k] = $info['nums'][$k]?$info['nums'][$k]:0;
                            echo $info2.':'.$info['nums'][$k].'<br>';
                        }
                ?>
            </span>
        </th>
    </tr>
    <?php
    $i++;
    }
}
?>

原文地址:https://www.cnblogs.com/jami918/p/3078160.html