Jump to content


Photo

problems using foreach


  • Please log in to reply
1 reply to this topic

#1 apophis

apophis
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 19 August 2006 - 09:06 PM

Hope some one can help me with my problem:

With an url I get a number:
<? 
$Id = (isset($_GET['Id'])) ? intval($_GET['Id']) : 0 ;  
$where = ($Id != 0) ? " WHERE Id=".$Id : " "; 
?> 

With this I can find the number and of records of the $Id and $FatherId and $MotherId:
<? 
$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 
?> 
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)
<? 
    $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>"; 
                
           
               }  
      } 
    } 
?> 
This should be the output but now but :
6638
6642
6691
7470
7476

I am getting this

6638
6638
6642
6642
6691
6691
7470
7470
7476
7476

What do I wrong?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 19 August 2006 - 11:39 PM

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
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', '', '');

and this is the script using the data to produce a five-generation pedigree chart

<?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 = '&nbsp;';

         // 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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users