2014-11-01 126 views
1

我已经写在MySQL的脚本,只是列出了别人的名字。它在MySQL中工作正常,但我一直试图让它更新到mysqli,我得到了大部分。但是,结果应该是分页的。分页工作正常,因为应该有5页,并在那里停止....但所有的结果都显示在每一页上。不知道我在做什么...任何帮助将不胜感激....我知道它有“mysql_result”迁移从MySQL到MySQLi扩展

这里做的是我在mysqli的新代码:

<?php 
include ('paginate.php'); //include of paginat page 

$per_page = 10;   // number of results to show per page 
$result = $con->query("SELECT EmployeeID,FirstName,LastName FROM employee;"); 
//If Database Error...Print the error and exit 
if (!$result) { 
printf("Error: %s\n", mysqli_error($con)); 
exit(); 
} 
//If No Database Error.....Continue 
if ($result) 
{ 
// Return the number of rows in result set 
$total_results=mysqli_num_rows($result); 
//printf("Result set has %d rows.\n",$total_results); 
// Free result set 
} 
//total pages we going to have 
$total_pages = ceil($total_results/$per_page); 
//if page is setcheck 
$show_page=''; 
if (isset($_GET['page'])) { 
$show_page = $_GET['page'];//it will telles the current page 
if ($show_page > 0 && $show_page <= $total_pages) { 
$start = ($show_page - 1) * $per_page; 
$end = $start + $per_page; 
} else { 
// error - show first set of results 
$start = 0;    
$end = $per_page; 
} 
} else { 
// if page isn't set, show first set of results 
$start = 0; 
$end = $per_page; 
} 
// display pagination 
$page = intval(isset($_GET['page'])); 
$tpages=$total_pages; 
if ($page <= 0) 
$page = 1; 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>View All Employees</title> 
<link rel="stylesheet" type="text/css" href="css/style.css" /> 
</head> 
<body> 
<div class="container"> 
<div class="row"> 
<div class="logo"> 
<a href="#"><img src="../admin/images/logo-thompson-industrial-services.gif" class="text- center"width="259" height="59" /></a> 
</div> 
</div> 
<br /> 
<div class="row"> 
<div class="span6 offset3"> 
<div class="mini-layout"> 
<?php 
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages; 
echo '<div class="pagination"><ul>'; 
if ($total_pages > 1) { 
echo paginate($reload, $show_page, $total_pages); 
} 
echo "</ul></div>"; 
// display data in table 
echo "<table class='table table-bordered'>"; 
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>";      
// loop through results of database query, displaying them in the table 
// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++) { 
// make sure that PHP doesn't try to show results that don't exist 
if ($i == $total_results) { 
break; 
} 
while($row = mysqli_fetch_assoc($result)) { 
$id = $row['EmployeeID']; 
$fname = $row['FirstName']; 
$lname = $row['LastName']; 
echo "<tr>"; 
echo '<td class="col-md-4">' .$fname .'</td>'; 
echo '<td class="col-md-4">' .$lname .'</td>'; 
echo "</tr>"; 
} 
} 
// close table> 
echo "</table>";    
?> 
</div> 
</div> 
</div> 
</div> 
</body> 
</html> 

这里是我原来的代码在MySQL:

<?php 
error_reporting(E_ALL | E_NOTICE); 
ini_set('display_errors', '1'); 
error_reporting(E_ALL^E_DEPRECATED); 
include('config.php'); //include of db config file 
include ('paginate.php'); //include of paginat page 

$per_page = 10;   // number of results to show per page 
$result = mysql_query("SELECT * FROM employee"); 
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results/$per_page);//total pages we going to have 

//-------------if page is setcheck------------------// 
$show_page=''; 
if (isset($_GET['page'])) { 
$show_page = $_GET['page'];    //it will telles the current page 
if ($show_page > 0 && $show_page <= $total_pages) { 
$start = ($show_page - 1) * $per_page; 
$end = $start + $per_page; 
} else { 
// error - show first set of results 
$start = 0;    
$end = $per_page; 
} 
} else { 
// if page isn't set, show first set of results 
$start = 0; 
$end = $per_page; 
} 
// display pagination 
$page = intval(isset($_GET['page'])); 

$tpages=$total_pages; 
if ($page <= 0) 
$page = 1; 
?> 


<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>View Employees</title> 
<link rel="stylesheet" type="text/css" href="css/style.css" /> 
<style type="text/css"> 

