我想在复选框中显示属于ch_cat表的子类别列。
我在此列出了我的类别表:
<form action="tutorsubject.php">
<?php
$sql = "SELECT * FROM `ch_cat`";
$result = mysql_query($sql);
while( $row = mysql_fetch_assoc($result) ){ ?>
<label>
<input type="checkbox" name="segment[]" value="<?php echo $row['pid']; ?>"> <?php echo $row['p_cat']; ?>
</label>
<?php
}
?>
<input type="submit" value="tutorsubject"/>
</form>
Tutorsubject.php
if (isset($_POST['tutorsubject'])) {
$segment = $_POST['segment'];
print_r($segment);
//how to write query here based on child_cat table.
}
我的问题是:
Array ( [0] => 3 [1] => 13 )
。这里有3和13
child_cat主键传递给sub_cat表。答案 0 :(得分:0)
获取子类别与您在此处获取类别的方式相同,只需循环遍历它。
试试这个:
<?php
if (isset($_POST['tutorsubject'],$_POST['segment'])) {
$segment = $_POST['segment'];
foreach ($segment as $sub_cat)
{
$sql = "SELECT * FROM `sub_cat` WHERE cid = ".$sub_cat;
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
<label>
<input type="checkbox" name="sub_segment[]" value="<?php echo $row['sid']; ?>"> <?php echo $row['sub_cat']; ?>
</label>
<?php
}
}
}
?>