父查询sql查询结果

时间:2012-09-18 18:05:02

标签: php mysql sql

需要一些帮助来完成我的代码它的作用是在我的数据库的表中搜索指向parent_sections = 2的所有部分,然后在select标签中打印结果我想要一些帮助查询sql查询如果parent_sections = 2有孩子的话,如果是这样的话,无论如何要用optgroup样式打印它们,请检查下面的图片,他们会解释我需要的东西。

PHP& SQL代码:

<?php
include('../application_top.php');

function get_title_sections($id){
$sections_query = tep_db_query("select title_sections_detail from  sections_detail  where  id_sections = '" . (int)$id . "' and id_lang='0' order by title_sections_detail");
$result='';
    while ($sections = tep_db_fetch_array($sections_query)) {
 $result=$sections['title_sections_detail']; 
    }
    return $result;
}
?>


<form>
<select name="sections"> 
<option selected="selected" value="">Please choose an option</option>                           
        <?php   
        $sections_sql=tep_db_query("select p.id_sections, d.title_sections_detail as title from sections p, sections_detail d where  p.id_sections=d.id_sections and d.id_lang='0' and p.status_sections='1' and p.parent_sections='2'  order by d.title_sections_detail asc");         

    while ($sections = tep_db_fetch_array($sections_sql)) {
    $id_sec=$sections['id_sections'];
    $title_sec=get_title_sections($id_sec);
 ?>     
    <option value="<?php echo $id_sec ?>" ><?php echo $title_sec ?></option>

<?php }?>
</select>
</form>

SQL表部分: TABLE sections

SQL表sections_detail: TABLE sections_detail

结果:

RESULT

结果我需要:

RESULT I NEED

0 个答案:

没有答案