无法嵌入$ data以便从Controller到View的JSON响应

时间:2019-11-26 07:29:57

标签: php json codeigniter

我正在尝试使用Json_encode将$ data从控制器传输到View。我无法以HTML格式正确嵌入(“'。。'”)的For循环问题。请帮助我。

谢谢! :)

$data ='
   <div class="col-lg-12">
       <div class="ibox">
           <div class="ibox-title">
                <h5>Leave Table </h5>
           </div>
       <div class="ibox-content">
       <table class="table table-bordered" id="changeBlock">
           <thead>
                <tr>
                    <th>Agents</th>
                        '. for($i = 1; $i <= $days; $i++){
                            echo "<th class='w-30'>'.$i.'</th>";
                        } .'
                </tr>
            </thead>
            <tbody> 
                '.$cnt = count($agent_list); if(isset($agent_list) && ($agent_list != '')){ foreach($agent_list as $val) { $agent_id = $val['info']->ps_crm_agent_id;

                for($i = $cnt; $i <= $cnt ; $i++){
                    echo '<tr><td data-id="'.$agent_id.'">'.$val['info']->firstname.' '.$val['info']->lastname.'</td>';

                for($j = 1; $j <= $days; $j++){ 
                    if(!empty($val['leaves'])){
                        $key = array_search($j, array_column($val['leaves'], 'leave_date'));
                        if(is_numeric($key)){
                            echo '<td class="cell_style w-30 pointer" data-id-leaves= '.$val['leaves'][$key]['id_leaves'].' data-id='.$agent_id.' data-date="'.$j.'" style="background-color: '.$val['leaves'][$key]['color_code'].'"> </td>';
                        }else{
                            echo '<td class="cell_style w-30 pointer" data-id-leaves="0" data-id='.$agent_id.' data-date="'.$j.'"> </td>';
                        }
                    }else{  
                        echo '<td class="cell_style w-30 pointer" data-id-leaves="0" data-id='.$agent_id.' data-date="'.$j.'"> </td>';
                    }
                }
            echo '</tr>';
        } } }'
        </tbody>
      </table>
    </div>
 </div>
</div>';

   $data = $this->load->view('load_leave_calender', $data, TRUE);

**面对问题**

   '. for($i = 1; $i <= $days; $i++){
      echo "<th class='w-30'>'.$i.'</th>";
   } .'

1 个答案:

答案 0 :(得分:0)

for循环放置在以下位置:

$part = '';
for($i = 1; $i <= $days; $i++){
    $part .= '<th class="w-30">'.$i.'</th>';
}

然后,您可以将其串联在$data字符串中:

$data ='
   <div class="col-lg-12">
       <div class="ibox">
           <div class="ibox-title">
                <h5>Leave Table </h5>
           </div>
       <div class="ibox-content">
       <table class="table table-bordered" id="changeBlock">
           <thead>
                <tr>
                    <th>Agents</th>
                        '. $part .'
                </tr>
            </thead>';

Demo