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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解