kernelgpf Posted June 1, 2007 Share Posted June 1, 2007 Alright- here's my problem: I want a page to print out a lineage table. So, I have the first dragon's mom ID and dad ID, and then I want it to print out that dragon's mother's mother ID and dad ID, and that dragon's mother's mother's mother ID and dad ID, and so on. Anybody have any tips on how to accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/ Share on other sites More sharing options...
kernelgpf Posted June 1, 2007 Author Share Posted June 1, 2007 Oh, and it needs to do the father's lineage in it, also. Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/#findComment-265986 Share on other sites More sharing options...
Fergusfer Posted June 1, 2007 Share Posted June 1, 2007 Could you clarify your problem by asking a more specific question? The subject and the body of your post don't seem clearly related. If you do not know how many items are in data set, you'll often want to use foreach() for an array or while() to iterate it. Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/#findComment-265988 Share on other sites More sharing options...
kernelgpf Posted June 1, 2007 Author Share Posted June 1, 2007 Okay- on my website, dragons can be born to mom+dad dragons. I need to make a lineage table that prints out the parents of all that's dragon's parents and grandparents, and so on. But I don't know how many times it'll need to loop through, because I don't know what generation a dragon will be. Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/#findComment-265990 Share on other sites More sharing options...
Fergusfer Posted June 1, 2007 Share Posted June 1, 2007 You are discussing a sort of recursion then. Recursion is the act of a function calling itself. It is most often applicable to, surprise, recursive structures. It isn't necessarily the fastest approach, and it can have difficulty with scalability. Here's an example: <?php $selectedDragon = 5; $dragons = array( 1 => array('mom' => null, 'dad' => null), 2 => array('mom' => null, 'dad' => null), 3 => array('mom' => null, 'dad' => null), 4 => array('mom' => 1, 'dad' => 2), 5 => array('mom' => 4, 'dad' => 3) ); ?> <h1>Lineage of Dragon <?=$selectedDragon ?></h1> <table> <tr> <th>Dragon</th> <th>Mother</th> <th>Father</th> </tr> <?php getParents($selectedDragon, $dragons); ?> </table> <?php function getParents($dragonId, $dragons) { if (isset($dragons[$dragonId])) { $dragon = $dragons[$dragonId]; $mom = (!is_null($dragon['mom'])) ? $dragon['mom'] : 'unknown'; $dad = (!is_null($dragon['dad'])) ? $dragon['dad'] : 'unknown'; printf("<tr> <td>%d</td> <td>%s</td> <td>%s</td> </tr>\n", $dragonId, $mom, $dad); if (!is_null($dragon['mom'])) { getParents($dragon['mom'], $dragons); } if (!is_null($dragon['dad'])) { getParents($dragon['dad'], $dragons); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/#findComment-265996 Share on other sites More sharing options...
kernelgpf Posted June 1, 2007 Author Share Posted June 1, 2007 Fergusfer> Okay, awesome. But I have nooo idea how recursive structures work. I understand most of the code, except the $dragons array, which, I also have little experience with arrays. Does it work like for dragon ID #1, it's parents are unknown, along with 2 and 3? If I'm correct- my next problem is I'd need to select each dragon's parent IDs out of the database table "dragons", using "momID" and "dadID". How would I use that information to store it in the array $dragons? Thanks so much for your help, Fergusfer. Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/#findComment-266465 Share on other sites More sharing options...
Barand Posted June 1, 2007 Share Posted June 1, 2007 This script will print you a dragon pedigree chart eg [pre] sire sire dam dog name etc for N generations sire dam dam [/pre] <?php require 'db.php'; function printTree($name, $N, $max) { ########################################## # recursive routine to print cells in # pedigree chart # # Parameters # - name # - generation number # - max previous generations to display ########################################## if ($name == '') $name = ' '; // calculate how many rows the cell should span $rspan = pow(2, $max-$N); if ($rspan > 1) echo "\t<td rowspan='$rspan' >$name</td>\n"; else echo "\t<td>$name</td>\n"; // check for last cell in row if ($N == $max) echo "</tr>\n<tr>\n"; // print parent trees, sire then dam if ($N < $max) { $sql = "SELECT a.sire, a.dam FROM (dogtable a ) WHERE a.dogsname = '$name' "; $res = mysql_query($sql); list($s, $d) = mysql_fetch_row($res); printTree($s, $N+1, $max); printTree($d, $N+1, $max); } } function pedigree($name) { echo "<TABLE border='1' width='100%'>\n"; echo "<tr>\n"; echo "<th width='20%'>Dog</th>\n"; echo "<th width='20%'>Parents</th>\n"; echo "<th width='20%'>Grand-Parents</th>\n"; echo "<th width='20%'>Great<br>Grand-Parents</th>\n"; echo "<th width='20%'>Great-Great<br>Grand-Parents</th>\n"; echo "</tr>\n<tr>\n"; printTree($name, 0, 4); echo "<td colspan='5'>Produced by Barand</td></tr>\n</TABLE>\n"; } ?> <HTML> <HEAD> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> <meta http-equiv="content-language" content="en"> <meta Name="generator" content="PHPEd Version 3.1.2 (Build 3165)"> <title>Pedigree Chart</title> <META Name="keywords" CONTENT="Pedigree, chart"> <meta Name="author" content="B A Andrew"> <link rel="SHORTCUT ICON" href="/path-to-ico-file/logo.ico"> <META Name="Creation_Date" CONTENT="11/09/2004"> </HEAD> <BODY> <?php pedigree('dog A'); ?> </BODY> </HTML> Test data table attached [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/#findComment-266502 Share on other sites More sharing options...
Fergusfer Posted June 2, 2007 Share Posted June 2, 2007 But I have nooo idea how recursive structures work. The recursion here is simply that the function has calls to itself. It allows it to climb up the generational tree without requiring any idea of how far it needs to go. I understand most of the code, except the $dragons array, which, I also have little experience with arrays. Does it work like for dragon ID #1, it's parents are unknown, along with 2 and 3? That's correct. I chose to use nullity to indicate a condition where there is no ID. If every dragon has parents, there will be an infinite number of dragons, and in that scenario it is impossible to recur through or display the entire tree. If I'm correct- my next problem is I'd need to select each dragon's parent IDs out of the database table "dragons", using "momID" and "dadID". How would I use that information to store it in the array $dragons? Well, for a start, we need to know what sort of database you are using. MySQL is a popular database server for use in PHP development. You need to review the documentation of the mysqli extension paying particular attention to connection, querying, and fetching arrays from a result set. Quote Link to comment https://forums.phpfreaks.com/topic/53811-looping-dont-know-how-many-times-needed/#findComment-266667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.