有人可以告诉我我的代码出了什么问题:
class sub_category
{
private $con;
function __construct()
{
$this->con = new mysqli("localhost", "root", "", "whatever");
}
function show_all()
{
$per_page=5;
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}
$start_from = ($page-1)*$per_page;
$sql = "SELECT sub_kategori.id_sub_kategori, kategori.nama_kategori, sub_kategori.nama_sub_kategori FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori ORDER BY sub_kategori.id_sub_kategori LIMIT ?, ?";
$stmt = $this->con->prepare($sql);
$stmt->bind_param('ii', $start_from, $per_page);
$stmt->execute();
$stmt->bind_result($id, $namecat, $cat);
while($stmt->fetch())
{
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$namecat</td>";
echo "<td>$cat</td>";
echo "<td><a href='javascript:'>Update</a></td>";
echo "<td><a href='sub_category.php?ghgfh=$id'>Delete</a></td>";
echo "<td><a href=''>Report</a></td>";
echo "</tr>";
};
$stmt->close();
$page_sql = "SELECT sub_kategori.id_sub_kategori, kategori.nama_kategori, sub_kategori.nama_sub_kategori FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori ORDER BY sub_kategori.id_sub_kategori";
$stmt_page = $this->con->query($page_sql);
$total_record = $stmt_page->num_rows;
$total_page = ceil($total_record/$per_page);
echo "</tbody>";
echo "</table>";
echo "<center>";
echo "<ul class='pagination'>";
echo "<li><a href=sub_category.php?page=1>First Page</a></li>";
for($i=1;$i<=$total_page;$i++)
{
if(isset($_GET['page']))
{
if($i==$_GET['page'])
{
echo "<li class='active'><a href=sub_category.php?page=$i>$i</a></li>";
}
else
{
echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
}
}
else
{
echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
}
}
echo "<li><a href=sub_category.php?page=$total_page>Last Page</a></li>";
echo "</ul>";
echo "</center>";
$stmt_page->close();
}
function show($nama_sub_kategori)
{
$per_page=5;
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}
$start_from = ($page-1)*$per_page;
$nama_sub_kategori = "%$nama_sub_kategori%";
$sql = "SELECT sub_kategori.id_sub_kategori, kategori.nama_kategori, sub_kategori.nama_sub_kategori FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori WHERE sub_kategori.id_sub_kategori LIKE ? OR sub_kategori.nama_sub_kategori LIKE ? OR kategori.nama_kategori LIKE ? ORDER BY sub_kategori.id_sub_kategori LIMIT ?, ?";
$stmt = $this->con->prepare($sql);
$stmt->bind_param('sssii', $nama_sub_kategori, $nama_sub_kategori, $nama_sub_kategori, $start_from, $per_page);
$stmt->execute();
$stmt->bind_result($id, $namecat, $cat);
while($stmt->fetch())
{
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$namecat</td>";
echo "<td>$cat</td>";
echo "<td><a href=''>Update</a></td>";
echo "<td><a href='sub_category.php?ghgfh=$id'>Delete</a></td>";
echo "<td><a href=''>Report</a></td>";
echo "</tr>";
};
$stmt->close();
$page_sql = "SELECT count(*) as total FROM sub_kategori INNER JOIN kategori ON kategori.id_kategori = sub_kategori.id_kategori WHERE sub_kategori.id_sub_kategori LIKE ? OR sub_kategori.nama_sub_kategori LIKE ? OR kategori.nama_kategori LIKE ? ORDER BY sub_kategori.id_sub_kategori";
$stmt_page = $this->con->prepare($page_sql);
$stmt_page->bind_param('sss', $nama_sub_kategori, $nama_sub_kategori, $nama_sub_kategori);
$stmt_page->execute();
$stmt_page->bind_result($row);
$total_record = $row;
echo $total_record;
$total_page = ceil($total_record/$per_page);
echo "</tbody>";
echo "</table>";
echo "<center>";
echo "<ul class='pagination'>";
echo "<li><a href=sub_category.php?page=1>First Page</a></li>";
for($i=1;$i<=$total_page;$i++)
{
if(isset($_GET['page']))
{
if($i==$_GET['page'])
{
echo "<li class='active'><a href=sub_category.php?page=$i>$i</a></li>";
}
else
{
echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
}
}
else
{
echo "<li><a href=sub_category.php?page=$i>$i</a></li>";
}
}
echo "<li><a href=sub_category.php?page=$total_page>Last Page</a></li>";
echo "</ul>";
echo "</center>";
$stmt_page->close();
}
}
show_all()函数工作正常!它显示数据和分页没有问题!
show()函数向我展示它正在运行的数据!但是分页给了我NULL结果!
我的意思是
echo $ total record;
它给了我空分页!我的代码有问题吗?
答案 0 :(得分:1)
在列中添加名称(例如total):
SELECT count(*) as total FROM sub_kategori ....
然后回复价值:
echo $row->total
答案 1 :(得分:1)
也许我错过了一些明显的东西,因为我没有在PHP中以这种方式使用$ this;也就是说,指定对特定类之外的全局变量的访问。在函数之间共享变量时,$ global用于指定共享(在本例中为con)变量。
在showAll函数中,设置$ total_record = $ stmt_page-&gt; num_rows;在你的第二个(show)函数中你设置$ total_record = $ row;其中$ row似乎没有被初始化...因此NULL是正确的。