Jump to content

Pagination issue - records show but not....

Here is my code

 

<?php
//Connecting to sql db.
$connect = mysqli_connect("127.0.0.1","root","","leorsite");

$rec_limit= 4;

$sql = "SELECT count(article_id) FROM article";
$retval = mysqli_query ($connect, $sql);

if (! $retval) {
	die('Could not get data: ' . mysqli_error());
}

$row = mysqli_fetch_array($retval, MYSQLI_NUM);
$rec_count = $row[];

if( isset($_GET{'page'} ) ) {
            $page = $_GET{'page'} + 1;
            $offset = $rec_limit * $page ;
         }else {
            $page = ;
            $offset = ;
         }
$left_rec = $rec_count - ($page * $rec_limit);
         $sql = "SELECT article_id, article_title, article_summary ". 
            "FROM article ".
            "LIMIT $offset, $rec_limit";

$retval = mysqli_query ($connect, $sql);

if(! $retval ) {
            die('Could not get data: ' . mysqli_error());
         }
         
         while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
	?>
            <div class="completeArticleHolder">
                        <div class="articleTitle">
                        <h2><?php echo $row['article_title'] ?></h2>
                      </div>
                      <div class="articleBodyText">
                      <?php echo $row['article_summary'] ?>
		      <p><strong><a href= 'read_more.php?article_id=<?php echo $row['article_id'] ?>'>&lt;&lt;READ MORE&gt;&gt;</a></strong> <strong>&lt;&lt;EDIT&gt;&gt;</strong></p>
                      </div>
                </div>
      <?php
		 }
		 ?>
<?php

if( $page >  ) {
            $last = $page - 2;
            echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>";
            echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
         }else if( $page ==  ) {
            echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
         }else if( $left_rec < $rec_limit ) {
            $last = $page - 2;
            echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>";
         }
         
         mysqli_close($connect);
?>

I get this error:

Notice: Undefined variable: _PHP_SELF in C:\xampp\htdocs\Leorwebsite\paginationArticles.php on line 56
Next 10 Records

 

Now when I change all of the echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>"; to echo "<a href = \"Articles.php?page = $last\">Last 10 Records</a>"; The the error goes away. However my issue is that when I click the next record link it will not show another 4 records but the same 4 that I originally view. If someone can help that would be great.

Link to comment
Share on other sites

Link to post
Share on other sites

I would recommend looking at this post which explains why you should NOT use PHP_SELF. Also, the "Next 10 Records" link will not work because it links to localhost, which will only work on your computer.

http://stackoverflow.com/questions/12710803/undefined-variable-php-self

If you insist on using it, make sure to use htmlspecialchars(), which will prevent XSS attacks.

The reason you're getting that error is that you never defined

$_PHP_SELF

. To get PHP_SELF, use

$_SERVER["PHP_SELF].

 

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

$phpFileName = basename(__FILE__);

This returns the name of the file at the end of the __FILE__ path (which is the name of the current script). 

 

function pagination($count, $rowsPerPage, $page, $phpfilename) {
	$maxPages = ceil($count / $rowsPerPage);
	$page = intval($page);

	echo '<ul class="pagination">';

	if ($page != 1) {
		// Generate previous arrow button.
		$listItem = "<li class='arrow'><a href='$phpfilename?page=1'>«</a></li>";
		$listItem .= "<li class='arrow";
		if (1 == $page) {
			$listItem .= " unavailable";
		}
		$listItem .= "'><a href='$phpfilename?page=" . ($page - 1) . "'>Prev</a></li>";
		echo $listItem;
	}

	// Calculate start and end page number.
	if ($page > 3) {
		$start = $page - 2;
	} else {
		$start = 1;
	}
	if ($start + 4 > $maxPages) {
		$end = $maxPages;
	} else {
		$end = $start + 4;
	}

	// Generate pagination buttons.
	for ($i = $start; $i <= $end; $i++) {
		$listItem = "<li";
		if ($i == $page) {
			$listItem .= " class='current' ";
		}
		$listItem .= "><a href='$phpfilename?page=$i'>$i</a></li>";

		echo $listItem;
	}

	if ($page != $maxPages && $maxPages != ) {
		// Generate next button and last arrow button.
		$listItem = "<li class='arrow";
		if ($maxPages == $page) {
			$listItem .= " unavailable";
		}
		$listItem .= "'><a href='$phpfilename?page=" . ($page + 1);
		$listItem .= "'>Next</a></li>";

		$listItem .= "<li class='arrow'><a href='$phpfilename?page=$maxPages'>»</a></li>";
		echo $listItem;
	}

	echo '</ul>';
}

This is currently how I generate pagination buttons on my site.

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Blade of Grass said:

$phpFileName = basename(__FILE__);

This returns the name of the file at the end of the __FILE__ path (which is the name of the current script). 

 


function pagination($count, $rowsPerPage, $page, $phpfilename) {
	$maxPages = ceil($count / $rowsPerPage);
	$page = intval($page);

	echo '<ul class="pagination">';

	if ($page != 1) {
		// Generate previous arrow button.
		$listItem = "<li class='arrow'><a href='$phpfilename?page=1'>«</a></li>";
		$listItem .= "<li class='arrow";
		if (1 == $page) {
			$listItem .= " unavailable";
		}
		$listItem .= "'><a href='$phpfilename?page=" . ($page - 1) . "'>Prev</a></li>";
		echo $listItem;
	}

	// Calculate start and end page number.
	if ($page > 3) {
		$start = $page - 2;
	} else {
		$start = 1;
	}
	if ($start + 4 > $maxPages) {
		$end = $maxPages;
	} else {
		$end = $start + 4;
	}

	// Generate pagination buttons.
	for ($i = $start; $i <= $end; $i++) {
		$listItem = "<li";
		if ($i == $page) {
			$listItem .= " class='current' ";
		}
		$listItem .= "><a href='$phpfilename?page=$i'>$i</a></li>";

		echo $listItem;
	}

	if ($page != $maxPages && $maxPages != ) {
		// Generate next button and last arrow button.
		$listItem = "<li class='arrow";
		if ($maxPages == $page) {
			$listItem .= " unavailable";
		}
		$listItem .= "'><a href='$phpfilename?page=" . ($page + 1);
		$listItem .= "'>Next</a></li>";

		$listItem .= "<li class='arrow'><a href='$phpfilename?page=$maxPages'>»</a></li>";
		echo $listItem;
	}

	echo '</ul>';
}

This is currently how I generate pagination buttons on my site.

The issue is that the older records do not show on the next page, not the buttons themselves showing up. The next record button shows fine, but it does not show the next 10 records.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×