I'm trying to loop once with the $cat_id
to grab its id
and category
values for the $url
value and then add the id
and category
values to the $parent_cat_id
and $sub_cat_name
arrays and then loop the $parent_cat_id
value just added to the array to find its id
and category
values and add them to the arrays until all the $url
values have been looped. But I can only get the first $url
value to loop and not the rest of the $url
values to loop can someone help me correct this problem?
PHP code.
$parent_cat_id = array();
$sub_cat_name = array();
$cat_id = 23;
for ($i = 1; isset($_GET['sub'.$i]); ++$i) {
$url[$i] = '&sub' . $i . '=' . $_GET['sub'.$i];
if(isset($cat_id)){
$dbc = mysqli_query($mysqli, "SELECT id, category FROM categories WHERE url = '" . $url[$i] . "' AND parent_id = '" . $cat_id . "'");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($dbc)){
$parent_cat_id[] = $row['id'];
$sub_cat_name[] = $row['category'];
}
}
} else {
$dbc = mysqli_query($mysqli, "SELECT id, category FROM categories WHERE url = '" . $url[$i] . "' AND parent_id = '" . $parent_cat_id[$i] . "'");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($dbc)){
$parent_cat_id[] = $row['id'];
$sub_cat_name[] = $row['category'];
}
}
}
}
Current output.
$parent_cat_id Array ( [0] => 77 )
$sub_cat_name Array ( [0] => A1 )
Expected Output.
$parent_cat_id Array ( [0] => 77 [1] => 78 [2] => 97 [3] => 100 )
$sub_cat_name Array ( [0] => A1 [1] => A2 [2] => B4 [3] => CD )