</style> 
</head> 
<body> 
<div class="container"> 
<div class="row"> 
<div class="logo"> 
<a href="#"><img src="../admin/images/logo-thompson-industrial-services.gif" class="text-center"width="259" height="59" /></a> 
</div> 
</div> 
<br /> 
<div class="row"> 
<div class="span6 offset3"> 
<div class="mini-layout"> 
<?php 
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages; 
echo '<div class="pagination"><ul>'; 
if ($total_pages > 1) { 
echo paginate($reload, $show_page, $total_pages); 
} 
echo "</ul></div>"; 
// display data in table 
echo "<table class='table table-bordered'>"; 
echo "<thead><tr><th>First Name</th> <th>Last Name</th></tr></thead>"; 
// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++) { 
// make sure that PHP doesn't try to show results that don't exist 
if ($i == $total_results) { 
break; 
} 

// echo out the contents of each row into a table 
echo "<tr>"; 
echo '<td>' . mysql_result($result, $i, 'FirstName') . '</td>'; 
echo '<td>' . mysql_result($result, $i, 'LastName') . '</td>'; 
echo "</tr>"; 
}  
// close table> 
echo "</table>"; 
// pagination 
?> 
</div> 
</div> 
</div> 
</div> 
</body> 
</html> 

在这里,我做错了什么,我不能弄明白....

任何帮助将不胜感激...

非常感谢。

+0

请尝试煮沸您的代码到最小的例子仍然存在问题;这不仅可以帮助我们,还可以帮助你自己找到答案。见http://stackoverflow.com/help/mcve – IMSoP 2014-11-01 19:08:27

+0

我想myqsl_result转换成mysqli的....这是工作,但结果都显示在同一页上...而不是paginatd – user3251779 2014-11-01 19:09:53

+0

有很多缺失:所有的变量从哪里来?例如。 '$ total_results'。 – Paul 2014-11-01 19:18:08

回答

0

我也只是删除while语句,并结合使用mysqli_data_seekmysqli_fetch_accoc。该MySQLi_data_seek点,结果我需要的。检查出来:

// display data in table 
echo "<table class='table table-bordered'>"; 
echo "<thead><tr><th>First 

Name</th> <th>Last Name</th></tr></thead>"; 
// loop through results of database query, displaying them in the table 
// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++) { 
// make sure that PHP doesn't try to show results that don't exist 
if ($i == $total_results) { 
break; 
} 

//go to the column you want the results 
mysqli_data_seek($result, $i); 
$row = mysqli_fetch_assoc($result); 
// echo out the contents of the row into a table 
echo "<tr>"; 
echo '<td>' . $row['FirstName'] . '</td>'; 
echo '<td>' . $row['LastName'] . '</td>'; 
echo "</tr>"; 
} 

} 
echo "</table>"; 
+0

这工作完美....我会选择它作为正确的答案....非常感谢您的帮助。 – user3251779 2014-11-01 21:20:03

+0

你能提出我的问题吗? – user3251779 2014-11-01 21:44:05

+0

您是否检查过其他解决方案?这个答案发生了什么。是的,我会投你一票。 – 2014-11-01 21:45:49

0

你提取查询的方式有问题。一旦你进入while statement它不会离开,直到所有的结果都做了,然后执行

if ($i == $total_results) { 
    break; 
} 

所以它从来没有得到一个机会来结束在I $超过$ total_result更大。修复方法是在查询中包含LIMITOFFSET。这意味着你必须修复分页页面。

所以才再次运行查询,如:

// display data in table 
echo "<table class='table table-bordered'>"; 
echo "<thead><tr><th>First 

Name</th> <th>Last Name</th></tr></thead>"; 
// loop through results of database query, displaying them in the table 
// loop through results of database query, displaying them in the table 
for ($i = $start; $i < $end; $i++) { 
// run the query again 

    $sql ="SELECT EmployeeID,FirstName,LastName FROM employee LIMIT $per_page OFFSET $end; 
    $result = $con->query($sql); 
    while($row = mysqli_fetch_assoc($result)) { 
     $id = $row['EmployeeID']; 
     $fname = $row['FirstName']; 

     $lname = $row['LastName']; 
     echo "<tr>"; 
     echo '<td class="col-md-4">' .$fname .'</td>'; 
     echo '<td class="col-md-4">' .$lname .'</td>'; 
     echo "</tr>"; 
    } 

} 
echo "</table>";    
+0

非常感谢您的答复....你知道我怎么会修复paginization? – user3251779 2014-11-01 20:37:03

+0

我需要看看分页代码。但是,我可以帮助 – 2014-11-01 21:44:00