简体   繁体   English

单击提交按钮时的PHP排序表

[英]PHP sort table when submit button is clicked

I want to be able to sort my table as ASC in a field when the submit button is clicked. 我希望能够在单击“提交”按钮时将表按ASC排序。

I have used an array function to get the information from a SQL database, I have tried making a sort function but when I click 'submit' nothing happens. 我曾使用数组函数从SQL数据库获取信息,我曾尝试制作一个排序函数,但是当我单击“提交”时,什么也没发生。

This is my attempt of creating the submit button: 这是我创建提交按钮的尝试:

 <h1> Courses </h1>
<form name="Table Properties" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Order by Week
<button type="submit" name="week" class="button" <?php echo $value='1'; ?>"> Sort Week </button>
</form>
<?php 
if(isset($_POST['week'])){
        $query="SELECT * FROM `classes` ORDER BY `classes`.`week` ASC";
   }

When I click submit the table remains the same. 当我单击提交时,表格保持不变。

Here is my table connect information 这是我的表连接信息

$con = mysql_connect("host", "username", "password");
if (!$con) 
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("username", $con);
$sql = "SELECT * FROM `classes`";
$myData = mysql_query($sql, $con);
echo "<table border=1>
<tr>
<th> Name 1 </th>
<th> Name 2 </th>
<th> Name 3 </th>
<th> Name 4 </th>
</tr>";
while ($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo"<td>" . $record['name 1'] . "</td>";
    echo"<td>" . $record['week'] . "</td>";
    echo"<td>" . $record['name 3'] . "</td>";
    echo"<td>" . $record['name 4'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>

I asked a similar question in the past but I think they misunderstood due to lack of full information. 过去我曾问过类似的问题,但由于缺乏完整的信息,我认为它们被误解了。

You have to alter your original query. 您必须更改原始查询。 $sql = "SELECT * FROM classes";

Let's summarize : 让我们总结一下:

Your form : 您的表格:

<h1> Courses </h1>
<form name="Table Properties" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Order by Week
<button type="submit" name="week" class="button" <?php echo $value='1'; ?>"> Sort Week </button>
</form>

When you submit : 提交时:

<?php

$orderString = ''; // default 
if (isset($_POST['week'])){
    $orderString = "`classes`.`week` ASC";
}

Your display : 您的显示:

$con = mysql_connect("host", "username", "password");
if (!$con) 
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("username", $con);
$sql = "SELECT * FROM `classes`";
// now check order
if ($orderString !== '') {
    // Concatenate
    $sql .= ' ORDER BY ' . $orderString;
}
$myData = mysql_query($sql, $con);
echo "<table border=1>
<tr>
<th> Name 1 </th>
<th> Name 2 </th>
<th> Name 3 </th>
<th> Name 4 </th>
</tr>";
while ($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo"<td>" . $record['name 1'] . "</td>";
    echo"<td>" . $record['week'] . "</td>";
    echo"<td>" . $record['name 3'] . "</td>";
    echo"<td>" . $record['name 4'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>

在“表连接信息”中的SQL语句中添加ORDER BY子句。

try this code 试试这个代码

$con = mysql_connect("host", "username", "password");
if (!$con) 
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("username", $con);
$sql = "SELECT * FROM `classes`";

if(isset($_POST['week'])){
        $sql="SELECT * FROM `classes` ORDER BY `classes`.`week` ASC";
   }

$myData = mysql_query($sql, $con);
echo "<table border=1>
<tr>
<th> Name 1 </th>
<th> Name 2 </th>
<th> Name 3 </th>
<th> Name 4 </th>
</tr>";
while ($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo"<td>" . $record['name 1'] . "</td>";
    echo"<td>" . $record['week'] . "</td>";
    echo"<td>" . $record['name 3'] . "</td>";
    echo"<td>" . $record['name 4'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>

You define your action, when the submit button is pressed, but don't actually use it. 您可以在按下提交按钮时定义您的操作,但实际上并不使用它。 You don't need the php part after your form. 您不需要填写表格后的php部分。 Try to edit your table connect information: 尝试编辑您的表连接信息:

...
mysql_select_db("username", $con);
if(isset($_POST['week'])){
    $sql="SELECT * FROM `classes` ORDER BY `classes`.`week` ASC";
}
else{
    $sql = "SELECT * FROM `classes`";
}
$myData = mysql_query($sql, $con);
...

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

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