Search code examples
phpget

How do I use php to link to a page with variables in the url?


So let's say the current url is...

.../friends.php?episode=1&season=1

How would I be able to have a default 'next' button to go to the next episode.

I know how to retrieve the variable with

$_GET[episode] and $_GET[season] and save it to a variable.

Then I can add one to one of them with restrictions... (and subtract)

But I can't figure out how to make it a link that I can press to go to next or previous.


Solution

  • You can use the http_build_query function:

    $season = $_GET["season"];
    $episode = $_GET["episode"];
    $query = http_build_query(array("season" => $season, "episode" => $episode+1));
    

    EDIT: I've switched the episode and season increment, now it increments the episodes.

    Then concat this with the link:

    echo "<a href='/page.php?$query'>Next Episode</a>";