mauriatti
mauriatti

Reputation: 69

PHP pagination - url not working

I having some difficult to paginate my database results. When i click in the next button i get the link: http://localhost/sistema-noticias/?action=noticias=2
But instead of showing my page 2 database results i go to homepage.

INDEX.PHP

<?php

require( "config.php" );
$action = isset( $_GET['action'] ) ? $_GET['action'] : "";

switch ( $action ) {
  case 'noticias':
    noticias();
    break;
  case 'viewArticle':
    viewArticle();
    break;

  default:
    homepage();
}

function noticias() {
  $results = array();
  $data = Article::getList();
  $results['articles'] = $data['results'];
  $results['totalRows'] = $data['totalRows'];
  $results['pageTitle'] = "Article Archive | FPT - Federação Portuguesa do Táxi";
  require( TEMPLATE_PATH . "/archive.php" );
}

function viewArticle() {
  if ( !isset($_GET["articleId"]) || !$_GET["articleId"] ) {
    homepage();
    return;
  }

  $results = array();
  $results['article'] = Article::getById( (int)$_GET["articleId"] );
  $results['pageTitle'] = $results['article']->title . " | FPT - Federação Portuguesa do Táxi";
  require( TEMPLATE_PATH . "/viewArticle.php" );
}

function homepage() {
  $results = array();
  $data = Article::getList( HOMEPAGE_NUM_ARTICLES );
  $results['articles'] = $data['results'];
  $results['totalRows'] = $data['totalRows'];
  $results['pageTitle'] = "FPT - Federação Portuguesa do Táxi";
  require( TEMPLATE_PATH . "/homepage.php" );
}

NOTICIA.PHP

<?php
$query1=mysql_connect("localhost","root","root");
mysql_select_db("noticias",$query1);

$numRows=100;

$noticias = isset($_GET['noticias']) ? $_GET['noticias'] : 1;
$start = ($noticias - 1) * $numRows;
$limit = 2;


$query=mysql_query("select * from articles LIMIT $start, $limit");
echo "<ul>";
while($query2=mysql_fetch_array($query))
{
echo "<li>".$query2['title']."</li>";
}
echo "</ul>";
$rows=mysql_num_rows(mysql_query("select * from articles"));
$total=ceil($rows/$limit);

if($noticias>1)
{
echo "<a href='./?action=noticias=".($noticias-1)."' class='button'>PREVIOUS</a>";
}
if($noticias!=$total)
{
echo "<a href='./?action=noticias=".($noticias+1)."' class='button'>NEXT</a>";
}

echo "<ul class='page'>";
for($i=1;$i<=$total;$i++)
{
if($noticias==$noticias) { echo "<li class='current'>".$noticias."</li>"; }

else { echo "<li><a href='./?action=noticias=".$noticias."'>".$noticias."</a></li>"; }
}
echo "</ul>";
?>

Help!

Upvotes: 0

Views: 338

Answers (2)

Oliveira
Oliveira

Reputation: 1321

Your querystring is returning action = "noticias=2" and in your switch there's no case for that value, so you end up falling in the default, which is the homepage.

You should do: action=noticias&page=2

Upvotes: 1

Shashank Singh
Shashank Singh

Reputation: 1300

You may be passing wrong query in your URL.

 <a href='./?action=noticias=".$noticias."'>".$noticias."</a>

Replace above line with this one

 <a href='./?action=noticias&page=".$noticias."'>".$noticias."</a>

Upvotes: 2

Related Questions