dsdtumf776629385 2014-07-04 04:41
浏览 49
已采纳

PHP函数在HTML标记之前打印

I have quite the peculiar problem... I've got a working PHP function, as shown below, which fetches a users' score and outputs some information based on that score (it's not optimized yet).

Unfortunately, for whatever reason, when I put it in my code, it's not actually being rendered inside the HTML markup/position I want it to be rendered in - instead, it's being rendered at the top of the page, and a search has not yielded any results.

The code simplified is:

<?php
require('displayCodes.php');
displayCodesMain(1);
?>

The displayCodesMain code:

<?php
function displayCodesMain($page){ 
    if($page == 1){?>
<table class="friendcodes">
    <tr>
        <th>Username</th>
        <th>Friend Code</th>
        <th>Affinity</th>
        <th>Level</th>
        <th>Rating</th>
    </tr>
<?php
        $plisting = file_get_contents('plist.txt');
        $plist = explode(' ', $plisting, 4);
        require_once('vote.php');
        array_pop($plist);
        foreach($plist as $item) {
            $item = explode(':', $item);
            echo '<tr class="fc_premium">';
            $item[3] = substr($item[3], 0, 3) . '-' . substr($item[3], 3, 7);
            $item[1] = str_replace('&', ' ', $item[1]);
            echo '<td>' . $item[1] . '</td><td>' . $item[3] . '</td><td style="text-transform:capitalize;">' . $item[4] . '</td><td>' . $item[5] . '</td><td> '. displayRating(1)
.' </td>';
            echo '</tr>'; 
        }    
        echo '</table>';
    }
} // End of the function
?>

The actual function itself:

<?php
function displayRating($id){
    if(isset($id)){
        require_once('medoo.min.php');
        // Start up the D.B. Connection
        // Removed some DB setup code here...
        $phrase = '<div class="bc"><div data-hint="%1$s" class="hint--right vote %2$s"></div></div>';
        if($vScore == 0){
            printf($phrase, 'Neutral Score', 'v_equal');
            return;
        } elseif ($vScore > 0){
            // Positive Here
            switch($vScore){
                case 1:
                    printf($phrase, '1 Like', 'v_p1 hint--success');
                    break;
                case 2:
                    printf($phrase, '2 Likes', 'v_p2 hint--success');
                    break;
                case 3:
                    printf($phrase, '3 Likes', 'v_p3 hint--success');
                    break;
                case 4:
                    printf($phrase, '4 Likes', 'v_p4 hint--success');
                    break;
                case 5:
                    printf($phrase, '5 Likes', 'v_p5 hint--success');
                    break;
                case 6:
                    printf($phrase, '6 Likes', 'v_p6 hint--success');
                    break;
                case 7:
                    printf($phrase, '7 Likes', 'v_p7 hint--success');
                    break;
                default:
                    printf($phrase, $vScore . ' Likes', 'v_p7 hint-success');
                    break;
            }
        } elseif ($vScore < 0){
            // Negative Here
            switch($vScore){
                case -1:
                    printf($phrase, '1 Dislike', 'v_m1 hint--error');
                    break;
                default:
                    if($vScore < -7){ $vClass = 7; } else { $vClass = abs($vScore); }
                    printf($phrase, abs($vScore) . ' Dislikes', 'v_m' . $vClass . ' hint--error');
            }
        }
    } else {
        return;
    }
}
?>

I've even put in a static variable for the function in the first foreach statement, yet, I still have no luck in getting it to display in the proper place.

I am currently unable to gain access to my php.ini, and phpinfo() appears to be disabled for security reasons. I will get back to you guys after I find out my info from my host.

  • 写回答

1条回答 默认 最新

  • dqzpt40064 2014-07-04 05:12
    关注

    In your main program, you're using echo to output the return value of displayRating(), but it doesn't return anything. Instead, displayRating() is also using echo. This is why your output appears out of order.

    Turn this:

    echo /* ... */ '</td><td> '. displayRating(1) .' </td>';
    

    into:

    echo /* ... */ '</td><td> ';
    displayRating(1);
    echo ' </td>';
    

    or build and return a string from displayRating().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度