I am trying to make it so that the snippet below automatically creates extra pages to show me all the followers I have on my Twitch Channel. At the moment I have it at 25, but it does cap at 100. How can I make it so that it automatically populates the table?

<?php  
    $json  = json_decode(file_get_contents("https://api.twitch.tv/kraken/channels/greatbritishbg/follows?limit=25"));

    $i = 0;
    // Use http://json.parser.online.fr/ to get the string parse of the Json_Decode

        echo "<table><tr>";
        echo "<th>Username:</th>";
        echo "<th>Follow Date:</th>";
        echo "<th>Type:</th>";
        echo "<th> </th>";  
        echo "</tr>";

        foreach($json->follows as $follow) { 
        $i++;
        echo "<tr>";
        echo "<td><a href='" . $follow->user->_links->self . "'>" . $follow->user->name . " (".$i. ")</td>";
        echo "<td>" . $follow->user->created_at . "</td>";
        echo "<td>" . $follow->user->type . "</td>";
        echo "</tr>";
    } 

        echo "</table>";
    //$follow = grabbing the json object for follows > user > name. 
?>

Here's a stab at it with a previous and next button. There are probably pagination 'widgets' out there, and you could easily replace my hard coded JS with some jQuery. An alternate approach without JS would involve passing a variable and reloading the page.

<?php
$json = json_decode(file_get_contents("https://api.twitch.tv/kraken/channels/greatbritishbg/follows?limit=25"));

$currentFollower = 0;
$currentPage     = 0;
$resultsPerPage  = 5;

// Use http://json.parser.online.fr/ to get the string parse of the Json_Decode

$tableHtml = <<<TABLE
<div id="page-number-%s" style="%s">
    <table>
        <tr>
            <th>Username:</th>
            <th>Follow Date:</th>
            <th>Type:</th>
        </tr>
        %s  
    </table>
</div>
TABLE;

$rowHtml = <<<ROW
<tr>
    <td><a href="%s">%s</a></td>
    <td>%s</td>
    <td>%s</td>
</tr>
ROW;

$html = "";
$rows = "";

foreach ($json->follows as $follow)
{
    if ($currentFollower % $resultsPerPage == 0 && $currentFollower> 0)
    {
        $style = $currentPage === 0 ? '' : 'display:none';
        $html .= sprintf($tableHtml, $currentPage, $style, $rows);
        $rows = "";
        $currentPage++;
    }   

    $rows .= sprintf(   $rowHtml, 
                        $follow->user->_links->self,  
                        $follow->user->name . ' (' . $currentFollower . ')',
                        $follow->user->created_at,
                        $follow->user->type
    );
    $currentFollower++;
}

$html .= <<<BUTTONS

<button onclick="previousPage()">previous</button> 
<button onclick="nextPage()">next</button>

BUTTONS;


$javascript = <<<JS
<script>
var currentPage = 0;
function previousPage() {
    if(currentPage > 0)
    {
        document.getElementById('page-number-'+currentPage).style.display = 'none';
        currentPage--;
        document.getElementById('page-number-'+currentPage).style.display = '';
    }
};

function nextPage() {
    if(currentPage < {$currentPage} - 1)
    {
        document.getElementById('page-number-'+currentPage).style.display = 'none';
        currentPage++;
        document.getElementById('page-number-'+currentPage).style.display = '';
    }
};
</script>
JS;

echo $javascript.$html;

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.