apophis Posted August 19, 2006 Share Posted August 19, 2006 Hope some one can help me with my problem:With an url I get a number:[code]<? $Id = (isset($_GET['Id'])) ? intval($_GET['Id']) : 0 ; $where = ($Id != 0) ? " WHERE Id=".$Id : " "; ?> [/code]With this I can find the number and of records of the $Id and $FatherId and $MotherId:[code]<? $SQL_parents = "SELECT FatherId, MotherId FROM dog_table WHERE Id = '".$Id."'" or die(mysql_error()); $SQL_parents_Result = mysql_query($SQL_parents); if ($SQL_parents_Result) { $parents_row = mysql_fetch_assoc($SQL_parents_Result); $MotherId = $parents_row['MotherId']; //De nummer van de Moeder $FatherId = $parents_row['FatherId']; //De nummer van de Vader ?> [/code]Now i want to find the unique records with the information I have with the help of a query: This will give me again numbers and with this number i can find there records with a query.The numbers I get from that distinct query are (6638,6642,6691,7470,7476)[code]<? $SQL_distinct_mother = "SELECT DISTINCT MotherId FROM dog_table where FatherId = $FatherId and MotherId != $MotherId and Id != $Id"; $SQL_distinct_mother_result = mysql_query($SQL_distinct_mother) or die(mysql_error()); if ($SQL_distinct_mother_result) { $numRows = mysql_num_rows($SQL_distinct_mother_result); for ($x=0;$x<$numRows;$x++) { $row = mysql_fetch_array($SQL_distinct_mother_result); //print $row['MotherId']; //print $row."<br>"; foreach ($row as $sValue) { $sql = "SELECT * FROM dog_table WHERE Id = '". $sValue ."'"; $sql_result = mysql_query($sql) or die(mysql_error()); $sql_row = mysql_fetch_assoc($sql_result); print $sql_row['Id']."<br>"; } } } ?> [/code]This should be the output but now but :66386642669174707476I am getting this6638663866426642669166917470747074767476What do I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/18054-problems-using-foreach/ Share on other sites More sharing options...
Barand Posted August 19, 2006 Share Posted August 19, 2006 I haven't chacked your code yet, but given the nature of your problem you may find this (somehing I prepared earlier) of use.This creates the test data[code]CREATE TABLE dogtable ( `id` int(11) NOT NULL auto_increment, `dogsname` varchar(20) NOT NULL , `sire` varchar(20) , `dam` varchar(20) , PRIMARY KEY(dogsname));INSERT INTO dogtable VALUES ('1', 'dog A', 'dog B', 'dog C');INSERT INTO dogtable VALUES ('2', 'dog B', 'dog E', 'dog F');INSERT INTO dogtable VALUES ('3', 'dog C', 'dog G', 'dog H');INSERT INTO dogtable VALUES ('4', 'dog E', 'dog I', 'dog J');INSERT INTO dogtable VALUES ('5', 'dog F', 'dog K', 'dog L');INSERT INTO dogtable VALUES ('6', 'dog G', 'dog M', 'dog N');INSERT INTO dogtable VALUES ('7', 'dog H', 'dog O', 'dog P');INSERT INTO dogtable VALUES ('8', 'dog I', 'dog Q', 'dog R');INSERT INTO dogtable VALUES ('9', 'dog J', 'dog S', 'dog T');INSERT INTO dogtable VALUES ('10', 'dog K', 'dog U', 'dog V');INSERT INTO dogtable VALUES ('11', 'dog L', 'dog W', 'dog X');INSERT INTO dogtable VALUES ('12', 'dog M', 'dog Y', 'dog Z');INSERT INTO dogtable VALUES ('13', 'dog N', '', '');INSERT INTO dogtable VALUES ('14', 'dog O', '', '');INSERT INTO dogtable VALUES ('15', 'dog P', '', '');INSERT INTO dogtable VALUES ('16', 'dog Q', '', '');INSERT INTO dogtable VALUES ('17', 'dog R', '', '');INSERT INTO dogtable VALUES ('18', 'dog S', '', '');INSERT INTO dogtable VALUES ('19', 'dog T', '', '');INSERT INTO dogtable VALUES ('20', 'dog U', '', '');INSERT INTO dogtable VALUES ('21', 'dog V', '', '');INSERT INTO dogtable VALUES ('22', 'dog W', '', '');INSERT INTO dogtable VALUES ('23', 'dog X', '', '');INSERT INTO dogtable VALUES ('24', 'dog Y', '', '');INSERT INTO dogtable VALUES ('25', 'dog Z', '', '');[/code]and this is the script using the data to produce a five-generation pedigree chart[code]<?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 "<td width='20%'>Dog</td>\n"; echo "<td width='20%'>Parents</td>\n"; echo "<td width='20%'>Grand-Parents</td>\n"; echo "<td width='20%'>Great<br>Grand-Parents</td>\n"; echo "<td width='20%'>Great-Great<br>Grand-Parents</td>\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>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18054-problems-using-foreach/#findComment-77405 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.