Bradly Spicer Bradly Spicer - 6 months ago 16
SQL Question

SQL Query PHP, populate content quickly

Not sure I worded the title correctly, I'm unsure of what this is called.

I have a while loop which is populated by an SQL query

while($row = $result->fetch_assoc()){
$Tchannel = $row['chan'];
echo '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_'.$Tchannel.'-320x180.jpg" alt="Thumbnail"><br />';
echo '<p>';
}


My problem is that when the page loads it does it in sections. I've seen other websites populate these straight away on page load. How would I replicate something like this?

See Example: http://puu.sh/oTh6v/73446c0c15.jpg

Answer

Could this be what you meant to achieve...? And by the way, the opening Paragraph Tag:< p > could contribute to some unexpected &/or sluggish behaviour since it was just opened without being closed anywhere in your code... I commented it out... You may try to see if it helps....

    <?php
        // CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
        $strOutput      = "";
        while($row = $result->fetch_assoc()){
            $Tchannel = $row['chan'];
            $strOutput .=  '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';

            // WHY DO YOU NEED THIS OPENING PARAGRAPH TAG? WHERE IS IT CLOSED IN YOUR CODE?
            //echo '<p>';
        }
        // RENDER THE OUTPUT 
        echo $strOutput;

    ?

If you need the images wrapped in paragraph tags, you can do it differently:

    <?php
        // CREATE A STRING VARIABLE TO HOLD THE ENTIRE OUTPUT TILL YOU ARE READY TO RENDER IT TO THE STREAM...
        $strOutput      = "";
        while($row = $result->fetch_assoc()){
            $Tchannel = $row['chan'];
            $strOutput .=  '<p>';     // <== OPEN A PARAGRAPH
            $strOutput .=  '<img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br />';
            $strOutput .= '</p>';     // <== CLOSE THE PARAGRAPH

            // OR ALL IN ONE LINE:                
            // $strOutput .=  '<p><img src="https://static-cdn.jtvnw.net/previews-ttv/live_user_' . $Tchannel . '-320x180.jpg" alt="Thumbnail"><br /></p>';
        }
        // RENDER THE OUTPUT 
        echo $strOutput;

    ?>
Comments