SELECT DISTINCT或array_unique不使用我的数据

时间:2015-02-27 09:38:43

标签: php mysql select

使用PHP,我生成以下SQL语句(ID是动态的,具体取决于所选的下拉列表:

$makeid = intval($_POST['make_id']);

//SELECT partshop_id FROM partshop2categories WHERE category_id = $makeid
$pcats = $objDb->select("partshop2categories", "partshop_id", " category_id=" . $makeid);

foreach ($pcats as $p) {
    // SELECT Brand FROM partshop WHERE id = $p['partshop_id']
    $prods = $objDb->select("partshop", "Brand", " id=" . $p['partshop_id']);
    foreach ($prods as $b) {

        // SELECT DISTINCT id, text FROM Brand_type WHERE ID = $b['Brand']
        $brands = $objDb->selectDistinct("Brand_type", "id, text", "id=" . $b['Brand']." GROUP BY text");

        $brands = array_unique($brands);

        foreach ($brands as $brand) {

            $htmlResponse .= '<option value="' . $brand['id'] . '">' . $brand['text'] . '</option>';
        }

        echo($htmlResponse);
    }
}

此代码然后在我的选择中产生以下值:

<option value="146">AGV</option>
<option value="134">DAYTONA</option>
<option value="134">DAYTONA</option>

当然SELECT DISTINCT应该删除这些重复项?这是它在var_dump上给我的数组:

array(1) { [0]=> array(2) { ["id"]=> string(3) "146" ["text"]=> string(3) "AGV" } }

array(1) { [0]=> array(2) { ["id"]=> string(3) "134" ["text"]=> string(7) "DAYTONA" } }

array(1) { [0]=> array(2) { ["id"]=> string(3) "134" ["text"]=> string(7) "DAYTONA" } }

1 个答案:

答案 0 :(得分:0)

在工作同事的帮助下,它现在正在工作!这是修改后的代码:

$makeid = intval($_POST['make_id']);

$pcats = $objDb->select("partshop2categories", "partshop_id", " category_id=" . $makeid);
$arrBrands = array();
$htmlResponse = '';
foreach ($pcats as $p) {
    $prods = $objDb->selectDistinct("partshop", "Brand", " id=" . $p['partshop_id'] . " GROUP BY id");
    foreach ($prods as $b) {

        $brands = $objDb->selectDistinct("Brand_type", "id, text", "id=" . $b['Brand']." GROUP BY id");

        foreach ($brands as $brand) {
        if(!in_array($brand['text'], $arrBrands))
        {
            $arrBrands[$brand['id']] = $brand['text'];
        }

        }

    }
}

foreach($arrBrands as $key=>$brand)  {
  $htmlResponse .= '<option value="' . $key . '">' . $brand . '</option>';
}

echo($htmlResponse); 

为品牌创建了一个新阵列。感谢大家的帮助!