Hi there everyone!
I've been using a query loop that I duplicated twice inside itself to build a category list that will list categories two levels under the parent. So I can show categories with this structure:
Parent
.First Level Child 1
.First Level Child 2
..Second Level Child 1
..Second Level Child 2
.First Level Child 3
.First Level Child 4
But I need to modify it so I can have recursive folders any number deep.
My thought is to write a function that will use reuse itself inside itself to keep finding children. I'm really close, but for some reason, I'm coming up with some empty entries.
/* Build the categories */
function cat_array($parent, $level){
global $link;
$q_cats = "SELECT * FROM categories WHERE parent = '$parent' ORDER BY name ASC";
$r_cats = mysqli_query ($link, $q_cats) or die('Catastrophic failure [Super secret code 95162]');
$num_cats=mysqli_num_rows($r_cats);
if($num_cats != 0){
while ($row_cats = mysqli_fetch_assoc ($r_cats)) {
$cat_id = $row_cats['id'];
$name = $row_cats['name'];
$safename = $row_cats['safename'];
$parent = $row_cats['parent'];
$catcounter = mysqli_query($link, "SELECT COUNT(*) AS id FROM links WHERE category = '$cat_id'");
$catnum = mysqli_fetch_array($catcounter);
$catcount = number_format($catnum["id"]);
/* This is supposed to append the data to the end of the cat_array */
if(!ISSET($cat_array)){
$cat_array = array();
}
$cat_array[] = $row_cats;
/* This is supposed to use the function again inside this loop to find the children of the current row */
$cat_array[] = cat_array($cat_id, $level+1);
}
}
return $cat_array;
}
Here's my result array. Notice that after "Loose or Weak Contact at Generator Harness Connector TSB 96-21-4 for 86-93 Bronco", my code began inserting blank array entries. To be clear, it's not a valid result that's showing blank, those blank entries should not be there at all.
Array
(
[0] => Array
(
[id] => 1
[name] => ALTERNATOR, BATTERY & CHARGING
[safename] => alternator-battery-and-charging
[description] => 3G, Dual batteries, isolators and more
[parent] => 0
)
[1] => Array
(
[0] => Array
(
[id] => 3
[name] => ALTERNATOR
[safename] => alternator
[description] =>
[parent] => 1
)
[1] => Array
(
[0] => Array
(
[id] => 35
[name] => Loose or Weak Contact at Generator Harness Connector TSB 96-21-4 for 86-93 Bronco
[safename] => loose-or-weak-contact-at-generator-harness-connector-tsb-96-21-4-for-86-93-bronco
[description] =>
[parent] => 3
)
[1] =>
[2] => Array
(
[id] => 36
[name] => No Crank, Low State Of Battery Charge TSB 91-10-8 for 85-91 Bronco, Bronco II, Econoline, F-150-350 Series, Ranger; 86-91 Aerostar; 88-91 F Super Duty, F47, F-53, F-59; 91 Explorer, etc.
[safename] => no-crank-low-state-of-battery-charge-tsb-91-10-8-for-85-91-bronco-bronco-ii-econoline-f-150-350-series-ranger-86-91-aerostar-88-91-f-super-duty-f47-f-53-f-59-91-explorer-etc-
[description] =>
[parent] => 3
)
[3] =>
)
[2] => Array
(
[id] => 4
[name] => BATTERY & ISOLATOR
[safename] => battery-and-isolator
[description] =>
[parent] => 1
)
[3] =>
[4] => Array
(
[id] => 5
[name] => GENERAL INFORMATION
[safename] => general-information
[description] =>
[parent] => 1
)
[5] =>
)
[2] => Array
(
[id] => 2
[name] => AUDIO AND VIDEO
[safename] => audio-and-video
[description] => CBs, antennas, radios and amps
[parent] => 0
)
[3] =>
[4] => Array
(
[id] => 6
[name] => BODY
[safename] => body
[description] => Paint, repairs, tailgate and wiring
[parent] => 0
)
[5] =>
[6] => Array
(
[id] => 7
[name] => BRAKE SYSTEM
[safename] => brake-system
[description] => 4WABS, RABS, Self test
[parent] => 0
)
[7] =>
[8] => Array
(
[id] => 8
[name] => CALCULATORS & CONVERTERS
[safename] => calculators-and-converters
[description] => Axle ratio, HP
[parent] => 0
)
)
)
Could someone let me know what I might be able to do to this function to get the result I need?
Thanks for your time!