Anthony
Anthony

Reputation: 481

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.

Upvotes: 2

Views: 157

Answers (3)

Pankaj Khairnar
Pankaj Khairnar

Reputation: 3108

Working Code

<?php
$season = $_GET['season'];
$episode = $_GET['episode'];

echo '<a href="friends.php?episode='.($episode - 1).'&season='.$season.'"> Back Episode  </a>-----';
echo '<a href="friends.php?episode='.($episode + 1).'&season='.$season.'">  Forward Episode  </a><br>';

echo '<a href="friends.php?episode='.$episode.'&season='.($season-1).'"> Back Season</a>-------';
echo '<a href="friends.php?episode='.$episode.'&season='.($season+1).'">Forward Season</a>';

Upvotes: 1

dande
dande

Reputation: 233

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>";

Upvotes: 3

koopajah
koopajah

Reputation: 25552

just construct the link you want and then put it into an a HTML element:

$link = "./friends.php?episode=" . $_GET['episode'] . "&season=" . $_GET['season'];
echo "<a href='$link'>Next episode</a>";

Upvotes: 0

Related Questions