【问题标题】:Not able to embed $data for JSON response from Controller to View无法为从控制器到视图的 JSON 响应嵌入 $data
【发布时间】:2019-11-26 07:29:57
【问题描述】:

我正在尝试使用 Json_encode 将 $data 从控制器传输到视图。在 For 循环中面临问题,我无法使用 HTML 格式正确嵌入(“'..'”)它。请帮我解决这个问题。

谢谢! :)

$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>";
   } .'

【问题讨论】:

    标签: php json codeigniter


    【解决方案1】:

    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

    【讨论】:

    • @PranavChavan,不客气。如果它完全解决了你的问题,你可以接受这个答案。
    • @PranavChavan,不,你没有
    猜你喜欢
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2016-11-05
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2012-07-26
    相关资源
    最近更新 更多