Tra Từ Điển

Thời gianTiếtThứ 2Thứ 3Thứ 4Thứ 5Thứ 6
8g40 - 9g101Tiếng Việt
Tiếng Anh
Tiếng Việt
Tiếng Việt
9g10 - 9g302Nghỉ giải lao
Tiếng Anh
Nghỉ giải lao
Nghỉ giải lao
9g30 - 10g3Toán
Toán
Toán

Tuesday, January 18, 2022

PHP#20MySQL#16 - tin.php : Lọc tin theo Ngôn ngữ, Thể loại, Loại & Phân trang

//tin.php
 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.ht{ font-weight: bold; color:#E80206; font-size:20px;}
</style>
</head>

<body>
<?php
include("../connect.php"); 
if(isset($_GET['lang'])) $lang=$_GET['lang'];
else $lang="vi";
?>
<form method="get" id="form1" name="form1" action="">
  <p>
    <label for="idTL">Ngôn ngữ:</label>
    <select name="lang" id="lang" onChange="form1.submit();">
      <option value="vi">Việt</option>
      <option value="en"<?php if($lang=="en") echo "selected";?>>English</option>
    </select>
  </p>
  <p>
    <label for="idTL">Thể loại:</label>
    <select name="idTL" id="idTL" onChange="form1.submit();">
    <?php 
$sl="select * from theloai where lang='$lang' order by ThuTu";
$kq=mysqli_query($link,$sl);
$idTL=0;
while($d=mysqli_fetch_array($kq))
{
if($idTL==0) $idTL=$d['idTL'];
?>
      <option value="<?php echo $d['idTL'];?>"<?php if(isset($_GET['idTL'])&&$_GET['idTL']==$d['idTL']) {echo "selected"; $idTL=$_GET['idTL'];}?>><?php echo $d['TenTL'];?></option>
      <?php }?>
    </select>
  </p>
  <p>
    <label for="idLT">Loại tin:</label>
    <select name="idLT" id="idLT" onChange="form1.submit();">
    <?php 
$sllt="select * from loaitin where idTL=$idTL order by ThuTu";
$kqlt=mysqli_query($link,$sllt);
$idLT=0;
while($dlt=mysqli_fetch_array($kqlt))
{
if($idLT==0) $idLT=$dlt['idLT'];
?>
      <option value="<?php echo $dlt['idLT'];?>"<?php if(isset($_GET['idLT'])&&$_GET['idLT']==$dlt['idLT']) {echo "selected";$idLT=$_GET['idLT'];}?>><?php echo $dlt['Ten'];?></option>
      <?php }?>
    </select>
  </p>
</form>
<table width="776" border="1" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <th width="51" scope="col">STT</th>
      <th width="140" scope="col">Tiêu đề</th>
      <th width="187" scope="col">Tóm tắt</th>
      <th width="120" scope="col">Hình mô tả</th>
      <th width="92" scope="col">Ngày </th>
      <th width="72" scope="col">Trạng thái</th>
      <th width="98" scope="col">Thêm</th>
    </tr>
    <?php
$sl="select * from tin where idLT=$idLT";
$kq=mysqli_query($link,$sl);
$tstin=mysqli_num_rows($kq);
$tin=5;
$tst=ceil($tstin/$tin);
if(isset($_GET['p'])) $p=$_GET['p'];
else $p=1;
$vt=ceil($p-1)*$tin;
 
$sltin="select * from tin where idLT=$idLT order by idTin DESC limit $vt,$tin";
$kqtin=mysqli_query($link,$sltin);
$stt=0;
while($dtin=mysqli_fetch_array($kqtin))
{
$stt++;
?>
    <tr>
      <td><?php echo $stt;?></td>
      <td><?php echo $dtin['TieuDe'];?></td>
      <td><?php echo $dtin['TomTat'];?></td>
      <td><img src="<?php echo $dtin['urlHinh'];?>" width="120" height="100" alt=""/></td>
      <td><?php echo date("d-m-Y h:i:s",strtotime($dtin['Ngay']));?></td>
      <td><?php if($dtin['AnHien']) echo "Hiện"; else echo "Ẩn";?></td>
      <td>Xóa/Sửa</td>
    </tr>
    <?php }?>
  </tbody>
</table>
<p>Trang 
<?php 
for($i=1;$i<=$tst;$i++)
{
if($i==$p) echo "<span class='ht'>".$i."</span>";
else{
?>
<a href="?p=<?php echo $i;?>&lang=<?php echo $lang;?>&idTL=<?php echo $idTL;?>&idLT=<?php echo $idLT;?>"><?php echo $i;?></a> 
<?php }}?>
</p>
</body>
</html>

//Cách 2: tin.php Lọc+Phân trang+Phân nhóm

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.ht{ font-weight: bold; color:#E80206; font-size:20px;}
</style>
</head>

<body>
<?php
include("../connect.php"); 
if(isset($_GET['lang'])) $lang=$_GET['lang'];
else $lang="vi";
?>
<form method="get" id="form1" name="form1" action="">
  <p>
    <label for="idTL">Ngôn ngữ:</label>
    <select name="lang" id="lang" onChange="form1.submit();">
      <option value="vi">Việt</option>
      <option value="en"<?php if($lang=="en") echo "selected";?>>English</option>
    </select>
  </p>
  <p>
    <label for="idTL">Thể loại:</label>
    <select name="idTL" id="idTL" onChange="form1.submit();">
    <?php 
$sl="select * from theloai where lang='$lang' order by ThuTu";
$kq=mysqli_query($link,$sl);
$idTL=0;
while($d=mysqli_fetch_array($kq))
{
if($idTL==0) $idTL=$d['idTL'];
?>
      <option value="<?php echo $d['idTL'];?>"<?php if(isset($_GET['idTL'])&&$_GET['idTL']==$d['idTL']) {echo "selected"; $idTL=$_GET['idTL'];}?>><?php echo $d['TenTL'];?></option>
      <?php }?>
    </select>
  </p>
  <p>
    <label for="idLT">Loại tin:</label>
    <select name="idLT" id="idLT" onChange="form1.submit();">
    <?php 
$sllt="select * from loaitin where idTL=$idTL order by ThuTu";
$kqlt=mysqli_query($link,$sllt);
$idLT=0;
while($dlt=mysqli_fetch_array($kqlt))
{
if($idLT==0) $idLT=$dlt['idLT'];
?>
      <option value="<?php echo $dlt['idLT'];?>"<?php if(isset($_GET['idLT'])&&$_GET['idLT']==$dlt['idLT']) {echo "selected";$idLT=$_GET['idLT'];}?>><?php echo $dlt['Ten'];?></option>
      <?php }?>
    </select>
  </p>
</form>
<table width="776" border="1" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <th width="51" scope="col">STT</th>
      <th width="140" scope="col">Tiêu đề</th>
      <th width="187" scope="col">Tóm tắt</th>
      <th width="120" scope="col">Hình mô tả</th>
      <th width="92" scope="col">Ngày </th>
      <th width="72" scope="col">Trạng thái</th>
      <th width="98" scope="col">Thêm</th>
    </tr>
    <?php
$sl="select * from tin where idLT=$idLT";
$kq=mysqli_query($link,$sl);
$tstin=mysqli_num_rows($kq);
$tin=3;
$sn=3;
$tst=ceil($tstin/$tin);
$tsn=ceil($tst/$sn);
if(isset($_GET['p'])) {$p=$_GET['p']; $n=ceil($p/$sn);}
else if(isset($_GET['n'])) {$n=$_GET['n']; $p=($n-1)*$sn+1;}
else $p=$n=1;
$vt=ceil($p-1)*$tin;
 
$sltin="select * from tin where idLT=$idLT order by idTin DESC limit $vt,$tin";
$kqtin=mysqli_query($link,$sltin);
$stt=0;
while($dtin=mysqli_fetch_array($kqtin))
{
$stt++;
?>
    <tr>
      <td><?php echo $stt;?></td>
      <td><?php echo $dtin['TieuDe'];?></td>
      <td><?php echo $dtin['TomTat'];?></td>
      <td><img src="<?php echo $dtin['urlHinh'];?>" width="120" height="100" alt=""/></td>
      <td><?php echo date("d-m-Y h:i:s",strtotime($dtin['Ngay']));?></td>
      <td><?php if($dtin['AnHien']) echo "Hiện"; else echo "Ẩn";?></td>
      <td>Xóa/Sửa</td>
    </tr>
    <?php }?>
  </tbody>
</table>
<p>Trang

<?php if($n>1){?><a href="?n=<?php echo ($n-1);?>">&lt;&lt;</a><?php }?>

<?php 
$dau=($n-1)*$sn+1;
$cuoi=$n*$sn;
if($cuoi>$tst) $cuoi=$tst;
for($i=$dau;$i<=$cuoi;$i++)
{
if($i==$p) echo "<span class='ht'>".$i."</span>";
else{
?>
<a href="?p=<?php echo $i;?>&lang=<?php echo $lang;?>&idTL=<?php echo $idTL;?>&idLT=<?php echo $idLT;?>"><?php echo $i;?></a> 
<?php }}?>

<?php if($n<$tsn){?><a href="?n=<?php echo ($n+1);?>">&gt;&gt;</a><?php }?>
</p>
</body>
</html>

0 comments:

Post a Comment