Changing a PHP variable at the click of a button

Asked

Viewed 207 times

1

Currently I do a search using my code I pick only the first page, to change the results I have to do it manually, as I can 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>";
}
}
?>

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 set the $pageParameter with default value equal to 1 not to need to refactor the function in all its codes.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.