按顺序输出

时间:2013-08-09 17:42:35

标签: php mysql

我有一个表格,可以将我数据库中的所有信息放在页面上。我想添加一个下拉列表,按名字,姓氏,组等顺序说明......

我如何做到这一点?

    <?php $query = "SELECT * FROM contacts WHERE type = 'lead'";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error()); } ?>

    <table>
    <tr><td>ID</td>     <td>Name</td>       <td>Email</td></tr>
    <?php

    while ($row = mysql_fetch_assoc($result))
    {
        echo '<tr>';
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['email'] . '</td>';
        echo '<td>' . '<a href = "/admin/leads/convert-contact.php?id=' . $row[id] . '">Make Contact</a>';
        echo '</tr>';
    }
    echo '</table>';


?>

4 个答案:

答案 0 :(得分:0)

SELECT *
  FROM Whatever
       ORDER BY CASE 
                  WHEN @order =  'first' THEN first_name
                  WHEN @order = 'last' THEN last_name
                  WHEN @order = 'group' THEN group_name
                  ELSE CAST(id AS Varchar)
                END

请注意,所有排序列都必须是类型兼容的(例如,所有字符/ varchars,或所有数字)

答案 1 :(得分:0)

 <form action='database.php' type='post'>
 <select name='orderby'>
 <option value='firstname'>First Name</option>
 <option value='secondname'>Second Name</option>
 <option value='group'> group</option>
 </select>
 </form>
PHP文件中的

if($_POST['orderby']=='firstname') $lastpart= 'ORDER BY firstname';
if($_POST['orderby']=='lastname') $lastpart= 'ORDER BY lastname';
if($_POST['orderby']=='group') $lastpart= 'GROUP BY field';

<?php $query = "SELECT * FROM contacts WHERE type = 'lead' $lastpart"; ----

答案 2 :(得分:0)

也许:

<form action='this page url' type='post'>
    <select name='orderby'>
        <option value='0'>First Name</option>
        <option value='1'>Second Name</option>
        <option value='2'> Name</option>
        <option value='3'> Group</option>
    </select>
    <input type='submit' name='querygroup' value='Order'/>
</form>
<table>
<tr><td>ID</td>     <td>Name</td>       <td>Email</td></tr>

<?php 
    if(isset($_POST['querygroup'])){
        if(is_numeric($_POST['orderby'])){
            $query = "SELECT * FROM contacts WHERE type = 'lead' ORDER BY CASE '".$_POST['orderby']."' WHEN '0' THEN field_name WHEN '1' THEN field_name [other options] ELSE field_name END";
            $result = mysql_query($query);
            if (!$result) {
                die('Invalid query: ' . mysql_error()); 
            }

            while ($row = mysql_fetch_assoc($result)){
                echo '<tr>';
                echo '<td>' . $row['id'] . '</td>';
                echo '<td>' . $row['name'] . '</td>';
                echo '<td>' . $row['email'] . '</td>';
                echo '<td>' . '<a href = "/admin/leads/convert-contact.php?id=' . $row[id] . '">Make Contact</a>';
                echo '</tr>';
            }
        }
    }
?>
</table>

答案 3 :(得分:0)

一点点AJAX可以使它成为可重新排序的输出。从下面的例子可以看出,并不困难。

以下代码将在页面上放置一个下拉列表,并允许用户选择排序顺序。选择后,排序后的结果将显示在div中。然后,用户可以选择不同的排序顺序,这些结果将显示在div中。

HTML:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {

                $('#mysel').change(function() {
                    var sby = $(this).val();
                    //alert(sby);
                    $.ajax({
                        type: "POST",
                        url: "another_php_file.php",
                        data: 'sortby=' + sby,
                        success: function(thedata) {
                            $("#results").html(thedata) ;
                        }
                    });
                });

            }); //END $(document).ready()

        </script>
    </head>
<body>

    <div id="results"></div>
    <select id="mysel">
        <option value = '0'>Choose One</option>
        <option value = 'last'>Sort by Last Name</option>
        <option value = 'first'>Sort by First Name</option>
    </select>


</body>
</html>

<强> another_php_file.php

<?php

    $order = $_POST['sortby'];

    // Do your database login here

    $output = '<table><tr><td>ID</td><td>Name</td><td>Email</td></tr>';

    $result = mysql_query("SELECT * FROM contacts WHERE type = 'lead' ORDER BY '$order'");
    $num_rows = mysql_num_rows($result);

    while ($row = mysql_fetch_assoc($result)) {
        $output .= '<tr>';
        $output .= '<td>' . $row['id'] . '</td>';
        $output .= '<td>' . $row['name'] . '</td>';
        $output .= '<td>' . $row['email'] . '</td>';
        $output .= '<td>' . '<a href = "/admin/leads/convert-contact.php?id=' . $row[id] . '">Make Contact</a>';
        $output .= '</tr>';
    }
    $output .= '</table>';

    echo $output;

如果您不熟悉AJAX,这里有一些非常简单的示例可以帮助您了解它。它们非常值得玩15或20分钟。

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1