如何添加对数据进行排序的按钮或链接?

发布于 2024-12-29 10:26:14 字数 1221 浏览 0 评论 0原文

在以下代码中,如何添加按“名称”对数据进行排序的按钮或链接?

以下代码只是一个示例,是我从 http://fwebde.com 找到的代码的修改版本/php/sqlite-php/ 。代码的结构非常有趣,我想用排序元素来改进它。

谢谢!

<?php
$db = new PDO('sqlite:db.db');
if (isset($_POST['name']) && isset($_POST['message'])) {
try {
    $stmt = $db->prepare("INSERT INTO messages (name, message) VALUES (:name, :message);");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':message', $message);

    $title = $_POST['name'];
    $content = $_POST['message'];
    $stmt->execute();
} catch (Exception $e) {
    die ($e);
}
}try {
$posts = $db->prepare('SELECT * FROM messages;');
$posts->execute();
} catch (Exception $e) {
die ($e);
}

?>

<?php while ($post = $messages->fetchObject()): ?> 
 <?php echo $post->name ?>
 <?php echo $post->message ?>
<?php endwhile; ?>

<form action="" method="post">
    <label for="name">Name:</label>
    <input type="text" name="name" />
    <textarea name="message" rows="8" cols="50"></textarea>
    <input type="submit" name="submit" value="Submit" />
</form>

In following code, how can I add a button or a link that sorts the data by 'name'?

The following code is just an example, a modified version of code that I found from http://fwebde.com/php/sqlite-php/ . The structure of the code is so enjoyable that I would like to improve it with a sorting-element.

Thanks!

<?php
$db = new PDO('sqlite:db.db');
if (isset($_POST['name']) && isset($_POST['message'])) {
try {
    $stmt = $db->prepare("INSERT INTO messages (name, message) VALUES (:name, :message);");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':message', $message);

    $title = $_POST['name'];
    $content = $_POST['message'];
    $stmt->execute();
} catch (Exception $e) {
    die ($e);
}
}try {
$posts = $db->prepare('SELECT * FROM messages;');
$posts->execute();
} catch (Exception $e) {
die ($e);
}

?>

<?php while ($post = $messages->fetchObject()): ?> 
 <?php echo $post->name ?>
 <?php echo $post->message ?>
<?php endwhile; ?>

<form action="" method="post">
    <label for="name">Name:</label>
    <input type="text" name="name" />
    <textarea name="message" rows="8" cols="50"></textarea>
    <input type="submit" name="submit" value="Submit" />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2025-01-05 10:26:14
<?php
    $db = new PDO('sqlite:db.db');
    if (isset($_POST['submit']) && isset($_POST['name']) && isset($_POST['message'])) {
    try {
     //Insert data
    } catch (Exception $e) {
        die ($e);
    }
    }
    try {
        $order = "";
        if(isset($_POST['sort'])){
            $order = " ORDER BY `name`"; // Order list by name
            $orderFlag = 1;
            if(isset($_POST['order']) && $_POST['order'] == 1){
                 $order .= " DESC";
                 $orderFlag = 0;
            }

         }
        $posts = $db->prepare("SELECT * FROM `messages`" . $order  . ";");
        $posts->execute();
    } catch (Exception $e) {
        die ($e);
    }

    ?>

    <?php while ($post = $posts ->fetchObject()): ?> 
     <?php echo $post->name ?><?php echo $post->message ?><br/>
    <?php endwhile; ?>

    <form action="" method="post">
        <!--pushing this button sort by name -->
        <input type="submit" name="sort" value="Sort by name" />
        <input type="hidden" name="order" value="<?=$orderFlag?>" />

        <label for="name">Name:</label>
        <input type="text" name="name" />
        <textarea name="message" rows="8" cols="50"></textarea>
        <!--pushing this button insert new data -->
        <input type="submit" name="submit" value="Submit" />
    </form>
<?php
    $db = new PDO('sqlite:db.db');
    if (isset($_POST['submit']) && isset($_POST['name']) && isset($_POST['message'])) {
    try {
     //Insert data
    } catch (Exception $e) {
        die ($e);
    }
    }
    try {
        $order = "";
        if(isset($_POST['sort'])){
            $order = " ORDER BY `name`"; // Order list by name
            $orderFlag = 1;
            if(isset($_POST['order']) && $_POST['order'] == 1){
                 $order .= " DESC";
                 $orderFlag = 0;
            }

         }
        $posts = $db->prepare("SELECT * FROM `messages`" . $order  . ";");
        $posts->execute();
    } catch (Exception $e) {
        die ($e);
    }

    ?>

    <?php while ($post = $posts ->fetchObject()): ?> 
     <?php echo $post->name ?><?php echo $post->message ?><br/>
    <?php endwhile; ?>

    <form action="" method="post">
        <!--pushing this button sort by name -->
        <input type="submit" name="sort" value="Sort by name" />
        <input type="hidden" name="order" value="<?=$orderFlag?>" />

        <label for="name">Name:</label>
        <input type="text" name="name" />
        <textarea name="message" rows="8" cols="50"></textarea>
        <!--pushing this button insert new data -->
        <input type="submit" name="submit" value="Submit" />
    </form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文