dscrn1974
2018-05-20 02:09 阅读 49
已采纳

如何使用foreach()插入HTML而不使用echo

I have this working code in my view:

<?php
                $task_num = 0;
                foreach ($curent_day->getTasksList() as $task){

                    echo '<div class="task">';
                        echo '<span class="task_id">'.($task_num+1).'.'.'</span>';
                        echo '<div class="task_time">';
                            echo '<span class="task_time_start">'.$task->getStartTime().'</span>';
                            echo '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
                        echo '</div>';
                        echo '<a href="/" class="task_name">'.$task->name.'</a>';
                        echo '<a href="/" class="btn task_start btn_disabled">Start</a>';
                        echo '<a href="/" class="btn task_finish btn_disabled">Finish</a>';
                        echo '<div class="status_round '.$task->status.'"></div>';
                    echo '</div>';

                $task_num++;

                }
?>

Is there any way to get rid of 'echo'? P.S. and is the way to insert HTML with HTML helper more correct even if it takes more space?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    duanqinjiao5244 duanqinjiao5244 2018-05-20 09:07

    You can drop echo completely and use native HTML syntax:

    <?php $task_num = 0 ?>
    <?php foreach ($curent_day->getTasksList() as $task): ?>
        <div class='task'>
            <span class='task_id'><?= ++$task_num ?></span>
            <div class='task_time'>
                <span class='task_time_start'><?= $task->getStartTime() ?></span>
                <span class='task_time_finish'><?= $task->getFinishTime() ?></span>
            </div>
            <a href='/' class='task_name'><?= $task->name ?></a>
            <a href='/' class='btn task_start btn_disabled'>Start</a>
            <a href='/' class='btn task_finish btn_disabled'>Finish</a>
            <div class='status_round <?= $task->status ?>'></div>
        </div>
    <?php endforeach ?>
    

    This will give you better syntax highlighting and autoformatting support in your IDE/editor.

    点赞 评论 复制链接分享
  • doudun1934 doudun1934 2018-05-20 02:15

    There are several different ways that you can concatenate the string by using single quotes and double quotes, or by using a variable and the ".=" operator to append text onto the end of a string. Just google php strings & concatenation and it will have more than you need to figure this out.

    But using you example here is a method:

    $task_num = 0;
    foreach ($curent_day->getTasksList() as $task){
    echo 
    '<div class="task">' .
      '<span class="task_id">' . ($task_num+1) . '.'.'</span>' .
        '<div class="task_time">' .
        '<span class="task_time_start">' . $task->getStartTime() . '</span>' .
        '<span class="task_time_finish">' . $task->getFinishTime() . '</span>' .
        '</div>' . 
      '<a href="/" class="task_name">'.$task->name.'</a>' . 
      '<a href="/" class="btn task_start btn_disabled">Start</a>' . 
      '<a href="/" class="btn task_finish btn_disabled">Finish</a>' . 
      '<div class="status_round '.$task->status.'"></div>' .
    '</div>';
    
    $task_num++;
    
    }
    

    You have to echo to output you data to the browser.

    点赞 评论 复制链接分享
  • dongsheng4126335 dongsheng4126335 2018-05-20 06:54

    You need not use string concatenation or multiple echo statements.

    Alternative

    $task_num = 0;
      foreach ($curent_day->getTasksList() as $task){
        $task_num++;
        echo
          "<div class='task'>
            <span class='task_id'>{$task_num}</span>
              <div class='task_time'>
              <span class='task_time_start'>{$task->getStartTime()}</span>
              <span class='task_time_finish'>{$task->getFinishTime()}</span>
              </div>
            <a href='/' class='task_name'>{$task->name}</a>
            <a href='/' class='btn task_start btn_disabled'>Start</a>
            <a href='/' class='btn task_finish btn_disabled'>Finish</a>
            <div class='status_round {$task->status}'></div>
          </div>";
    
        }
    
    点赞 评论 复制链接分享
  • douhanshu5517 douhanshu5517 2018-05-20 09:58

    You can concatenate your html in one variable and then you can use it with one time echo statement

    <?php
                $task_num = 0;
                $html = '';
                foreach ($curent_day->getTasksList() as $task){
                    $html .= '<div class="task">';
                    $html .= '<span class="task_id">'.($task_num+1).'.'.'</span>';
                    $html .= '<div class="task_time">';
                    $html .= '<span class="task_time_start">'.$task->getStartTime().'</span>';
                    $html .= '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
                     $html .= '</div>';
                     $html .= '<a href="/" class="task_name">'.$task->name.'</a>';
                     $html .=  '<a href="/" class="btn task_start btn_disabled">Start</a>';
                     $html .= '<a href="/" class="btn task_finish btn_disabled">Finish</a>';
                     $html .= '<div class="status_round '.$task->status.'"></div>';
                    $html .=  '</div>';
    
                $task_num++;
    
                }?>
    
    点赞 评论 复制链接分享

相关推荐