Page 1 of 1

problems using foreach

Posted: Sat Aug 19, 2006 4:10 pm
by Apophis
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hope some one can help me with my problem:

With an url I get a number:

Code: Select all

<? 
$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:

Code: Select all

<? 
$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)

Code: Select all

<? 
    $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?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Aug 19, 2006 4:17 pm
by feyd
mysql_fetch_array() returns both named and numeric indices of your query. print_r($row) to see what I'm talking about. Either tell mysql_fetch_array() to only give you one type or use mysql_fetch_assoc() or mysql_fetch_row()..

...and please start using the correct tags (

Code: Select all

) when posting php code.

Why if the date is "NULL"

Posted: Sat Aug 19, 2006 4:41 pm
by Apophis
But why if the Date is "NULL" is he giving 01 Jan 1970. If the records is having a date it will display correct.

This is the function I use in the foreach statement

Code: Select all

function Print_distinct_mother ($sql_row){
	  
		$Id = $sql_row['Id'];
	 	$DOB1 = $sql_row['DOB'];
		$DOB = date('d M Y', strtotime($DOB1));
		  
		$Image_folder = "<img src=\"../pictures/andere/new.gif\" width=\"35\" height=\"15\" />";
	    $new = $Image_folder;
		$bewerk_datum = $sql_row['DOC'];  
		$aantal_dagen = floor(abs((strtotime($bewerk_datum)-time())/(60*60*24)));  
	
		$Image_folder2 = "<img src=\"../pictures/andere/update.gif\" width=\"47\" height=\"10\" />";
	    $update = $Image_folder2;
		$bewerk_datum2 = $sql_row['DOU'];  
		$aantal_dagen2 = floor(abs((strtotime($bewerk_datum2)-time())/(60*60*24)));
		 
		print "<span class=\"style2\">".$sql_row['Ch']."</span>\n";
		print "</a>&nbsp";
		print "<span class=\"style3\">".$sql_row['DogName']."</span>";
		print "</a>&nbsp";
		print "<span class=\"style4\">(".$sql_row['Color'].")</span>";
		print "</a>&nbsp";
		print "<span class=\"style4\">".$DOB."</span>";
		print "</a>&nbsp;&nbsp;&nbsp&nbsp";
		if($aantal_dagen <60){echo $new;}  
	      //else{echo'';}
	    if($aantal_dagen2 <60){echo $update;}  
	     //else{echo'';}
		print "<br><font color='black' size='1' face='Arial'>";
		print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp[<a href=\"pedigree.php?Id=$Id\">Pedigree]</a>&nbsp;&nbsp;";
		print "[<a href=\"breeding.php?Id=$Id\">Breeding]</a>&nbsp;&nbsp;";
		print "[<a href=\"sibling.php?Id=$Id\">Sibling]</a></font>";
	  
}
This is the query with the if and the foreach:

Code: Select all

//Dit geeft de volgende nummers ((6638,6642,6691,7470,7476)
	$SQL_distinct_mother = "SELECT DISTINCT MotherId FROM dog_table where FatherId = $FatherId and MotherId != $MotherId and Id != $Id ORDER BY DogName ASC";
	$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_assoc($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>";
      		 print Print_distinct_mother($sql_row)."<br>";
      	
      	} 
        }
   }

Posted: Sat Aug 19, 2006 4:51 pm
by feyd
For current, many previous, and many forthcoming generations of computers start counting time on January 1st, 1970 (the exact time and date varies on where the machine thinks it is.) A NULL date will result in the beginning of time. This is known as the Epoch.

Your code should detect a null value and choose to display something else, I suspect.