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

时间:2015-02-12 10:46:54

标签: php mysql arrays sorting html-table

我希望能够在单击提交按钮时在字段中将我的表排序为ASC。

我使用数组函数从SQL数据库中获取信息,我尝试过编写排序功能但是当我点击“提交”时没有任何反应。

这是我尝试创建提交按钮:

 <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";
   }

当我点击提交时,表格保持不变。

这是我的表连接信息

$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);
}
?>

我过去曾问过类似的问题,但我认为由于缺乏完整的信息而误解了。

4 个答案:

答案 0 :(得分:1)

您必须更改原始查询。 $sql = "SELECT * FROM classes";

让我们总结一下:

您的表格:

<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

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

您的展示:

$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);
}
?>

答案 1 :(得分:0)

在&#34;表连接信息&#34;中为SQL语句添加ORDER BY子句。

答案 2 :(得分:0)

试试这段代码

$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);
}
?>

答案 3 :(得分:0)

您可以在按下提交按钮时定义操作,但实际上并未使用它。您的表单后不需要php部分。 尝试编辑表格连接信息:

...
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);
...