简体   繁体   English

使用来自MySQL数据库PHP的计数填充列表

[英]Populate a List with counts from MySQL database PHP

I am trying to create a list based off of results selected with a wildcard from a column in a MySQL database. 我正在尝试根据从MySQL数据库的列中使用通配符选择的结果创建一个列表。 I am able to do it, however it seems very slow, and probably the wrong way. 我能够做到,但是似乎很慢,而且可能是错误的方式。 Here is the code I am tinkering with now 这是我现在正在修改的代码

$optarr = array("leather", "cruise", "tint", "sunroof", "moonroof", "navigation", "antilock");
 foreach ($optarr as $i) {
    $refquery = "SELECT stock, COUNT(stock) FROM vehicle_list AND options LIKE '%$i%'"; 
    $refresult = mysql_query($refquery) or die(mysql_error());
    $row = mysql_fetch_array($refresult);

    if ($row['COUNT(stock)'] != '0') {
    echo "".ucfirst($i)." (".$row['COUNT(stock)'].")<br />";
    }
}

This will output the following 这将输出以下内容

Leather(234)
Cruise(343)
Tint(231)
Sunroof(343)
.....

This list will eventually be around 20-30 different filter options, right now at 4-5 I can see a substantial delay. 该列表最终将包含大约20-30个不同的过滤器选项,现在在4-5,我可以看到一个很大的延迟。 I'm doing something wrong, Just don't know what. 我做错了,只是不知道什么。 Maybe the entire approach? 也许是整个方法?

The options column in the Database is an entire block of text separated by @ symbols so I am using the Like wildcard to extract vehicle options. 数据库中的options列是由@符号分隔的整个文本块,因此我使用Like通配符来提取车辆选项。

Thanks! 谢谢!

Make the database call prior to the foreach loop. 在foreach循环之前进行数据库调用。 This is calling the database on each loop and will be slow as expected. 这在每个循环上都在调用数据库,并且会变慢。

From a normalization and optimization perspective your should place your options in a separate table and then use a table to link the vehicle_list table to your options. 从规范化和优化的角度来看,您应该将选项放置在单独的表中,然后使用表将vehicle_list表链接到选项。

This would allow you to use a join and a group by to get your required result using only one query instead of a load of queries using a double sided like (which is not a good idea even when using indexes). 这样一来,您可以使用联接和分组方式,仅使用一个查询即可获得所需的结果,而不必使用双面查询来获得大量查询(即使使用索引也不是一个好主意)。

http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

You have to join tables 你必须参加表

 $optarr = array("leather", "cruise", "tint", "sunroof", "moonroof", "navigation", "antilock");
     foreach ($optarr as $i) {
    $refquery = "SELECT stock, COUNT(stock)  FROM vehicle_list 
    LEFT JOIN options ON vehicle_list.stock=options.stock LIKE '%$i%'"; 
    $refresult = mysql_query($refquery) or die(mysql_error());
    $row = mysql_fetch_array($refresult);

    if ($row['COUNT(stock)'] != '0') {
    echo "".ucfirst($i)." (".$row['COUNT(stock)'].")<br />";
    }

It seems you are firing a new query for every time present in the $optarr 看来您每次在$optarr出现时都会触发一个新查询

You can try creating a list of conditions first and then fire a single query 您可以先尝试创建条件列表,然后触发一个查询

Something like 就像是

$optarr = array("leather", "cruise", "tint", "sunroof", "moonroof", "navigation", "antilock");
 $strCondition = NULL;
 foreach ($optarr as $i) {
    $strCondition .= ( true == is_null(strCondition ) )? "LIKE '%$i%'" : "OR LIKE '%$i%'"; 
}

if( false != $strCondition ) {
    $refresult = mysql_query( 'SELECT stock, COUNT(stock) FROM vehicle_list AND options ' . $strCondition ) or die(mysql_error());
     while( $row = mysql_fetch_array($refresult) )
     {
         if ($row['COUNT(stock)'] != '0') {
         echo "".ucfirst($i)." (".$row['COUNT(stock)'].")<br />";
     }
}

There could be some syntax errors as I haven't test it :p. 可能有一些syntax errors因为我没有测试过:p。

The above method would give a performance gain , in general, you should store values within tables in the way you wish to retrieve them (I would recommend adding a new column condition only values like 'leather', cruise' ..etc) 上面的方法可以performance gain ,通常,您应该以希望检索它们的方式将值存储在表中(我建议仅添加一个新的列条件,例如“ leather”,“ cruise” .. etc等值)

You should probably normalize your options such that they are in a table related to the vehicle_list table. 您可能应该规范化选项,以使它们位于与vehicle_list表相关的表中。 This will make your life much easier going forward. 这将使您的生活更加轻松。

So say you had a table vehicle_options like this 所以说你有一个这样的表vehicle_options

vehicle_option_id -> primary_key, autoincrement
vehicle_id -> foreign key to vehicle_list table
option -> text string describing option

All three fields would need an index. 所有这三个字段都需要一个索引。

You could then get your count from a single query against this table. 然后,您可以通过针对该表的单个查询获得计数。 You don't even need to query vehicle_list at all in this case. 在这种情况下,您甚至都不需要查询vehicle_list。

SELECT option, COUNT (vehicle_option_id) AS `option_count` FROM
vehicle_options
GROUP BY option

To normalize further, you might even consider having yet another table options that actually contains the description of the available options and having the vehicle_options table simple act as a many-to-many join table. 为了进一步规范化,您甚至可以考虑拥有另一个表选项,该表选项实际上包含可用选项的描述,并且让vehicle_options表简单地充当多对多vehicle_options表。 With this schema your tables would look like this: 使用此模式,您的表将如下所示:

vehicle_options

vehicle_id -> foreign key to vehicle_list table
option_id -> foreign key to option table

You would have a primary key across both fields 您将在两个字段中都有一个主键

options

option_id -> primary_key, autoincrement
option -> text string describing option

You would want an index on option if you are going to use it for grouping/sorting purposes 如果要用于选项分组/排序,则需要索引选项

Your query would then look like this: 您的查询将如下所示:

SELECT o.option AS `option`, COUNT (vo.option_id) AS `option_count`
FROM vehicle_options AS vo
INNER JOIN options AS o
GROUP BY `option`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM