dongtuhe0506 2015-02-12 10:46
浏览 80
已采纳

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

I want to be able to sort my table as ASC in a field when the submit button is clicked.

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.

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.

  • 写回答

4条回答 默认 最新

  • 普通网友 2015-02-12 11:05
    关注

    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);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路