Jump to content

PHP_self


sabo86

Recommended Posts

Hello guys.. I am just trying the page limiting code for mysql results.. and here is the code..

 

#
<?php
$server = "localhost";
#
$user = "root";
#
$pass = "password";
#
$databasename = "fleifel";
#
$db = mysql_connect($server, $user);
#
mysql_select_db($databasename,$db);
#

#
$sql = "SELECT * FROM items  ";
#
$query = mysql_query($sql,$db);
#
$total_results = mysql_num_rows($query);
#
$limit = "3"; //limit of archived results per page.
#
$total_pages = ceil($total_results / $limit); //total number of pages
#
if (empty($page))
#
{
#
$page = "1"; //default page if none is selected
#
}
#
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB
#

#
$query = "SELECT * FROM items LIMIT $offset, $limit";
#
$result = mysql_query($query);
#
//This is the start of the normal results...
#

#
while ($row = mysql_fetch_array($result))
#
{
#
// display your results as you see fit here.
#
}
#
mysql_close();
#

#

#
// This is the Previous/Next Navigation
#
echo "<font face=Verdana size=1>";
#
echo "Pages:($total_pages)  "; // total pages
#
if ($page != 1)
#
{
#
echo "<a href=$PHP_SELF?page=1><< First</a>   "; // First Page Link
#
$prevpage = $page - 1;
#
echo " <a href=$PHP_SELF?page=$prevpage><<</a> "; // Previous Page Link
#
}
#
if ($page == $total_pages)
#
{
#
$to = $total_pages;
#
}
#
elseif ($page == $total_pages-1)
#
{
#
$to = $page+1;
#
}
#
elseif ($page == $total_pages-2)
#
{
#
$to = $page+2;
#
}
#
else
#
{
#
$to = $page+3;
#
}
#
if ($page == 1 || $page == 2 || $page == 3)
#
{
#
$from = 1;
#
}
#
else
#
{
#
$from = $page-3;
#
}
#

#
for ($i = $from; $i <= $to; $i++)
#

#
{
#
if ($i == $total_results) $to=$total_results;
#
if ($i != $page)
#
{
#
echo "<a href=$PHP_SELF?showold=yes&page=$i>$i</a>";
#
}
#
else
#
{
#
echo "<b><font face=Verdana size=2>[$i]</font></b>";
#
}
#
if ($i != $total_pages)
#
echo " ";
#
}
#
if ($page != $total_pages)
#
{
#
$nextpage = $page + 1;
#
echo " <a href=$PHP_SELF?page=$nextpage>>></a> "; // Next Page Link
#
echo "   <a href=$PHP_SELF?page=$total_pages>Last >></a>"; // Last Page Link
#
}
#
echo "</font>";
#
?>

 

I got the following result:

# Pages:(2)  [1]

Notice: Undefined variable: PHP_SELF in C:\Program Files\EasyPHP 2.0b1\www\test fleifel\example.php on line 141

2

Notice: Undefined variable: PHP_SELF in C:\Program Files\EasyPHP 2.0b1\www\test fleifel\example.php on line 165

>>

Notice: Undefined variable: PHP_SELF in C:\Program Files\EasyPHP 2.0b1\www\test fleifel\example.php on line 167

  Last >>

 

What is the problem?

 

Link to comment
https://forums.phpfreaks.com/topic/124570-php_self/
Share on other sites

You don't need a reference to $_SERVER['PHP_SELF'] in the href's. If you don't put in an address, it will default to the current page. So those three lines can be written as

<?php
echo '<a href="?showold=yes&page=' . $i . '">$i</a>';
?>

<?php
echo ' <a href="?page=' . $nextpage . '">>></a> '; // Next Page Link
echo '   <a href="?page=' . $total_pages . '">Last >></a>'; // Last Page Link
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/124570-php_self/#findComment-643469
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.