I am attemping to paginate for a public salaries database I am building for my newspaper. I have the code figuring out that it should only display X rows, however, each page displays the same rows. I also have noticed that clicking the links I've created for Previous/Next Page only adds an extra ?page= string to the URL. So, things like ?selID=3?page=1?page=2?page=2?page=2 are occuring when you press the next page button (every time it is pressed, another ?page=2 is added).

Here is the full code:

<?php
$link = mysql_connect('LINK', 'USER', 'PASS');
if (!$link) {
    die('Server Connection Failed : ' . mysql_error());
}
$db_selected = mysql_select_db('DATABASE', $link);
if (!$db_selected) {
    die ('Server Connection Failed : ' . mysql_error());
}



$ops = "";
$table = '';
$title = '';
$selNo = (isset($_GET['selID'])) ? intval($_GET['selID']) : 0;
    $r = mysql_query("SELECT * FROM Muni ORDER BY `Municipality`");
    while($d = mysql_fetch_assoc($r))
    {
        $sel = ($selNo == $d['ID']) ? ' selected' : '';
        $ops .= "\n\t<li><a href='?selID={$d['ID']}'>{$d['Municipality']}</a></li>";
        if($selNo == $d['ID']) $title = "<h4>{$d['Municipality']}</h4>";  
    }

    if($selNo !== 0){
$start = 0;
$per_page = 10;
if(!isset($_GET['page']))
{
$page = 1;
}else
{
$page = $_GET['page'];
}
if($page<=1)
$start = 0;
else
$start = $page * $per_page - $per_page;

$n=0;
$num_rows = mysql_num_rows($results);
$num_pages = $num_rows / $per_page;

$r = mysql_query("SELECT LastName, FirstName, Title, Salary FROM salary WHERE MuniID = $selNo ORDER BY Salary DESC LIMIT $start, $per_page");
        $table .= "<table id='newspaper-b' summary='Local Public Salaries'><thead><tr><th scope='col'>Name</th><th scope='col'>Job Title/Dept.</th><th scope='col'>Salary</th></tr></thead><tbody>";
        while($d = mysql_fetch_assoc($r))
        {
            $table .= "<tr><td>{$d['FirstName']} {$d['LastName']}</td><td>{$d['Title']}</td><td>${$d['Salary']}</td></tr>";   
        }
        $table .= "</tbody></table>";

    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database</title>


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
    <style type="text/css">
h4{font-family:"Arial", "Helvetica", Sans-Serif;font-color:#69c;font-size:15px;width:480px;text-align:left;margin:20px;}
#newspaper-b{font-family:"Arial", "Helvetica", Sans-Serif;font-size:12px;width:480px;text-align:left;border-collapse:collapse;border:1px solid #69c;margin:20px;}
#newspaper-b th{font-weight:normal;font-size:14px;color:#039;padding:15px 10px 10px;}
#newspaper-b tbody{background:#e8edff;}
#newspaper-b td{color:#669;border-top:1px dashed #fff;padding:10px;}
#newspaper-b tbody tr:hover td{color:#339;background:#d0dafd;}

        body { font-family:Arial, Helvetica, Sans-Serif; font-size:0.75em; color:#000;}
        .desc { color:#e8edff;}
        .desc a {color:#e8edff;}
        .dropdown { margin:20px; }
        .dropdown dd, .dropdown dt, .dropdown ul { margin:0px; padding:0px; }
        .dropdown dd { position:relative; }
        .dropdown a, .dropdown a:visited { color:#339; text-decoration:none; outline:none;}
        .dropdown a:hover { color:#339;}
        .dropdown dt a:hover { color:#339; border: 1px solid #69c;}
        .dropdown dt a {background:#e8edff url(arrow.png) no-repeat scroll right center; display:block; padding-right:20px;border:1px solid #69c; width:160px; padding:10px;}
        .dropdown dt a span {cursor:pointer; display:block;}
        .dropdown dd ul { background:#e8edff none repeat scroll 0 0; border:1px solid #339; color:#C5C0B0; display:none;
                          left:0px; padding:10px 0px; position:absolute; top:2px; width:auto; min-width:180px; list-style:none;}
        .dropdown span.value { display:none;}
        .dropdown dd ul li a { padding:10px; display:block;}
        .dropdown dd ul li a:hover { background-color:#d0dafd;}

        .dropdown img.flag { border:none; vertical-align:middle; margin-left:10px;martin-right:20px; }


    </style>
    <script type="text/javascript">
        $(document).ready(function() {
            $(".dropdown dt a").click(function() {
                $(".dropdown dd ul").toggle();
            });

            $(".dropdown dd ul li a").click(function() {
                var text = $(this).html();
                $(".dropdown dt a span").html(text);
                $(".dropdown dd ul").hide();
                $("#result").html("Selected value is: " + getSelectedValue("sample"));
            });

            function getSelectedValue(id) {
                return $("#" + id).find("dt a span.value").html();
            }

            $(document).bind('click', function(e) {
                var $clicked = $(e.target);
                if (! $clicked.parents().hasClass("dropdown"))
                    $(".dropdown dd ul").hide();
            });
        });
    </script>

</head>
<body>
    <dl id="sample" class="dropdown">
        <dt><a href="#"><span>Select Entity</span></a></dt>
        <dd>
            <ul>
                <?php echo $ops;?>
            </ul>
        </dd>
    </dl>
<?php
    echo $title . $table;
$prev = $page - 1;
$next = $page + 1;
if($prev == 0){
    echo'';
}else{
echo "<a href='?selID={$_GET['selID']}?page=$prev'>Prevous Page</a> | ";
}
     echo "<a href='?selID={$_GET['selID']}?page=$next'>Next Page</a>";
?>
                     </div>
</body>
</html>

Recommended Answers

All 2 Replies

Error is in line 139 and 141
Second paramter must be separated by & and not ?. Just replace that and your problem will be solved
line 139 must be

echo "<a href='?selID={$_GET['selID']}&page=$prev'>Prevous Page</a> | ";

and line 141 must be

 echo "<a href='?selID={$_GET['selID']}&page=$next'>Next Page</a>";

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.