0

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.

5
  • 1
    This may have something to do with output buffering. I had a similar problem (though it was in WordPress, it was doing the same thing). I got an answer here: stackoverflow.com/questions/22467912/… , which suggested some edits for .htaccess and php.ini . Let me know if that helps at all. Commented Jul 4, 2014 at 4:48
  • Is the table what you want to have displayed below your <h3>? Commented Jul 4, 2014 at 4:48
  • FWIW - looking at your test page - your code is generating broken HTML. It's creating <div> elements in the <tr> when, I guess, they should be in the <td> elements. Commented Jul 4, 2014 at 4:51
  • The table is not below the <h3>, However the circles should be appearing in the last column of the table row, but they are not. Commented Jul 4, 2014 at 5:01
  • Should I be 'returning' the values from my function in some other manner? This might explain the incredibly odd manner that it's acting in... Commented Jul 4, 2014 at 5:08

1 Answer 1

1

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.