Jump to content
Larry Ullman's Book Forums

Script 1.3: How Does It Work?


Recommended Posts

Hi,

I'm working on script 1.3. It works OK but I can't see how.

 

1. The user defined function: function make_list($parent) requires the parameter $parent. I can't see where it gets $parent from.

 

2. The only other mention of $parent is within a foreach loop: foreach ($parent as $task_id => $todo) {etc.... My understanding of foreach loops is that $parent should be an array but I cannot see in the script where an array called $parent is created. The only array I can see being created is $tasks.

 

Any clarrification would be great.

 

Cheers

Paul

Link to comment
Share on other sites

Look at the assignment in the while loop at line 62. Here he assigns all task ids along with their parent id to the $tasks array. ( $tasks[$parent_id][$task_id] ).

 

On page 20, look at script line 74. Here the first element of the array is sent to the make_list function. ($tasks[0]) THIS is the $parent input the first time around. Also note the global $tasks.

 

Because this function is recursive, (in other words, calling itself) it will loop through all task ids found in parent_id 0 before doing the same thing up to parent id N (number of elements.) Parent id 0, task 1, 2, 3.....n, then parent id 1, task 0, 1, 2, 3...n, then parent id 2 and so on.

 

You can basically look at it as a nested foreach loop. Recursion is hard to grasp, but not very difficult. The code below will do pretty much the same thing.

 

echo '<ol>';
foreach ( $tasks as $parent_id => $task_id ) 
{
   echo '<li>'.$parent_id;
   foreach ( $task_id as $task )
   {
    echo '<li>'.$task.'</li>';
   }
   echo '</li>'; // End parent id
}
echo '</ol>';

  • Upvote 1
Link to comment
Share on other sites

Antonio,

Thanks for your answer. It won't surprise you to know that it's not so much the function I've a mental block with, I think I can see how it works now, it's where it gets it's initial value from that I still don't get.

 

I think I have a real problem 'seeing' how arrays are structured. Can I go through the process step by step, please tell me where I'm wrong:

 

1. Line 56. The database query. It returns the task_id, parent_id and task in an array. Key 0 = the first line in the database, key 1 = the second line etc. This cannot be accesssed until mysqli_fetch_array is run.

 

2. Line 62. The while loop. It loops through the database array one line at a time assigning task_id, parent_id and task to variables of the same name. That's what the 'list' code does. Without the 'list code it would assign the values as, well, values in an array. Not sure I've explained that clearly but by using variables they can be used in the line below.

 

3. Line 65. For each loop that it does this for it assigns $task_id, $parent_id and $task to an array called $tasks. Now this is the line I don't get......... Actually in working through this I think I might get it. Could the same thing be achieved a much more long winded way such as

$parent_id = array($task_id => $task); $tasks = array($parent_id)

.

 

This line is creating the array from scratch isn't it, but using variables from lione 62?

 

4. The bit I still don't get is the $parent parameter. I think I don't understand what that parameter does. Is $parent an arbituary word that is only used within the function? Could I call it $mickey_mouse and providing I used Mickey later in the function (i.e. in the foreach loop) then it will work. The actually information that the function uses is assigned when the function is called, i.e. $tasks[0]. Am I warm?

 

Thanks once again for your help and patience.

Paul

Link to comment
Share on other sites

4. The bit I still don't get is the $parent parameter. I think I don't understand what that parameter does. Is $parent an arbituary word that is only used within the function? Could I call it $mickey_mouse and providing I used Mickey later in the function (i.e. in the foreach loop) then it will work. The actually information that the function uses is assigned when the function is called, i.e. $tasks[0]. Am I warm?

 

Pretty much spot on. There are nuances here, but understand it one step at the time. Learn about the difference of "assigning by reference and assigning by value". That sounds kind of difficult to understand, but it's really not. It's important for properly understanding arrays and objects.

 

Will let some others contribute to the rest.

  • Upvote 1
Link to comment
Share on other sites

Paul, you are correct in that you could give the argument $parent a different variable name and it would still work. This has to do with variable scope, an important concept to learn. It is discussed in a sidebar in the PHP and MySQL book in the chapter that discusses creating your own functions. A variable defined in a function lives only within that function, and is separate from variables defined outside the function (unless they are global variables). In the example you provided, $parent will take on the value that is passed to it when the function call is made.

Link to comment
Share on other sites

 Share

×
×
  • Create New...