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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里