Jump to content

Barand

Moderators
  • Posts

    24,612
  • Joined

  • Last visited

  • Days Won

    834

Everything posted by Barand

  1. I'm struggling too. My method got U correct but not J. Using your nested foreach() method gets J correct but not U. Time for a new algorithm?
  2. Those results were from my own code. Yours almost agrees with mine in that we both get | 44 | A | 2,2| 2,2| but I interpret that as A | 0.03125 | 2 | 2 | A | 0.03125 | 2 | 2 | whereas you apparently have doubled it to A | 0.03125 | 2 | 2 | A | 0.03125 | 2 | 2 | A | 0.03125 | 2 | 2 | A | 0.03125 | 2 | 2 |
  3. I get these results, which look right to me DOG: J | A | 0.0312 | 2 | 2 | | A | 0.0312 | 2 | 2 | | B | 0.0312 | 2 | 2 | | B | 0.0312 | 2 | 2 | COI : 0.1250 DOG: U | Q | 0.1250 | 1 | 1 | | O | 0.0312 | 2 | 2 | | O | 0.0312 | 2 | 2 | | P | 0.0312 | 2 | 2 | | P | 0.0312 | 2 | 2 | | R | 0.1250 | 1 | 1 | COI : 0.3750 DOG: Z COI : 0.0000
  4. In the code I posted, A - J would be in the sires array and O - U would be in the dams array, which would produce no common ancestors
  5. Specify just the columns you want in the SELECT (which you should do anyway, instead of using "SELECT * ")
  6. I used your site page to generate the pedigree for Dog#4 then entered that in their calculator chart. There was no dog #20 in their results (only 14 and 17) and then I looked at what results would give their value for #17.
  7. "It doesn't work" tells us sweet FA about the problem. We cannot see your screen. What is happening? What isn't happening?
  8. Looks like that site used REF SITE METHOD 3 | 14 | 0.0625 | 1 | 2 | | 17 | 0.0312 | 2 | 2 | | 17 | 0.0625 | 2 | 1 | Dog 4 COI : 0.1562 Dog #20 ignored, and only two of the three occurrences of #17 were used. So why?
  9. I would think the "\r\n" is the cure. Let us know.
  10. So, which is correct for Dog #4? METHOD 1 | 14 | 0.0625 | 1 | 2 | | 20 | 0.0156 | 2 | 3 | | 17 | 0.0156 | 2 | 3 | | 17 | 0.0312 | 2 | 2 | | 17 | 0.0625 | 2 | 1 | Dog 4 COI : 0.1875 METHOD 2 | 14 | 0.0625 | 1 | 2 | | 20 | 0.0156 | 2 | 3 | | 17 | 0.0156 | 2 | 3 | | 17 | 0.1250 | 0 | 2 | | 17 | 0.2500 | 0 | 1 | Dog 4 COI : 0.4688
  11. For dog 4, you had this for common ancestor 17 sire dam | 17 | Dog R | 2| 3,2,1| so you processed it as 0.015625 | 2 | 3 | 0.03125 | 2 | 2 | 0.0625 | 2 | 1 | Shouldn't it have been 0.015625 | 2 | 3 | 0.125 | 0 | 2 | 0.25 | 0 | 1 |
  12. I see, different in each table. Need to change the syntax DELETE table1, table2, table3 FROM table1 LEFT JOIN table2 ON Rooms.id = Room_users.Roomid LEFT JOIN table3 ON Rooms.id = Room_chats.roomid WHERE table1.time < UNIX_TIMESTAMP()
  13. Is it using caps in all tables? If not, why the inconsistency. (Why use caps at all?)
  14. your column names may be case-sensitive
  15. then DELETE table1, table2, table3 FROM table1 LEFT JOIN table2 USING (roomid) LEFT JOIN table3 USING (roomid) WHERE table1.time < UNIX_TIMESTAMP()
  16. If there are possibly no matching records in table2 or table3 then use LEFT JOINS. Where are you binding the time parameter? As I've no idea what format the time is in I left that for you to set a value for "currenttime"
  17. Given the total absence of database-related code moved from mysql to php help
  18. try DELETE table1, table2, table3 FROM table1 INNER JOIN table2 USING (roomid) INNER JOIN table3 USING (roomid) WHERE table1.time < :currenttime
  19. Does this version give the results you expect? <?php $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); function getAncestors($id, &$dogs, &$ancests, $dist) { if ($id==0) return; $ancests[$id][] = $dist; if (isset($dogs[$id]) ) { getAncestors($dogs[$id][0], $dogs, $ancests, $dist+1); getAncestors($dogs[$id][1], $dogs, $ancests, $dist+1); } } function COI($id, &$dogs) { if ($id==0) return 0; $sires = $dams = []; getAncestors($dogs[$id][0], $dogs, $sires, 1); getAncestors($dogs[$id][1], $dogs, $dams, 1); $result=0; foreach ($sires as $did=>$dists) { if (isset($dams[$did])) { if (!is_null($dogs[$did][3])) { return $dogs[$did][3]; } else { $sumd = array_sum($dists) + array_sum($dams[$did]); $result += pow(0.5, 1+$sumd) * (1 + COI($did, $dogs)); $dogs[$did][3] = $result; } } } return $result; } $sql = "SELECT id, dogname, sire, dam FROM dogtable"; $dogs = array(); $res = $db->query($sql); while (list($id, $nm, $s, $d) = $res->fetch_row()) { $dogs[$id] = [$s,$d,$nm,null]; } $dogid = 9; printf("Dog %d COI : %0.3f", $dogid, COI($dogid, $dogs)); ?>
  20. The problem is the function was only storing one occurrence per ancestor. Try this $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); function getAncestors($id, &$dogs, &$ancests, $dist) { if ($id==0) return; $ancests[$id][] = $dist; // <-- changed to store more than 1 distance if (isset($dogs[$id]) ) { getAncestors($dogs[$id][0], $dogs, $ancests, $dist+1); getAncestors($dogs[$id][1], $dogs, $ancests, $dist+1); } } $sql = "SELECT id, dogname, sire, dam FROM dogtable"; $dogs = $sires = $dams = array(); $res = $db->query($sql); while (list($id, $nm, $s, $d) = $res->fetch_row()) { $dogs[$id] = [$s,$d,$nm]; } $dogid = 1; getAncestors($dogs[$dogid][0], $dogs, $sires, 1); getAncestors($dogs[$dogid][1], $dogs, $dams, 1); ksort($sires); ksort($dams); echo '<pre>sires ',print_r($sires, true),'</pre>'; echo '<pre>dams ',print_r($dams, true),'</pre>'; $common = array_intersect_key($sires,$dams); echo "<pre>"; echo "| ID | NAME | SIRE | DAM |\n"; echo "| | | DIST | DIST |\n"; echo "|-----|--------------------|------|------|\n"; foreach ($common as $id => $dist) { printf("|%4d | %-18s |%6s|%6s|\n", $id, $dogs[$id][2], join(',', $sires[$id]), // changed to show the join(',', $dams[$id]) ); // multiple distances } Now gives results like | ID | NAME | SIRE | DAM | | | | DIST | DIST | |-----|--------------------|------|------| | 8 | dog I | 3| 2| | 10 | dog K | 4,3| 3| | 16 | dog Q | 4| 3| | 17 | dog R | 4| 3| | 20 | dog U | 5,4| 4| | 21 | dog V | 5,4| 4|
  21. You need to take seat location into account. If you have, say, 10 seats in 2 rows of 5 seats 1-3 are booked 3 people require seats 1* 2* 3* 4 5 6 7 8 9 10 then allocating seats 4,5,10 would be a better solution than 4,5,6 similar post that may help http://forums.phpfreaks.com/topic/284542-allocated-seating-layout-help/?do=findComment&comment=1461309
  22. Add another element to the $dogs array to store the COI (Fa) for that dog. It will require a recursive function along these lines function COI(ancester_id) { if (COI is in the table already) { return COI from the table } else { calculate COI of ancester // will require calls to COI(ancester) the recursive bit store in table return calculated value; } }
  23. This simple library function of mine will do it. You can use CSS styling to make it look prettier. $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); $sql = "SELECT * FROM users WHERE allowed_user=1"; // call the function echo query2HTML($db, $sql); function query2HTML($db, $sql) { $output = "<table border='1' cellpadding='2' style='border-collapse:collapse'>\n"; // Query the database $result = $db->query($sql); // check for errors if (!$result) return ("$db->error <pre>$sql</pre>"); if ($result->num_rows == 0) return "No matching records"; // get the first row and display headings $row = $result->fetch_assoc(); $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n"; // display the data do { $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } while ($row = $result->fetch_assoc()); $output .= "</table>\n"; return $output; } I suggest you examine each line of code in conjunction with the PHP manual until you understand what each line is doing.
  24. try this version $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); $sql = "SELECT id, dogname, sire, dam FROM dogtable"; $dogs = $sires = $dams = array(); $res = $db->query($sql); while (list($id, $nm, $s, $d) = $res->fetch_row()) { $dogs[$id] = [$s,$d,$nm]; } function getAncestors($id, &$dogs, &$ancests, $dist) { if ($id==0) return; $ancests[$id] = $dist; if (isset($dogs[$id]) ) { getAncestors($dogs[$id][0], $dogs, $ancests, $dist+1); getAncestors($dogs[$id][1], $dogs, $ancests, $dist+1); } } $dogid = 1; $sires = $dams = []; getAncestors($dogs[$dogid][0], $dogs, $sires, 1); getAncestors($dogs[$dogid][1], $dogs, $dams, 1); ksort($sires); ksort($dams); #echo '<pre>',print_r($dogs, true),'</pre>'; echo '<pre>sires ',print_r($sires, true),'</pre>'; echo '<pre>dams ',print_r($dams, true),'</pre>'; $common = array_intersect_key($sires,$dams); #$common = array_merge($sires,$dams); echo "<pre>"; echo "| ID | NAME | SIRE | DAM |\n"; echo "| | | DIST | DIST |\n"; echo "|-----|--------------------|------|------|\n"; foreach ($common as $id => $dist) { printf("|%4d | %-18s | %4d | %4d |\n", $id, $dogs[$id][2], $sires[$id], $dams[$id]); }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.