Changing a PHP variable with the click of a button

1

Currently I do a search using my code I only get the first page, to change the results I have to do this manually, how can I change the $ page variable with the click of a button.

My code:

<?php 
function buscaUser($user)
{
$opts = [
        'http' => [
                'method' => 'GET',
                'header' => [
                        'User-Agent: PHP'
                ]
        ]
];
$page = 1;
$user=  $_GET['username'];
$context = stream_context_create($opts);
$content = file_get_contents('https://api.github.com/search/users?q='.$user."&page=".$page, false, $context); 
$json = json_decode($content,true);
$jsoncount = count($json['items']);
$totalcount = $json['total_count'];
$page = $totalcount/30;
echo "<h1> Total de :".$totalcount." Usuarios encontrados.</h1> <br>";
for ($numItem = 0 ; $numItem <= $jsoncount-1; $numItem++){
echo "<div class='usuario'>";
echo "<a href=".$json['items'][$numItem]['html_url']."><img class='img-thumbnail' width='100px' height='100px' src=".$json['items'][$numItem]['avatar_url']."/><br>";
echo $json['items'][$numItem]['login']."</a><br>";
echo "Pontos: ".$json['items'][$numItem]['score']."<br>";
echo "</div>";
}
}
?>
    
asked by anonymous 18.10.2016 / 18:44

1 answer

0

You can pass the value of the $ page variable as a function parameter:

function buscaUser($user, $pageParameter = 1)
{
    //...código anterior

    $page = $pageParameter;

    //...codigo posterior
}

I've set the $ pageParameter to a default value of 1 so you do not need to refactor the function throughout your code.

I hope I have helped.

    
18.10.2016 / 21:54