从循环PHP中获取变量值

时间:2013-05-23 03:15:53

标签: php for-loop

当我使用PHP进行循环时,我遇到了一些问题。代码如下:

$subsel = mysql_query ("SELECT * FROM ar_subkategori, ar_subkategori_bhs WHERE ar_subkategori.id_subkategori = ar_subkategori_bhs.id_subkategori AND id_bahasa = 1 AND status = 'y'");
    while ($getsubsel = mysql_fetch_array($subsel)){
        if($getsubsel['id_subkategori_induk'] == 0){
            for($d = 0; $d < $cnt; $d++){
                $fetchit = mysql_query("SELECT * FROM ar_subkategori WHERE id_subkategori = $detail_sub[$d]");
                $datafetch = mysql_fetch_array($fetchit);
                $id_values = $datafetch['id_subkategori'];
                if ($id_values == $getsubsel['id_subkategori']){
                    echo "<input type='checkbox' name='checkbox[]' id='checkbox' value='$getsubsel[id_subkategori]' checked> <span style='font-size:10px; color:#FFA412;'>[ID: $getsubsel[id_subkategori]]</span> $getsubsel[nama_subkategori]</br>";
                }
            }
            if ($id_values != $getsubsel['id_subkategori']){
                echo "<input type='checkbox' name='checkbox[]' id='checkbox' value='$getsubsel[id_subkategori]'> <span style='font-size:10px; color:#FFA412;'>[ID: $getsubsel[id_subkategori]]</span> $getsubsel[nama_subkategori]</br>";
            }
        } else {
            for($d = 0; $d < $cnt; $d++){
                $fetchit = mysql_query("SELECT * FROM ar_subkategori WHERE id_subkategori = $detail_sub[$d]");
                $datafetch = mysql_fetch_array($fetchit);
                $id_values = $datafetch['id_subkategori'];
                if ($id_values == $getsubsel['id_subkategori']){
                    echo "<input type='checkbox' name='checkbox[]' id='checkbox' value='$getsubsel[id_subkategori]' checked> <span style='font-size:10px; color:#FFA412;'>[ID: $getsubsel[id_subkategori]]</span> $getsubsel[nama_subkategori] <span style='color:#FFA412; font-style:italic; font-size:12px;'>*Ini adalah sub dari ID: $getsubsel[id_subkategori_induk]</span></br>";
                }               
            }
            if ($id_values != $getsubsel['id_subkategori']){
                echo "<input type='checkbox' name='checkbox[]' id='checkbox' value='$getsubsel[id_subkategori]'> <span style='font-size:10px; color:#FFA412;'>[ID: $getsubsel[id_subkategori]]</span> $getsubsel[nama_subkategori] <span style='color:#FFA412; font-style:italic; font-size:12px;'>*Ini adalah sub dari ID: $getsubsel[id_subkategori_induk]</span></br>";
            }
        }
    }

here从该代码中捕获。

我的问题是,如何消除多重/双重数据显示?非常感谢你帮助我。

1 个答案:

答案 0 :(得分:0)

您需要将''放在value='$getsubsel[id_subkategori]'这里 - value='$getsubsel[\'id_subkategori\']'以使其正常工作。

不要坚持mysql_*学习PDO或转到mysqli_*作为旁注。