HTML出现之前我在PHP中调用它

时间:2017-05-19 18:57:27

标签: php html

我有以下代码

function displayTotalRequestType() {
    echo '<div>';
    echo '  <ul class="nav nav-tabs" role="tablist">';
    echo '    <li role="presentation" class="active"><a href="#today" aria-controls="today" role="tab" data-toggle="tab">Today</a></li> ';
    echo '    <li role="presentation"><a href="#week" aria-controls="week" role="tab" data-toggle="tab">This Week</a></li>';
    echo '    <li role="presentation"><a href="#month" aria-controls="month" role="tab" data-toggle="tab">This Month</a></li>';
    echo '  </ul>';
    echo '  <div class="tab-content">';
    echo '    <div role="tabpanel" class="tab-pane active" id="today">'.$this->CI->model('custom/Dashboard')->displayTotalRequestTypeTable(date('Y-m-d')).'</div>';
    echo '    <div role="tabpanel" class="tab-pane" id="week">'.$this->CI->model('custom/Dashboard')->displayTotalRequestTypeTable(date("Y-m-d",strtotime('monday this week'))).'</div>';
    echo '    <div role="tabpanel" class="tab-pane" id="month">'.$this->CI->model('custom/Dashboard')->displayTotalRequestTypeTable(date('Y-m-01')).'</div>';
    echo '  </div>';
    echo '</div>';
}

所以我在这个($this->CI->model('custom/Dashboard')->displayTotalRequestTypeTable())内调用的函数根据我传入的数据显示一个表

但是,当页面呈现时,生成的HTML会显示displayTotalRequestTypeTable()的输出实际上在<div role="tabpanel"...之前开始,而不是在我预期的标签内

有什么原因吗?如何解决?

1 个答案:

答案 0 :(得分:1)

由于您在函数中echo,因此在调用函数时会显示输出。要控制输出的位置,请将调用分配给变量,然后将变量输出到所需的位置;或echo displayTotalRequestType();。 e.g。

function displayTotalRequestType() {
    return '<div>'.
    '  <ul class="nav nav-tabs" role="tablist">'.
    '    <li role="presentation" class="active"><a href="#today" aria-controls="today" role="tab" data-toggle="tab">Today</a></li> '.
    '   <li role="presentation"><a href="#week" aria-controls="week" role="tab" data-toggle="tab">This Week</a></li>'.
    '    <li role="presentation"><a href="#month" aria-controls="month" role="tab" data-toggle="tab">This Month</a></li>'.
    '  </ul>'.
    '  <div class="tab-content">'.
    '    <div role="tabpanel" class="tab-pane active" id="today">'.$this->CI->model('custom/Dashboard')->displayTotalRequestTypeTable(date('Y-m-d')).'</div>'.
     '    <div role="tabpanel" class="tab-pane" id="week">'.$this->CI->model('custom/Dashboard')->displayTotalRequestTypeTable(date("Y-m-d",strtotime('monday this week'))).'</div>'.
    '    <div role="tabpanel" class="tab-pane" id="month">'.$this->CI->model('custom/Dashboard')->displayTotalRequestTypeTable(date('Y-m-01')).'</div>'.
    '  </div>'.
    '</div>';
}

然后在你的代码中,

<?php
$requesttype = displayTotalRequestType();
.... //later echo $requesttype;

 echo displayTotalRequestType();