简体   繁体   中英

error with the Ajax to show 3 user per page

i am trying to show a 3 user per page using Pagination using jquery,mySql and PHP so i follow some tutorial in this page :

http://php-dev-zone.blogspot.com/2013/09/simple-pagination-using-jquerymysql-and.html

i got 2 errors

the first error is that my platform do not support Mysql_query so i change it to Mysqli_query so i thing it is fixed

the second error is the page is displaying the page number only without the users info !?

can any one help me please

this is an image to show how is the page :

http://store1.up-00.com/2014-02/1391845579081.jpg

and those are my files after changing them to Mysqli_query

Config.php

    <?php

$db_username = '****';
$db_password = '*****';
$db_name = '*****';
$db_host = '********';
$item_per_page = 3;


$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
?>

data.php

<?php
include('config.php');
$per_page = 3; 
if($_GET)
{
    $page=$_GET['page'];
}

//getting table contents
$start = ($page-1)*$per_page;
$sql = "select * from users order by id limit $start,$per_page";
$rsd = mysqli_query($sql);
?>



<table id="tb1">
        <th><b>Id</b></th>
        <th><b>FirstName</b></th>
        <th><b>Email</b></th>
        <th><b>LastName</b></th>
        <?php
        while($row = mysqli_fetch_array($rsd))
        {
            $id    = $row['id'];
            $fname = $row['first_name'];
            $mname    = $row['email'];
            $lname    = $row['last_name'];
        ?>
        <tr>
        <td><?php echo $id; ?></td>
        <td><?php echo $fname; ?></td>
        <td><?php echo $mname; ?></td>
        <td><?php echo $lname; ?></td>
        </tr>
        <?php
        } //End while
        ?>
</table>

<style>
#tbl
{
width:800px;
border:1px solid #000000;
margin-top:50px;
}

#tbl tr:nth-child(odd) {
  background: #000000
}

#tbl td{
border:1px solid #000000
}

#tbl th
{
  background: #00000;
border:1px solid #000000
}
</style>

index.php

<?php
include('config.php');
$per_page = 3; 

//getting number of rows and calculating no of pages

$sql = "select count(*) from users";
$result = mysqli_query($connecDB,$sql);
$count = mysqli_fetch_row($result);
$pages = ceil($count[0]/$per_page);

?>


<!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" xml:lang="en">
<head>
    <title>PHPDevZone : Pagination Using Mysql and jQuery </title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://yourjavascript.com/2141420575/pagination.js"></script>   
<style>
body { margin: 0; padding: 0; font-family:Verdana; font-size:15px }
a
{
text-decoration:none;
color:#000000;

}

a:hover
{

color:#DF3D82;
text-decoration:underline;

}
#loading { 
width: 100%; 
position: absolute;
}

#pagination
{
text-align:center;
margin-left:120px;

}
li{ 
list-style: none; 
float: left; 
margin-right: 16px; 
padding:5px; 
color:#0063DC; 
}
li:hover
{ 
color:#FF0084; 
cursor: pointer; 
}
</style>
</head>
<body>
<div align="center">
   <div style="margin-top:300px;"><b><a href="http://php-dev-zone.blogspot.com" > PHPDevZone </a></b>: Pagination Demo</div>
 <div id="content" ></div>
    <table width="800px">
    <tr><Td>
            <ul id="pagination">
                <?php
                //Show page links
                for($i=1; $i<=$pages; $i++)
                {
                    echo '<li id="'.$i.'">'.$i.'</li>';
                }
                ?>
           </ul>    
        </Td>
    </tr></table>
<div id="loading" ></div>
    </div> 
</body>
</html>

pagination.js

    $(document).ready(function(){

    //Loading Image Display
    function Display_Load()
    {
        $("#loading").fadeIn(100);
        $("#loading").html("<img src='loading.gif' />");
    }
    //Hide Loading Image
    function Hide_Load()
    {
        $("#loading").fadeOut('slow');
    };


   //Default Starting Page Results

    $("#pagination li:first").css({'color' : '#FF0084','border' : 'none'});
    $("#content").load("data.php?page=1", Hide_Load());

    //Pagination Click
    $("#pagination li").click(function(){
        Display_Load();

        //CSS Styles
        $("#pagination li")
        .css({'border' : 'solid #dddddd 1px'})
        .css({'color' : '#0063DC'});

        $(this)
        .css({'color' : '#FF0084'})
        .css({'border' : 'none'});

        //Loading Data
        var pageNum = this.id;
        $("#content").load("data.php?page=" + pageNum, Hide_Load());
    });
});

Thank you

The problem is with your mysqli_query in Data.php page. mysqli_query first need a database link and then query, so your code have to be like this to get it works:

<?php
include ('config.php');
$per_page = 3;
if ($_GET) {
    $page = $_GET['page'];
}

//getting table contents
$start = ($page - 1) * $per_page;
$sql = "select * from users order by id limit $start,$per_page";
$rsd = mysqli_query($connecDB,$sql);
?>



<table id="tb1">
        <th><b>Id</b></th>
        <th><b>FirstName</b></th>
        <th><b>Email</b></th>
        <th><b>LastName</b></th>
        <?php
        while($row = mysqli_fetch_array($rsd))
        {
            $id    = $row['id'];
            $fname = $row['first_name'];
            $mname    = $row['email'];
            $lname    = $row['last_name'];
        ?>
        <tr>
        <td><?php echo $id; ?></td>
        <td><?php echo $fname; ?></td>
        <td><?php echo $mname; ?></td>
        <td><?php echo $lname; ?></td>
        </tr>
        <?php } //End while ?>
</table>

<style>
    #tbl {
        width: 800px;
        border: 1px solid #000000;
        margin-top: 50px;
    }

    #tbl tr:nth-child(odd) {
        background: #000000
    }

    #tbl td {
        border: 1px solid #000000
    }

    #tbl th {
        background: #00000;
        border: 1px solid #000000
    }
</style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM