|P is for Practical|
Re^3: mysql DBI Nested Queriesby roboticus (Canon)
|on Dec 23, 2012 at 23:55 UTC||Need Help??|
Sure thing. Just take it step by step. First, notice that your code is structured something like this:
The first thing you need to do is identify a repeated chunk of code that's relatively easy to separate from the surrounding code. There are several things you might choose, but the one that jumped out at me was the code that converts the array reference into a set of variables and then writes the variables to a worksheet.
It's easy to separate from the surrounding code because there's only three variables on the input side ($aref, $worksheet0 and $row) and one variable on the output side ($supervisor). Since the $row is going to change everywhere, and since it would be pain to have to pass it around and return it everywhere, I decided to leave it a global variable. So what I wound up with is this:
We return the supervisor variable so you can use it in the next level. When you pull that code out into a subroutine, your code simplifies a good bit:
There are a good few tuneups you can do to this program. You might want to read up on placeholders in DBI. That can simplify your code a bit, too. Then, depending on how many supervisors a person may have, whether you want to go as many levels as required without hardcoding, etc., there are several ways you could go from here. I'll stop here, as I don't want to take *all* the fun out of it! ;^) But I *will* provide a hint: If each person has only one supervisor, you can turn the successive level of while loops into a simple loop. Give it a try and let me know if you run into any stumbling blocks.
When your only tool is a hammer, all problems look like your thumb.