PHP 单击提交按钮时排序 table
PHP sort table when submit button is clicked
我希望能够在单击提交按钮时在字段中将我的 table 排序为 ASC。
我使用数组函数从 SQL 数据库中获取信息,我尝试制作排序函数,但是当我单击 'submit' 时没有任何反应。
这是我创建提交按钮的尝试:
<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";
}
当我点击提交时 table 保持不变。
这是我的 table 连接信息
$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);
}
?>
我过去问过类似的问题,但我认为由于缺乏完整的信息,他们误解了。
将 ORDER BY
子句添加到 "table connect information" 中的 SQL 语句。
您必须更改您的原始查询。
$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);
}
?>
试试这个代码
$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);
}
?>
您定义您的操作,当提交按钮被按下时,但实际上并不使用它。表格后不需要 php
部分。
尝试编辑您的 table 连接信息:
...
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);
...
我希望能够在单击提交按钮时在字段中将我的 table 排序为 ASC。
我使用数组函数从 SQL 数据库中获取信息,我尝试制作排序函数,但是当我单击 'submit' 时没有任何反应。
这是我创建提交按钮的尝试:
<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";
}
当我点击提交时 table 保持不变。
这是我的 table 连接信息
$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);
}
?>
我过去问过类似的问题,但我认为由于缺乏完整的信息,他们误解了。
将 ORDER BY
子句添加到 "table connect information" 中的 SQL 语句。
您必须更改您的原始查询。
$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);
}
?>
试试这个代码
$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);
}
?>
您定义您的操作,当提交按钮被按下时,但实际上并不使用它。表格后不需要 php
部分。
尝试编辑您的 table 连接信息:
...
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);
...