带有名称的按位值数组,如何检查值

时间:2013-09-18 01:15:20

标签: php sql arrays pdo bit-manipulation

我有一个带有名称列表的数据库表,其中有按位值关联,我可以创建一个没有任何实际问题的php数组,但我需要能够检查另一个查询它所映射的结果条件中的哪一个数组,然后显示相关的名称。

Conditions Bitwise table

$DBH = new PDO("dblib:host=$myServer;dbname=$SystemDB", $myUser, $myPass);
$DBH1 = new PDO("dblib:host=$myServer;dbname=$TaxiHistoryDB", $myUser, $myPass);


$sth = $DBH->prepare("SELECT dbo.Conditions.Name, dbo.Conditions.ConditionValue     
                      FROM dbo.Conditions 
                      WHERE dbo.Conditions.ConditionID > 0");
$sth->execute();

$ConditionsArray = $sth->fetchAll();

$STH1 = $DBH1->query("SELECT dbo.tblBooking.Conditions 
                      FROM dbo.tblBooking  
                      WHERE dbo.tblBooking.BookingID = '36607762'");

$STH1->setFetchMode(PDO::FETCH_LAZY);

while($row1 = $STH1->fetch()){

$condition = $row1->Conditions;


}//end while

第一个查询给出了一个如下所示的结果:

Array ( [0] => Array ( [Name] => [0] => [ConditionValue] => 1 [1] => 1 ) 
[1] => Array ( [Name] => [0] => [ConditionValue] => 2 [1] => 2 ) 
[2] => Array ( [Name] => [0] => [ConditionValue] => 4 [1] => 4 ) 
[3] => Array ( [Name] => [0] => [ConditionValue] => 8 [1] => 8 ) 
[4] => Array ( [Name] => [0] => [ConditionValue] => 16 [1] => 16 ) 
[5] => Array ( [Name] => [0] => [ConditionValue] => 32 [1] => 32 ) 
[6] => Array ( [Name] => [0] => [ConditionValue] => 64 [1] => 64 ) 
[7] => Array ( [Name] => [0] => [ConditionValue] => 128 [1] => 128 ) 
[8] => Array ( [Name] => Exterior Hoist [0] => Exterior Hoist [ConditionValue] => 256 [1] => 256 ) 
[9] => Array ( [Name] => [0] => [ConditionValue] => 512 [1] => 512 ) 
[10] => Array ( [Name] => [0] => [ConditionValue] => 1024 [1] => 1024 ) 
[11] => Array ( [Name] => [0] => [ConditionValue] => 2048 [1] => 2048 ) 
[12] => Array ( [Name] => [0] => [ConditionValue] => 4096 [1] => 4096 ) 
[13] => Array ( [Name] => [0] => [ConditionValue] => 8192 [1] => 8192 ) 
[14] => Array ( [Name] => [0] => [ConditionValue] => 16384 [1] => 16384 ) 
[15] => Array ( [Name] => [0] => [ConditionValue] => 32768 [1] => 32768 ) 
[16] => Array ( [Name] => Parcel [0] => Parcel [ConditionValue] => 65536 [1] => 65536 ) 
[17] => Array ( [Name] => Cheques [0] => Cheques [ConditionValue] => 131072 [1] => 131072 )
[18] => Array ( [Name] => OuterArea [0] => OuterArea [ConditionValue] => 262144 [1] => 262144 ) 
[19] => Array ( [Name] => [0] => [ConditionValue] => 524288 [1] => 524288 ) 
[20] => Array ( [Name] => [0] => [ConditionValue] => 1048576 [1] => 1048576 ) 
[21] => Array ( [Name] => V [0] => V [ConditionValue] => 2097152 [1] => 2097152 )
[22] => Array ( [Name] => Wheelchair [0] => Wheelchair [ConditionValue] => 4194304 [1] => 4194304 ) 
[23] => Array ( [Name] => M50 [0] => M50 [ConditionValue] => 8388608 [1] => 8388608 ) 
[24] => Array ( [Name] => Executive Car (Silver) [0] => Executive Car (Silver) [ConditionValue] => 16777216 [1] => 16777216 ) 
[25] => Array ( [Name] => Two M50s [0] => Two M50s [ConditionValue] => 33554432 [1] => 33554432 ) 
[26] => Array ( [Name] => Special [0] => Special [ConditionValue] => 67108864 [1] => 67108864 ) 
[27] => Array ( [Name] => Animal [0] => Animal [ConditionValue] => 134217728 [1] => 134217728 ) 
[28] => Array ( [Name] => COD Parcel [0] => COD Parcel [ConditionValue] => 268435456 [1] => 268435456 ) 
[29] => Array ( [Name] => 9 seater [0] => 9 seater [ConditionValue] => 536870912 [1] => 536870912 ) 
[30] => Array ( [Name] => 6 seater [0] => 6 seater [ConditionValue] => 1073741824 [1] => 1073741824 ) 
[31] => Array ( [Name] => 7 seater [0] => 7 seater [ConditionValue] => 2147483648 [1] => 2147483648 ) 
[32] => Array ( [Name] => Wagon [0] => Wagon [ConditionValue] => 4294967296 [1] => 4294967296 ) 
[33] => Array ( [Name] => Maxi10str [0] => Maxi10str [ConditionValue] => 8589934592 [1] => 8589934592 ) 
[34] => Array ( [Name] => Bike [0] => Bike [ConditionValue] => 17179869184 [1] => 17179869184 ) 
[35] => Array ( [Name] => NonMaxi [0] => NonMaxi [ConditionValue] => 34359738368 [1] => 34359738368 ) 
[36] => Array ( [Name] => NonMaxiOrMulti [0] => NonMaxiOrMulti [ConditionValue] => 68719476736 [1] => 68719476736 ) 
[37] => Array ( [Name] => [0] => [ConditionValue] => 137438953472 [1] => 137438953472 ) 
[38] => Array ( [Name] => Towbar [0] => Towbar [ConditionValue] => 274877906944 [1] => 274877906944 ) 
[39] => Array ( [Name] => NO DISPATCH [0] => NO DISPATCH [ConditionValue] => 549755813888 [1] => 549755813888 ) )

我已经阅读了这里的一些帖子和其他几个帖子,我理解按位操作是如何工作的,但是我会因为语法或过程而迷失方向。实质上,一个工作号码将有0到40个与之相关的条件。

在while循环中使用以下代码

  

echo $ row1->条件;

我得到: 67108864

我知道

代表“特殊”

我只是不确定如何将第二个查询的结果与第一个查询所做的数组进行比较。

感谢。

1 个答案:

答案 0 :(得分:0)

我想你正在寻找类似的东西:

while($bookingRow = $STH1->fetch())
{    
    foreach($ConditionsArray as $codeRow)
    {
        if(($bookingRow->Conditions & $codeRow->ConditionValue) > 0)
        {
            printf('Matched Condition Name: %s', $bookingRow->Name);
        }
    }
}

但您可能希望在第二个查询中至少选择一个附加列,以便知道tblBooking表中的哪一行匹配。

另外,如果您的条件代码实际上只等于2 ^ n ,那么存储 n 更高效,而不是浪费所有那些存储零的位。

号码4294967296需要33位。如果您改为存储 n (= 32),则只需要6。

我猜测,如果预订有多个条件,那么它的价值就是各种条件代码的总和,因为将多个(2 ^ n )加在一起(对于不同的< em> n (s)为您提供一个整数值,您可以将其反转回条件代码。

但这违背了数据库的目的。如果真的想要那个,你应该有3个表:一个conditionCode表(用一个简单的递增整数主键索引),一个预订表(索引类似),然后第三个表可能是“bookingCondition” “每个预订的每个条件都有一行。

如果预订1有2个条件,预订2有5个,预订3有1个,你的“bookingCondition”表有8行,看起来像

1 a
1 b
2 c
2 d
2 e
2 f
2 g
3 h

如果字母a-h实际上不是字母,而是整数,则引用conditionCode表的主键。

然后确定特定预订的条件就像一个简单的SQL语句(使用JOIN)一样简单,而不是多个查询和PHP“后处理”

相关问题