i’m using a MVC style system for my code and i need some help.
Now what i need to do is display a table with parent and child category but the child categories need to be indented.
The problem i am having is creating the dynamic array for the child categories part.
The function i have takes two parameters a ID and a ParentID
The above code will get the parent categories - so my idea is to call the function again in the same function and carry the parents id into the function
$link = dbconnect();
$fullArry = array();
$query = mysqli_query($link, "Query String");
$queryNumRows = mysqli_num_rows($query);
if($queryNumRows > 0){
while($queryResults = mysqli_fetch_assoc($query)){
//Build array
//Call the function
$childArry = categories(/*include the in and parent id here*/);
}
}
return $fullArry;
And append the $childArry to the existing one.
is there a better way of doing this or am i on the right track?
Thanks for reading any replays will be great.
Then, when you loop, you check if the “current parent” was already displayed. If not, it’s a new parent so you need to display the “parent”, then all the children until a new parent shows up.
The table structure has to grow according to the array structure, the above is for two levels deep but i need the table to grow as far down as they want for example if i wanted to have child categories 5 deep then the table has to grow to that size without having to change any code.
But i think i have a plan on how to do what i want.
i’m thinking of making my array structure as follows:
parent.id#1 =>
name => parent.name
order => parent.order
children =>
child.id =>
name => child.name
order => child.order
level => child.level
child.id =>
name => child.name
order => child.order
level => child.level
parent.id#2 =>
name => parent.name
order => parent.order
children =>
child.id =>
name => child.name
order => child.order
level => child.level
child.id =>
name => child.name
order => child.order
level => child.level
...etc
so all the child categories are now in a flat array structure and i use the “level” will tell me how meany indentations i have to apply to that child category in the table.
Let me know what you think and if it’s a good idea.
Thanks for the replay have a great day.
Well, that’s a case where OOP would be easier to work with IMO. You could have an object like this (it’s pseudo code) :
class Category {
$children (list of "Category")
$title
Category($title, $level, $id);
getChildren();
addChild();
}
Also, getting a recursive structure like this is a little bit tricky with PHP & mySQL, but I think that you should be able to do it with one SQL query if you use a stored procedure.
Do a search on Google for “mysql tree structure”. I found this article which seems nice: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
The links you gave me are amazing, it is things like this that make me love coding - when you have no idea of what to do and then someone has the answer to your questions and it’s so simple, i think the links you gave will work i’m just looking up a couple of things so that i understand it currently.