Skip to content Skip to sidebar Skip to footer

Auto Increment When Clicked An Anchor

I want to auto increment my p_counter whenever the user click the anchor named next, but I don't know what to do since php is static and can't handle events, what is the alternati

Solution 1:

If you want that to work out, you need a () open/close parenthesis on the arithmetic:

echo '<a id = "next" href = "index.php?pages='.($p_counter+=1).'">'.'next'.'</a>';

By the way, if you want the value to persist, you could use sessions for this.

session_start(); // obviously start a session

if(!isset($_SESSION['counter'])) { // initialize that counter
    $_SESSION['counter'] = 1;
}

echo '<a id = "next" href = "index.php?pages='.($_SESSION['counter']++).'">'.'next'.'</a>';

Solution 2:

Pass the value along in the URL to the next page. Then use $_get to grab the value and update it.


Solution 3:

Using $_GET allows you to access the "pages" variable in your URI, so php will know what the current page is and be able to determine what the next page should be as well:

$current_page = 0;
if ($_GET['pages']) {
    $current_page = $_GET['pages'];
}
$next_page = $current_page + 1;
echo '<a id = "next" href = "members.php?pages=' . $next_page . '">next</a>';

Post a Comment for "Auto Increment When Clicked An Anchor"