$_SERVER ['PHP_SELF'] makes all links redirect to the same page. How can I make a separate section of links that is not affected by this pagination using PHP and MySQL? I know that’s normal and I notice that the ending of the url changes to show other pagination sections but I need to add a home button to go to another page like a home page that is not effected by this. Here’s the script.

<?php

$connection = mysqli_connect("localhost","1234_user","1234","1234db");

$sql = "SELECT COUNT(user_id) FROM user_details";
$query = mysqli_query($connection, $sql);
// total row count
$row = mysqli_fetch_row($query);
$rows = $row[0];
// results displayed per page
$page_rows = 5;
// page number of last page
$last = ceil($rows/$page_rows);
// makes sure $last cannot be less than 1
if($last < 1) {
$last = 1;
}
// page num
$pagenum = 1;
// get pagenum from URL if it is present otherwise it is 1
if(isset($GET['pn'])) {
$pagenum = preg_replace('#[0-9]#', '', $
GET['pn']);
}
// makes sure the page number isn't below 1, or more then our $last page
if($pagenum < 1) {
$pagenum = 1;
}
else if($pagenum > $last) {
$pagenum = $last;
}
// set the rage of rows to query for the chosen $pagenum
$limit = 'LIMIT ' . ($pagenum - 1) * $page_rows . ',' . $page_rows;
$sql = "SELECT user_id, username FROM user_details ORDER BY user_id DESC $limit";
$query = mysqli_query($connection, $sql);
// shows the user what page they are on, and the total number of pages
$textline1 = "User_details (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// establish $paginationCtrls variable
$paginationCtrls = '';
// if more the 1 page
if($last != 1) {
if($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'. $SERVER['PHP_SELF'] .'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';
// Render clickable number links
for($i = $pagenum - 4; $i < $pagenum; $i++) {
if($i > 0) {
$paginationCtrls .= '<a href="'. $
SERVER['PHP_SELF'] .'?pn='.$i.'">'.$i.'</a> &nbsp; ';
}
}
}
// render the target page number without a link
$paginationCtrls .= ''. $pagenum . ' &nbsp; ';
// render clickable number links that appear on the right
for($i = $pagenum + 1; $i < $last; $i++) {
$paginationCtrls .= '<a href="'. $SERVER['PHP_SELF'] .'?pn='.$i.'">'.$i.'</a> &nbsp; ';
// allows up to 4 pages
if($i >= $pagenum + 4) {
break;
}
}
if($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' &nbsp; &nbsp; <a href="'. $
SERVER['PHP_SELF'] .'?pn='. $next .'">Next</a> ';
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$user_id = $row["user_id"];
$username = $row["username"];
$list .= '<p><a href="username.php?user_id='.$user_id.'">'.$username.' Username</a> - Click here to view the Username<br></p>';
}
// close your database connection
mysqli_close($connection);
?>
<!DOCTYPE html>
<html>
<head>
<title>Paging Example</title>

<style>
div#pagination_controls {
font-size:21px;
}
div#pagination_controls > a {
color: #06F;
}
div#pagination_controls > a:visited {
color: #06F;
}
</style>
</head>
<body>
<h1><a href=”www.home.com”>Home</a></h1>

<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $list; ?></p>
<div user_id = "pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>

    When posting, please use the appropriate formatting tags described in the [thread=10238120]FAQ[/thread] to format your code.

    If you want to link to a page other than [font=monospace]PHP_SELF[/font], then don't use [font=monospace]$_SERVER['PHP_SELF'][/font]. Use the URL of the page you want to link to.

      Weedpacket;11061631 wrote:

      When posting, please use the appropriate formatting tags described in the [thread=10238120]FAQ[/thread] to format your code.

      If you want to link to a page other than [font=monospace]PHP_SELF[/font], then don't use [font=monospace]$_SERVER['PHP_SELF'][/font]. Use the URL of the page you want to link to.

      Sorry i'm still new. Well how can I do that every time I remove that it keeps giving me this message, errorParse error: syntax error, unexpected 'www' (T_STRING) and its saying that its own line 44 this is how I replaced it I remove all the . $_SERVER['PHP_SELF'] . with www.example.com and it still give me an error. How would you have done it?

        Note that the code above is "escaping" from quoted mode to echo the PHP_SELF variable. This is basic string stuff, applicable to several programming languages.

        // valid
        echo "www.example.com"; 
        
        //T_STRING parse error, like yours
        echo www.example.com; 
        
        // valid; $foo is 'expanded' to whatever its value is ...
        echo "Something, something else, and $foo"; 
        
        //with single quotes, the literal string '$foo' will be echoed, *not the value of $foo*
        echo  'Something, something else, and $foo'; 
        
        // with the variable concatenated to the string, no parse error, and value is printed
        // this would also work with double quotes
        echo 'Something, something else, and ' . $foo; 
        
        // valid HTML link, $foo is expanded to its value because the entire string is in double quotes
        echo "<a class='myClass' href='$foo'>Click Here</a>";  
        
        // note also in the above that we've used single quotes for the HTML attributes
        // because the string is in (delimited by) double quotes.
        
        
        // This is close to what you have going on up there.  The string's delimited by single quotes,
        // the HTML attribs are double quoted, and in order to print the value of the variable 
        // (in that case, PHP_SELF), they escaped from the sting and referenced the variable directly:
        
        echo '<a class="myClass" href="' . $foo . '">Click Here</a>'; //valid; HTML attributes double-quoted
        
        // to change that to an actual string URL:
        
        echo '<a class="myClass" href="www.example.com">Click Here</a>'; //valid; HTML attributes double-quoted
        
        // in double quote format:
        
        echo "<a class='myClass' href='www.example.com'>Click Here</a>"; //also valid; HTML attributes single-quoted

        Here's the Friendly Manual's discussion of strings.

        HTH!

          Look up what $_SERVER['PHP_SELF'] is. You don't use it in every link on the page.

          And dalecosp's reply is correct given what you have said about "unexpected 'www'". See his first two examples.

            I figure it out thanks every one for your help. The problem was that I didn't add the correct hyperlink structure it was something so tiny wow.

              Write a Reply...