10/10/2018, 09:54
Làm sao giới hạn số tin trên một trang ???
Em mới làm web nên nhờ người ta thiết kế, em tìm được cái file pho quy định số trang nhưng em kô hiểu làm sao để quy định GIỚI HẠN SỐ TIN TRÊN MỘT TRANG?
Bác nào coi giúp em cái đoạn php ở dưới rùi chỉ em sửa chỗ nào nha
<?php
/*=================================*
|| +++++++++++++++++++++++++++++++ ||
|| + DNT NEWS EXPRESS + ||
|| + ------------------------- + ||
|| + Code by diepnghitinh + ||
|| + phat trien boi xtremedia + ||
|| + Y!m : nguyen2242x@yahoo.com + ||
|| +++++++++++++++++++++++++++++++ ||
*=================================*/
if (!defined('IN_NEWS')) die("<center>thao tác không th***7875; th***7921;c hi***7879;n</center>");
//////////////////// ======= VIEW CAT ======= ////////////////////
function view_cat(){
global $db_type,$tpl,$input;
//////////////////// block cat ////////////////////
$file_tpl = 'view_cat';
$main = $tpl->get_main($file_tpl);
$vc['VIEW_CAT'] = $tpl->get_block_from_str($main,'VIEW_CAT',1);
//////////////////// block cat ////////////////////
if(isset($_GET["cat"]))
{
$cat_id = intval($_GET["cat"]);
$counter = intval($_GET["page"]);
if(!empty($counter))
{
$page = $counter;
}
else
{
$page = 1;
}
$limit = page(); //so tin tren 1 trang
$adjacents = 1;
$query = "SELECT COUNT(*) as num FROM ".$db_type."news where categoryidn = ".$cat_id."";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
$query = "SELECT * FROM ".$db_type."news where categoryidn = ".$cat_id." order by newsid desc LIMIT $start, $limit";
$portfolio = mysql_query($query) or die (mysql_error()); //portfolio
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($page <= $lastpage)
{
}
else
{
$error = "<font class='font_loi'><center>ch***432;a có thông tin</center></font>";
return $error;
}
if($lastpage > 1)
{
$pagination .= "<div class="pagination">";
if ($page > 1)
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$prev" class="title" style="text-decoration: none">trang tr***432;***7899;c</a>";
else
$pagination.= "<span class="disabled">trang tr***432;***7899;c</span>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration:none">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 2 + ($adjacents*2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration: none">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lpm1" class="title" style="text-decoration: none">$lpm1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lastpage" class="title" style="text-decoration: none">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=1" class="title" style="text-decoration: none">1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=2" class="title" style="text-decoration: none">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration: none">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lpm1" class="title" style="text-decoration: none">$lpm1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lastpage" class="title" style="text-decoration:none">$lastpage</a>";
}
else
{
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=1" class="title" style="text-decoration: none">1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=2" class="title" style="text-decoration: none">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration: none">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$next" class="title" style="text-decoration: none">trang sau</a>";
else
$pagination.= "<span class="disabled">trang sau</span>";
$pagination.= "</div> ";
}
while($item = mysql_fetch_array($portfolio))
{
$row_id = $item[newsid];
$row_id_md5 = md5($row_id);
$row_cat_id = intval($item[categoryidn]);
$row_title = $item[title];
$row_pics = $item[picurl];
$row_mieuta = $item[mieuta];
if (isset($_GET["cat"]))
{
if ($_GET["cat"]=="$row_cat_id")
{
$html .= $tpl->assign_vars($vc['VIEW_CAT'],array('NEWS_ID'=>$row_id,'NEWS_TITLE'=>cut_string ($row_title,70,1),
'MIEU_TA'=>cut_string($row_mieuta,300,1),'LINK_PIC '=>pic("$row_id","$row_id_md5")));
}
}
}
$html .= "<center>".$pagination."</center>";
}
else
{
$error = "<font class='font_loi'>thao tác không th***7875; th***7921;c hi***7879;n</font>";
return $error;
}
$html = $tpl->assign_blocks_content($main,array('VIEWCAT'=>$htm l));
return $html;
}
//////////////////// ======= VIEW CAT ======= ////////////////////
?>
Bác nào coi giúp em cái đoạn php ở dưới rùi chỉ em sửa chỗ nào nha
<?php
/*=================================*
|| +++++++++++++++++++++++++++++++ ||
|| + DNT NEWS EXPRESS + ||
|| + ------------------------- + ||
|| + Code by diepnghitinh + ||
|| + phat trien boi xtremedia + ||
|| + Y!m : nguyen2242x@yahoo.com + ||
|| +++++++++++++++++++++++++++++++ ||
*=================================*/
if (!defined('IN_NEWS')) die("<center>thao tác không th***7875; th***7921;c hi***7879;n</center>");
//////////////////// ======= VIEW CAT ======= ////////////////////
function view_cat(){
global $db_type,$tpl,$input;
//////////////////// block cat ////////////////////
$file_tpl = 'view_cat';
$main = $tpl->get_main($file_tpl);
$vc['VIEW_CAT'] = $tpl->get_block_from_str($main,'VIEW_CAT',1);
//////////////////// block cat ////////////////////
if(isset($_GET["cat"]))
{
$cat_id = intval($_GET["cat"]);
$counter = intval($_GET["page"]);
if(!empty($counter))
{
$page = $counter;
}
else
{
$page = 1;
}
$limit = page(); //so tin tren 1 trang
$adjacents = 1;
$query = "SELECT COUNT(*) as num FROM ".$db_type."news where categoryidn = ".$cat_id."";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
$query = "SELECT * FROM ".$db_type."news where categoryidn = ".$cat_id." order by newsid desc LIMIT $start, $limit";
$portfolio = mysql_query($query) or die (mysql_error()); //portfolio
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($page <= $lastpage)
{
}
else
{
$error = "<font class='font_loi'><center>ch***432;a có thông tin</center></font>";
return $error;
}
if($lastpage > 1)
{
$pagination .= "<div class="pagination">";
if ($page > 1)
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$prev" class="title" style="text-decoration: none">trang tr***432;***7899;c</a>";
else
$pagination.= "<span class="disabled">trang tr***432;***7899;c</span>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration:none">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 2 + ($adjacents*2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration: none">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lpm1" class="title" style="text-decoration: none">$lpm1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lastpage" class="title" style="text-decoration: none">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=1" class="title" style="text-decoration: none">1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=2" class="title" style="text-decoration: none">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration: none">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lpm1" class="title" style="text-decoration: none">$lpm1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$lastpage" class="title" style="text-decoration:none">$lastpage</a>";
}
else
{
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=1" class="title" style="text-decoration: none">1</a>";
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=2" class="title" style="text-decoration: none">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class="current">$counter</span>";
else
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$counter" class="title" style="text-decoration: none">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "<a href="?dnt=/view/&cat=$cat_id&page=$next" class="title" style="text-decoration: none">trang sau</a>";
else
$pagination.= "<span class="disabled">trang sau</span>";
$pagination.= "</div> ";
}
while($item = mysql_fetch_array($portfolio))
{
$row_id = $item[newsid];
$row_id_md5 = md5($row_id);
$row_cat_id = intval($item[categoryidn]);
$row_title = $item[title];
$row_pics = $item[picurl];
$row_mieuta = $item[mieuta];
if (isset($_GET["cat"]))
{
if ($_GET["cat"]=="$row_cat_id")
{
$html .= $tpl->assign_vars($vc['VIEW_CAT'],array('NEWS_ID'=>$row_id,'NEWS_TITLE'=>cut_string ($row_title,70,1),
'MIEU_TA'=>cut_string($row_mieuta,300,1),'LINK_PIC '=>pic("$row_id","$row_id_md5")));
}
}
}
$html .= "<center>".$pagination."</center>";
}
else
{
$error = "<font class='font_loi'>thao tác không th***7875; th***7921;c hi***7879;n</font>";
return $error;
}
$html = $tpl->assign_blocks_content($main,array('VIEWCAT'=>$htm l));
return $html;
}
//////////////////// ======= VIEW CAT ======= ////////////////////
?>
Bài liên quan
SELECT name FROM employee ORDER BY name ASC LIMIT 99 OFFSET 9999
(lấy tên 99 nhân viên sau 9999 nhân viên)
cách này cũng giảm lượng thời gian xử lý rất nhiều
có thể tạo procedure để trả về theo số trang nhất định
procedure: PageEmployee(int pageSize, int pageOder,out int pageCount)
bác này làm ăn ẩu đả quá . phải lưu các giá trị đặc biệt thành các biến ở trên như biến số tin trên 1 trang gì đó chứ T_T
đây là code query để lấy các tin ra .
vì hình như nó còn chèn cái file gì vào . phải xem mới biết được!