排序和分页相结合,PHP与SQL

时间:2013-06-03 16:02:40

标签: php sql sorting pagination sql-delete

我有一个CV数据库,你可以看到下面的字段,它们非常标准。通过将信息发送到SQL数据库的简单表单来完成检索。

我很满意我的简单系统,直到我的收件箱里有超过500名申请人。我之前的系统允许我一个接一个地查看申请人本来会永远...

我想要实现的是一个简单的后端页面,类似于表视图的phpmyadmin。 (不,我不想只使用phpmyadmin,因为我想将简历筛选任务交给其他员工)

基本上,概念是像excel一样显示表,允许通过单击标题进行排序,分页[每页20行]和一个复选框来删除行。

我很乐意寻求帮助,因为我已经付出了很多努力来解决这个问题;)

到目前为止,我得到的是:

排序没有问题,点击其中一个标题会将localhost / mena / new3.php?sort = fname吐出到地址栏并解析正确的Sql查询并对页面进行排序。

到目前为止,分页不起作用。该页面显示所有815候选人。它提供了编号的链接1-42,当点击时,地址栏中的结果变为localhost / new3.php?page = 2但是0更改。

同样对于我的生活,我看不出如何将php删除包含在此..

9 yo伪代码的想法是:

//Input the rows from SQL
While($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td> $checkbox1
  if checkbox1=true then mysqli_query($con,"DELETE FROM cv WHERE .$row[].");
  echo "<td>" . $row['title'] . 

到目前为止我的代码:

<?php
$con=mysqli_connect("localhost","root","","test_db-jil");
// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Pagination  
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 

// Sort, from headers.
if(isset($_REQUEST['sort'])){
    if($_GET['sort'] == "title"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY title");
    }
    elseif($_GET['sort'] == "fname"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY fname");
    }
    elseif($_GET['sort'] == "lname"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY lname");
    }
    elseif($_GET['sort'] == "gender"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY gender");
    }
    elseif($_GET['sort'] == "dob"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY dob");
    }
    elseif($_GET['sort'] == "nationality"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY nationality");
    }
    elseif($_GET['sort'] == "language"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY language");
    }
    elseif($_GET['sort'] == "phone"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY phone");
    }
    elseif($_GET['sort'] == "email"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY email");
    }
    elseif($_GET['sort'] == "uni"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY uni");
    }
    elseif($_GET['sort'] == "prog"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY prog");
    }
    elseif($_GET['sort'] == "graddate"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY graddate");
    }
    elseif($_GET['sort'] == "startdate"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY startdate");
    }
    elseif($_GET['sort'] == "grad"){
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY grad");
    }   
    else{
    $result = mysqli_query($con,"SELECT * FROM cv ORDER BY fname");
    }
}
else{ // Default if no parameters passed
    $result = mysqli_query($con,"SELECT * FROM cv");
    }


//Table of Content  
echo "<table border='1'>
<tr>
<th><a href=new3.php?sort=title>Title</a></th>

<th><a href=new3.php?sort=fname>First Name</a></th>

<th><a href=new3.php?sort=lname>Last Name</a></th>

<th><a href=new3.php?sort=gender>Gender</a></th>

<th><a href=new3.php?sort=dob>Date Of Birth</a></th>

<th><a href=new3.php?sort=nationality>Nationality</a></th>

<th><a href=new3.php?sort=language>Language</a></th>

<th><a href=new3.php?sort=phone>Phone No</a></th>

<th><a href=new3.php?sort=email>Email</a></th>

<th><a href=new3.php?sort=uni>University</a></th>

<th><a href=new3.php?sort=prog>Program</a></th>

<th><a href=new3.php?sort=graddate>Graduated</a></th>

<th><a href=new3.php?sort=startdate>Start Date</a></th>

<th><a href=new3.php?sort=grad>Applying for</a></th>

<th>CV File</th>

</tr>";

//Input the rows from SQL
While($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['fname'] . "</td>";
  echo "<td>" . $row['lname'] . "</td>";
  echo "<td>" . $row['gender'] . "</td>";
  echo "<td>" . $row['dob'] . "</td>";
  echo "<td>" . $row['nationality'] . "</td>";
  echo "<td>" . $row['language'] . "</td>";
  echo "<td>" . $row['phone'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['uni'] . "</td>";
  echo "<td>" . $row['prog'] . "</td>";
  echo "<td>" . $row['graddate'] . "</td>";
  echo "<td>" . $row['startdate'] . "</td>";
  echo "<td>" . $row['grad'] . "</td>";
  echo "<td><a href=" . $row['cvfilename'] .">" . $row['cvfilename'] ."</a></td>";


  echo "</tr>";
  }
echo "</table>";

//Get total count of rows then ceil divide by 20 as pages
$sql = "SELECT COUNT(*) as 'num' FROM cv";
$total_pages = $con->query($sql) or die(mysqli_error($connection)); 
$row = $total_pages->fetch_assoc();
$total_pages  = ceil($row['num'] / 20);

for ($i=1; $i<=$total_pages; $i++) { 
            //Can I ?page= and ?sort= ??????
            echo "<a href='new3.php?page=".$i."'>".$i."</a> "; 
};

mysqli_close($con);
?>

回顾一下,请帮我修复分页,让它与sort一起工作,最后在每行添加一个删除复选框。 :)

1 个答案:

答案 0 :(得分:1)

您知道只需分配

就可以优化整个“else if”语句块

$ _获取变量: $ type = $ _GET;

然后在你的mysqli中使用它: $ result = mysqli_query($ con,“SELECT * FROM cv ORDER BY $ type”);

要限制结果,请使用LIMIT: $ result = mysqli_query($ con,“SELECT * FROM cv ORDER BY $ type LIMIT 20,$ page”);

20 =要返回多少 $ page =您希望结果从

开始