mauriatti
mauriatti

Reputation: 69

NOTICE: Undefined index: Page

I dont understand what im doing wrong, im getting this message:

Notice: Undefined index: page in /Users/prinect/htdocs/sistema-noticias/classes/Article.php on line 221

My code:

public static function getList( $numRows=30, $order="publicationDate DESC" ) {
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );



$offset = ($_GET['page'] - 1) * $numRows;
    $rowCount = 20;

    $sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM articles
            ORDER BY {$order} LIMIT :offset, :rowCount";

$st = $conn->prepare( $sql );
$st->bindValue( ":rowCount", $rowCount, PDO::PARAM_INT );

$st->bindValue( ":offset", $offset, PDO::PARAM_INT );

 $st->execute();
    $list = array();

    while ( $row = $st->fetch() ) {
      $article = new Article( $row );
      $list[] = $article;
    }

    // Now get the total number of articles that matched the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";
    $totalRows = $conn->query( $sql )->fetch();
    $conn = null;
    return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
  }

Upvotes: 0

Views: 549

Answers (2)

AboElzooz
AboElzooz

Reputation: 319

you need to check the page if it don't exist because when the page is not in the query param it will be considered as undefined

$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = ($page - 1) * $numRows;
$rowCount = 20;

Upvotes: 1

GHugo
GHugo

Reputation: 2654

Undefined index means in your case that you are trying to access to an element of an array that does not exist.

It is most likely your $_GET['page']. Do you check if this exists? You should.

For example:

$page = (isset($_GET['page']) ? $_GET['page'] : 1; // 1 is the default page number

Upvotes: 3

Related Questions