用php过滤搜索结果 [英] filtering search results with php

查看:179
本文介绍了用php过滤搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一组结果,这些结果是从一个多二维数组。目前,数组键是产品的价格,而产品包含另一个数组,其中包含所有产品详细信息。

 键=> ; Item(name => test,foo => bar)

项目我只是按键排序,最小的第一,它列出的产品最小的价格第一。

然而,我想在此基础上,以便当用户看到的结果,他们可以选择其他订购选项,如从下拉框(或类似的东西)列出所有产品的名称,某些制造商,颜色,x,y,z等等。

b
$ b

这是我需要一些指导的地方。我只是不知道如何去做或最好的做法或任何事情。我能想到的唯一方法是通过嵌套数组来命令所有的项目,比如名字,制造商等等,但是我怎样在PHP中做到这一点?



你明白什么即时通讯试图达到(如果不是只是问)。任何有关这个想法,方法或例子的帮助将是伟大的。



感谢您的阅读



PHP5

解决方案

更新的例子在内存数据库中使用了一小段代码:)
PS:本例中的XSS保护根本不需要,因为我将输入检查为布尔值。要以相反顺序查看结果,请指定 order?desc

 < ?php 

/ * XSS保护。 * /
$ _GET = filter_input_array(INPUT_GET,FILTER_SANITIZE_STRING);



$ b $ a

$ b $ C,
C ++,
Clojure,
COBOL,
ColdFusion,
Erlang,
Fortran ,
Groovy,
Haskell,
Java,
JavaScript,
Lisp,
Perl,
PHP,
Python,
Ruby,
Scala,
Scheme
);

函数createTable($ db){
$ db-> exec(CREATE TABLE IF NOT EXISTS tags(id INTEGER PRIMARY KEY,tag TEXT NOT NULL UNIQUE));


函数insertData($ db,$ array){
$ db-> beginTransaction();
$ b foreach($ array as $ elm){
try {
$ stmt = $ db-> prepare(INSERT INTO tags(tag)VALUES(:tag) );
$ stmt-> execute(array(
:tag=> $ elm
));
} catch(PDOException $ e){
/ ***如果失败,回滚事务*** /
$ db-> rollback();
/ ***回显sql语句和错误信息*** /
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);




希望你明白什么即时尝试
实现(if不只是问)。任何帮助
这个想法,方法或
例子将是伟大的。


首先,我有一对夫妇您所说的问题是您正在使用PHP5。你如何检索你的数据(RDBMS)?如果没有,PHP5的默认启用SQLite 。我想你应该至少使用 RDBMS (SQLite / etc)来完成繁重的工作您。



当您学习SQL

a>你不必在PHP中进行任何排序。我觉得这个 PDO教程,同时给你内心如何安全地使用SQL。 SQL很容易受到SQL注入的影响,但是感谢PDO准备好的语句,您不必再担心这个问题。


我有一套从多维数组中抽取
的结果。
目前,数组键是产品的价格
,而产品包含
另一个数组,其中包含所有
产品详细信息。




使用 ORDER BY 进行订购。我会使用数据表来做排序客户端。还保险箱你在服务器上工作(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

解决方案

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.

这篇关于用php过滤搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