doumianfeng6979 2014-08-25 13:31
浏览 38
已采纳

使用JS / jQuery过滤PDO和MySQL查询

Sorry for my ignorance on the lack of knowledge I have on this subject however I cannot find an answer to my question anywhere.

So I have this MySQL table:

Feed_ID Vehicle_ID  FullRegistration    Colour  FuelType    Year    Mileage Bodytype    Doors   Make    Model   Variant EngineSize  Price   PreviousPrice   Transmission    PictureRefs ServiceHistory  PreviousOwners  Description FourWheelDrive  Options Comments    New Used    Site    Origin  V5  Condition   ExDemo  FranchiseApproved   TradePrice  TradePriceExtra ServiceHistoryText  Cap_ID

As you can see each column will contain vehicle data.

I have displayed all of the results in the database using PDO onto my front end, all data is displayed in a listing style similar to Ebay.

Now I need to filter these results however I have noticed that many result filter systems are using JS.

Here are some examples so you get a better idea of what I am talking about:

http://www.autotrader.co.uk/search/used/cars/

http://www.motors.co.uk/search/car/

As you can see all the filters are using JS however I am having a problem understanding how JS is filtering the MySQL query?

I know this question might be a little broad but can someone show me an example of how JS can filter PDO results just like the examples I have shown?

Thanks

  • 写回答

1条回答 默认 最新

  • duanang58939 2014-08-25 13:52
    关注

    The first one uses what I suspect to be a combined method of Javascript and a server-side language (it's hard to prove, because I can't see the server-side code involved). For simplicity, I'll assume this server-side language is PHP, though it could easily not be.

    Basically, all Javascript is doing on the first website is setting cookies and telling you to refresh the page. Once you refresh, PHP fetches the cookies that Javascript set and filters the results from the MySQL query based on those cookies.

    Now, the second one is actually filtering using Javascript, yet at the same time still using PHP (again, it could be any server-side language).

    This is a method called AJAX. It is a function built into Javascript which allows you to fetch another page from Javascript (aka. send and receive an HTTP request).

    The reason this is useful is because once you've changed an option on that page, Javascript can send an HTTP query using AJAX to something like "http://www.motors.co.uk/search/getcarinfo.php?transmission=manual", allowing PHP to fetch a new dataset from MySQL and return it to Javascript (this probably isn't the API entry point that they use, but it has to be somewhere in their Javascript).

    Once Javascript receives the response from that page (usually in JSON or XML form), it can modify HTML to update what's shown on the page.

    To answer your question directly, Javascript doesn't filter the data. MySQL filters the data based on a PHP query, which returns its response to the Javascript. Then, Javascript just puts it on the screen.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波