Other alternatives to GET request in PHP pagination

Apparantly no GET variables are used in our system (unless you do an AJAX call) so I’m having problems finding other alternatives. I’m a beginner and tbh I have no idea on how to find alternatives other than using $_GET. Maybe the variable $request_uri(array) would work or load the page with AJAX. Any ideas?

$request_uri array:

if (isset($request_uri[$i1+1]) && $request_uri[$i1+1] != "") {

    $setTeam = Teams::setTeamSession($request_uri[$i1+1]);

    header('Location: /account/');

}

As I said earlier, I tried to use GET method but it did not work.

$numPerPage = 2;

			if(isset($_GET['page'])) {
				$page = $_GET['page'];
			} else {
				$page = ""; 
			}

			if($page == "" || $page == 1){
				$start = 0; 
			} else {
				$start = ($page * $numPerPage) - $numPerPage; 
			}

			$countSql = DBi::db3connect()->prepare("SELECT COUNT(a.teamID) FROM userTeams a LEFT JOIN teams b ON a.teamID=b.teamID WHERE a.uID=:uID AND a.active=:active"); 
			$countSql->execute(array(
				':uID'  =>  $_SESSION['user']['uID'],
                ':active'   =>  1
			)); 
			$row = $countSql->fetch(); 
			$numTeams = $row[0];
			$numLinks = ceil($numTeams/$numPerPage); 
		

        	$stmt = DBi::db3connect()->prepare("SELECT a.teamID, b.teamName, b.hashKey FROM userTeams a LEFT JOIN teams b ON a.teamID=b.teamID WHERE a.uID=:uID AND a.active=:active LIMIT $start, $numPerPage");

I think the problem lies below. When I click on the link it takes me to an empty page. It should take me to selectTeam?page=1 instead.

			for ($i=1; $i <= $numLinks; $i++){
				echo "<li><a href='selectTeam.php?page={$i}'>{$i}</a></li>"; 
			}

You’re asking a lot of different questions here. $_GET parameters should work in any environment, so it’s worth seeing what could be going wrong in your code first.

First off, the last part of your post:

for ($i=1; $i <= $numLinks; $i++){
    echo "<li><a href='selectTeam.php?page={$i}'>{$i}</a></li>"; 
}

What does your output html look like? You can see this in most browsers by right clicking to get a menu, then clicking “view source”.

You need to go and ask whoever told you that for more details then.

You need threes things to do pagination:

if (isset($_GET['page']) && !empty($_GET['page'])) {
$current_page = urldecode($_GET['page']);
} else {
$current_page = 1;
}

$per_page = 5; // Total number of records to be displayed:
$total_count = CMS::countAll(); // Total Records in the db table:

/* Send the 3 variables to the Pagination class to be processed */
$pagination = new Pagination($current_page, $per_page, $total_count);

To figure out offset:
$per_page * ($current_page - 1);

To figure out total pages:
ceil($total_count / $per_page);

Think you can do the links

$links = "";
        $links .= "<div class=\"pagination\">";
        if ($current_page <= $total_pages()) {
            if ($current_page === 1) {
                $links .= "<a class='selected' href=\"{$url}?page=1\">1</a>";
            } else {
                $links .= "<a href=\"{$url}?page=1\">1</a>";
            }

            $i = max(2, $current_page - 5);
            if ($i > 2) {
                $links .= '<span class="three-dots">' . " ... " . '</span>';
            }
            for (; $i < min($current_page + 6, $total_pages()); $i++) {
                if ($current_page === $i) {
                    $links .= "<a class='selected' href=\"{$url}?page={$i}\">{$i}</a>";
                } else {
                    $links .= "<a href=\"{$url}?page={$i}\">{$i}</a>";
                }

            }
            if ($i !== $total_pages) {
                $links .= '<span class="three-dots">' . " ... " . '</span>';
            }
            if ($i === $total_pages) {
                $links .= "<a href=\"{$url}?page={$total_pages}\">{$total_pages}</a>";
            } elseif ($i === $this->current_page) {
                $links .= "<a class='selected' href=\"{$url}?page={$this->total_pages()}\">{$total_pages}</a>";
            } else {
                $links .= "<a href=\"{$url}?page={$total_pages}\">{$total_pages}</a>";
            }

        }
        $links .= "</div>";

and just for added measure here’s my SQL statement:
$sql = 'SELECT * FROM myTable ORDER BY date_updated DESC LIMIT :perPage OFFSET :blogOffset';

These scripts won’t work for one I converted over to Procedural from OOP, but I just want to show you that using $_GET will work. Doing via Ajax would not be hard at all.

Sponsor our Newsletter | Privacy Policy | Terms of Service