Jump to content
Larry Ullman's Book Forums
cantera25

Recursive Functions - Multiple Parents

Recommended Posts

Larry - thanks for your excellent section on recursive functions, I found it really valuable.

 

I'm trying to go one step further -- where items can have multiple parents -- and could really use your help.

 

As an example, I'd like to create a list of technology books by category, where each book can belong to more than one category -- and each category can be both a parent category and a sub-category.

 

Here's an illustration:

 

JavaScript

- JavaScript Patterns

- Object-Oriented JavaScript

 

Ajax

- Ajax Definitive Guide

- Building a Website with Ajax

 

jQuery

- Learning jQuery 1.3

- PHP jQuery Cookbook

 

PHP

- PHP 5 Advanced

- PHP jQuery Cookbook

 

Ajax

- Ajax Definitive Guide

- Building a Website with Ajax

 

XML

- XML Hacks

- No-Nonsense XML

 

As you can see:

 

- The book "PHP jQuery Cookbook" belongs to two categories: PHP and jQuery

 

- The category "Ajax" is both a child of JavaScript and a parent of XML (but XML isn't a child of JavaScript)

 

I've designed the database tables this way:

 

BOOK: book_id, book_title

CATEGORY: category_id, category_name

BOOK_CATEGORY: book_id, category_id

CATEGORY_TREE: parent_category_id, child_category_id

 

Solving this has become a 'white whale' for me, and I'd really appreciate your help. Have you ever seen or written a tutorial on how to handle a scenario like this?

 

Thanks!

Share this post


Link to post
Share on other sites

Interesting question, Ahab. Let me ponder your white whale a bit and see what I come up with. Just out of curiosity, is this something you need to do or are just wanting to see if you can figure out?

Share this post


Link to post
Share on other sites

Thanks Larry - this is strictly for me, definitely not something I'm being paid to code. I'm a beginner struggling to make the jump from theory to practice and, almost invariably, it's this "categorical hierarchy" problem that keeps tripping me up.

 

My current state of thinking is that recursion isn't the answer. It seems like if I structure the data properly and get SQL to return the right arrays, I can solve this more simply and elegantly using only array functions. My hope is that PHP shouldn't care about adjacency or paths or vertices -- instead, it should just check the tables for specific id's and return the corresponding lists as instructed.

 

(Full disclosure: I was pointed to Directed Acyclic Graphs as the solution to my problem, and my brain started leaking. So I may be hastily wishing for a simpler answer.)

Share this post


Link to post
Share on other sites

Yeah, my inclination is that a recursive function isn't necessary either, just a good SQL command or two. Another question I would have is what is the desired output? Like viewing everything at once or just one node or...?

Share this post


Link to post
Share on other sites

Glad to hear I might be on the right track (with my thinking at least). Regarding output, I'd prefer to view everything at once.

 

Right now I'm taking two approaches to trying to solve this:

 

1. Starting in the "middle," I'm writing a big array in PHP modeled after the hierarchy in my original post. From there, I will a.) work out the PHP logic to traverse the array and display the hierarchy, and b.) figure out the database queries that can produce the array.

 

2. Starting incrementally, I'm trying to simply list all of the categories alphabetically with the book titles underneath. Even this has me stumped, however, and searching for "list items by category in php" surprisingly turns up very little (both on the web and in the literature). To get me started, I'm trying to adapt your To-Do List application in Chapter 1.

 

Thanks again for looking into this -

Share this post


Link to post
Share on other sites

As a follow-up to solving this problem incrementally, I'm trying to create a simple list of books by category:

 

JavaScript

JavaScript Patterns

Object Oriented JavaScript

 

Ajax

Ajax Definitive Guide

Building a Website with Ajax

 

jQuery

jQuery Cookbook

Learning jQuery 1.3

 

I'm comfortable with the data structure and the SQL query:

 

BOOK: book_id, book_title, fk_category_id

CATEGORY: category_id, category_name

 

SELECT category.category_name, book.book_title

FROM category LEFT OUTER JOIN book

ON category.category_id = book.fk_category_id

ORDER BY category.category_name;

 

--

 

My problem is that I don't know how to write the PHP script to list the books under each category header.

 

I know enough to get the result set into an array, but then I'm stumped on using that array to group the items as shown.

 

This seems like a pretty basic issue, but I'm having trouble finding an example that solves this. Has anyone here done this or seen a tutorial that explains how to do it?

Share this post


Link to post
Share on other sites

I think that's a better approach and the PHP side is simple. Start by creating a category variable, outside of the while loop:

$category = NULL;

Then you've got the while loop:

while ($row = mysqli_fetch_array(MYSQLI_ASSOC)) {

}

Within the while loop, you compare the just-fetched record's category with the $category variable. If they are the same, you just need to print the record. If the fetched category is different, then do whatever (like display the category name) and assign the fetched category value to $category. And that's it!

Share this post


Link to post
Share on other sites

Thanks Larry - I was able to use your starter code and solve this incremental step. Much appreciated.

 

By the way, I just finished reading your most recent newsletter. You mentioned the newsletters require a significant time investment - just wanted to say I find them really valuable, especially this issue's article on PEAR. I highly recommend the newsletter to anyone who's not already subscribed.

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...