-2

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

Bradly Spicer
  • 2,278
  • 5
  • 24
  • 34
  • I am not sure if you mean this but instead echoing every single row in the loop, create the string variable with the content first then echo all at once. – smozgur May 15 '16 at 23:17
  • do you possibly have a slow internet connect? from my understanding, html is sent out in sequence, so you will always be sending the first part first and the last part last, and if your internet is slow, you might see some of it form before the other arrives. I could be wrong, though what you could do is ask a friend to go on your site and see if it loads the same way for him. – Webeng May 15 '16 at 23:27
  • Nope, as far as I know. I'm doing it wrong, in theory I shouldn't be producing it row by row? Example being this page: http://collectiongamingnetwork.com/streams – Bradly Spicer May 15 '16 at 23:30
  • see this for preloading images: stackoverflow.com/questions/476679/… then create your javascipt with php. if you are not using jquery then make a search for "preload images with javascript" or even better "preload images with javascript and php". if you are looking for page design then use "div" or "table" design. – smozgur May 15 '16 at 23:30
  • Thanks, I'll give it a go – Bradly Spicer May 15 '16 at 23:31
  • why on earth my answer was downvoted for a wrong tagged and wrongly asked question! – smozgur May 15 '16 at 23:32
  • @smozgur no idea; wasn't me anyway. – Funk Forty Niner May 15 '16 at 23:32
  • I actually even deleted it after figuring it out. anyway. no more soup for you wrong taggers! (quote from Seinfeld :) ) – smozgur May 15 '16 at 23:33
  • @BradlySpicer It is well necessary that you closed the paragraph you opened... it sure costs something if you don't close or delete it. The Browser would work so hard trying to figure out how to rectify that for you... at the cost of performance...and thus the expected faster loading would be compromised. – Poiz May 16 '16 at 00:19

1 Answers1

1

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;

    ?>
Poiz
  • 7,611
  • 2
  • 15
  • 17