为什么我的“if”声明不起作用?

时间:2014-01-04 07:12:27

标签: php mysql if-statement switch-statement

当我尝试从单独的主产品表中填充9个类别表时,我无法使if语句生效。

这是我试过的代码......没有用这个填充的表。

foreach ($tables as $category_table)
{
    if ($category_table = "my_products_dsdi_apparel")
    {
    $category_products_image = "'%images_products/apparel/%'";
    }
    ...

mysql_query
("
INSERT INTO $category_table
SELECT * FROM zen_products 
WHERE products_image LIKE $category_products_image
ORDER BY products_id ASC
");

}

所以,我尝试了SWITCH功能,它可以工作。

foreach ($tables as $category_table)
{
    switch ($category_table)
    {
    case "my_products_dsdi_apparel":
        $category_products_image = "'%images_products/apparel/%'";
        break;
... 

    default:
        echo "It failed again!";
    }

mysql_query
("
INSERT INTO $category_table
SELECT * FROM zen_products 
WHERE products_image LIKE $category_products_image
ORDER BY products_id ASC
");

}

为什么“if”场景不起作用?

2 个答案:

答案 0 :(得分:5)

use '==' i.e. comparison operator instead of'=' i.e assignment operator.

你的如果会是这样的。

if ($category_table == "my_products_dsdi_collectibles")

答案 1 :(得分:1)

为什么不定义关联数组?

会使代码更具可读性。

修改

列表中的两个以获得想法

$lookup = array(
"my_products_dsdi_apparel" => "'%images_products/apparel/%'",
"my_products_dsdi_at_home" => "'%images_products/at_home/%'",
...
);
$category_products_image = $lookup[$category_table];