简体   繁体   English

如何排序<div id="text_translate"><p>我在作业中需要做的一件事是在表格中显示有关注册用户的信息。 必须通过单击按钮(性别、名字、姓氏……)以不同的方式对其进行排序。</p><p> 我在 PHP 中写这个。 我唯一想到的是使用不同的页面,但我会得到大约 20 个不同的页面。</p><p> 实现这一点的最佳方法是什么? 每种订单类型都有不同的页面?<br> 我从未使用过 jQuery,但它似乎对这类事情有好处?</p></div>PHP 的数据<table> </table>

[英]How to sort <table> data with PHP

One of the things I need to do on an assignment is showing information about registered users in a table.我在作业中需要做的一件事是在表格中显示有关注册用户的信息。 It has to be sorted in different ways by clicking buttons (gender, first name, last name, ...).必须通过单击按钮(性别、名字、姓氏……)以不同的方式对其进行排序。

I'm writing this in PHP.我在 PHP 中写这个。 The only thing I came up with is working with different pages, but I would get about 20 different pages.我唯一想到的是使用不同的页面,但我会得到大约 20 个不同的页面。

What would be the best way to implement this?实现这一点的最佳方法是什么? A different page for every order type?每种订单类型都有不同的页面?
I've never worked with jQuery, but it would seem it would be good for this sort of thing?我从未使用过 jQuery,但它似乎对这类事情有好处?

Usually this is done with a database, in which case, you can use a language like MySQL to ORDER BY , and this will do it for you.通常这是通过数据库完成的,在这种情况下,您可以使用 MySQL 之类的语言来ORDER BY ,这将为您完成。

If you're in PHP only (per your question), try using one of the many-available PHP Array sort options here .如果您仅在 PHP 中(根据您的问题),请尝试在此处使用众多可用的 PHP 数组排序选项之一。

a good plugin for jQuery is TableSorter jQuery 的一个很好的插件是TableSorter

Here is an example of javascript and php combination for simple values processing:下面是 javascript 和 php 组合用于简单值处理的示例:

make a your_file.php, insert this code, and upload to your folder.制作 your_file.php,插入此代码,然后上传到您的文件夹。 (using this sortable.js script ) (使用这个sortable.js脚本)

<html><head>
<script src="sorttable.js"></script>

<style>
tbody tr td {color:green;border-right:1px solid;width:200px;}
</style>
</head><body>

<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');

if (!empty($_POST['myFirstvalues'])) 
{ $First = explode("\r\n",$_POST['myFirstvalues']); $Second = explode("\r\n",$_POST['mySecondvalues']);}

?>

</br>Hi User. PUT your values</br></br>
<form action="" method="POST">
projectX</br>
<textarea cols="20" rows="20" name="myFirstvalues" style="width:200px;background:url(untitled.PNG);position:relative;top:19px;Float:left;">
<?php foreach($First as $vv) {echo $vv."\r\n";}?>
</textarea>

The due amount</br>
<textarea cols="20" rows="20" name="mySecondvalues" style="width:200px;background:url(untitled.PNG);Float:left;">
<?php foreach($Second as $vv) {echo $vv."\r\n";}?>
</textarea>
<input type="submit">
</form>

<table class="sortable" style="padding:100px 0 0 300px;">
<thead style="background-color:#999999; color:red; font-weight: bold; cursor: default;  position:relative;">
  <tr><th>ProjectX</th><th>Due amount</th></tr>
</thead>
<tbody>

<?php
foreach($First as $indx => $value) {
    echo '<tr><td>'.$First[$indx].'</td><td>'.$Second[$indx].'</td></tr>';
}
?>
</tbody>
<tfoot><tr><td>TOTAL  = &nbsp;<b>111111111</b></td><td>Still to spend  = &nbsp;<b>5555555</b></td></tr></tfoot></br></br>
</table>
</body>
</html>

The HTML would look like this: HTML 看起来像这样:

<th><a href="mypage.php?sort=name">name:</a></th>
<th><a href="mypage.php?sort=price">price:</a></th>
<th><a href="mypage.php?sort=mark">mark:</a></th>
<th><a href="mypage.php?sort=id">id:</a></th>

php Source code php 源代码

<?php
$sql = "SELECT name, price, id, mark, value FROM cpu";

if ($_GET['sort'] == 'price')
{
    $sql .= " ORDER BY price";
}
elseif ($_GET['sort'] == 'name')
{
    $sql .= " ORDER BY name";
}
elseif ($_GET['sort'] == 'mark')
{
    $sql .= " ORDER BY mark";
}
elseif($_GET['sort'] == 'id')
{
    $sql .= " ORDER BY id";
}
$result = $conn->query($sql);
?>

Javascript sort table by column value html with PHP Javascript 按列值排序表 html 和 PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM