ds342222 2011-01-28 20:17
浏览 51
已采纳

用php过滤搜索结果

Cant really find any useful information on this through Google so hope someone here with some knowledge can help.

I have a set of results which are pulled from a multi dimensional array. Currently the array key is the price of a product whilst the item contains another array which contains all the product details.

key=>Item(name=>test, foo=>bar)

So currently when I list the items I just order by the key, smallest first and it lists the products smallest price first.

However I want to build on this so that when a user sees the results they can choose other ordering options like list all products by a name, certain manufacturer, colour, x ,y ,z etc etc from a drop down box(or something similar)

This is where I need some guidance. Im just not sure how to go about it or best practise or anything. The only way I can think of is to order all the items by the nested array eg by the name, manufacturer etc. but how do I do that in PHP?

Hope you understand what im trying to achieve(if not just ask). Any help on this with ideas, approaches or examples would be great.

Thanks for reading

p.s Im using PHP5

  • 写回答

3条回答 默认 最新

  • dttvb115151 2011-01-28 21:56
    关注

    Updated example with a little snippet using in memory database :) P.S: the XSS protection in this example is not needed at all because I check the input as boolean value. To see results in reverse order you specify order?desc

    <?php
    
    /* XSS-protection. */
    $_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
    
    $array = array(
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    );
    
    function createTable($db) {
        $db->exec("CREATE TABLE IF NOT EXISTS tags (id INTEGER PRIMARY KEY, tag TEXT NOT NULL UNIQUE)");
    }
    
    function insertData($db, $array) {
        $db->beginTransaction();
    
        foreach($array as $elm) {
            try {
                $stmt = $db->prepare("INSERT INTO tags (tag) VALUES (:tag)");
                $stmt->execute(array(
                    ":tag" => $elm
                ));
            } catch(PDOException $e) {
                /*** roll back the transaction if we fail ***/
                $db->rollback();
                /*** echo the sql statement and error message ***/
                echo $sql . '<br />' . $e->getMessage();
            }
        }
    
        $db->commit();
    }
    
    $db = new PDO('sqlite::memory:');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    //
    createTable($db);
    insertData($db, $array);
    
    $order = "ASC";
    if (strtoupper($_GET['order']) == "DESC") {
        $order = "DESC";
    }
    
    $stmt = $db->prepare("SELECT * FROM tags ORDER BY tag $order");
    $stmt->execute();
    
    $data = array();
    while($row = $stmt->fetch()) {  
        $data[] = array($row['tag']);
    }
    
    echo json_encode($data);
    

    Hope you understand what im trying to achieve(if not just ask). Any help on this with ideas, approaches or examples would be great.

    First I have a couple of questions you are saying that you are using PHP5. How do you retrieve your data(RDBMS)? If not, PHP5 has SQLite enabled by default. I think you should be using at least a RDBMS(SQLite/etc) to do the heavy lifting for you.

    When you learn SQL you don't have to any sorting in PHP. I think this PDO tutorial while give you insides how to use SQL while doing it safely. SQL is vulnerable to SQL-injections but thanks to PDO's prepared statements you don't have to worry about that anymore.

    I have a set of results which are pulled from a multi dimensional array. Currently the array key is the price of a product whilst the item contains another array which contains all the product details.

    Use ORDER BY to order. I would use a datatable to do the sorting client-side. Also safes you to do work on the server(PHP). You could for example look at YUI2's datatable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测