Page 1 of 1

Matching up Arrays

Posted: Sun Sep 23, 2007 9:11 am
by softcode
Ok,


Please brace yourselves for this, its rather long but the solution am looking for is simple and am sure it wont take long for you guys to get it.

I would like to make it as clear as crystal, please inform me where you feel confused.



$sql = "SELECT * FROM $dbtable WHERE job_id='$batch_nr' ORDER BY group_id ASC";
$result = mysql_query($sql ,$dbt);

Code: Select all

$total_records = mysql_num_rows($result);   
     $i=0;
     
     while ($i <  $total_records) {
	   
       
         /* To obtain the group_id  */
         $first=mysql_result($result,$i,"group_id");

       /* To obtain the serial value  which is composed of 
        *   nr values and test results in a string. 
      */
	 $serial=mysql_result($result,$i,"serial_value");
	   
	  /*To remove the = and & characters in the string  */ 
           $keywords = preg_split ("/[\=\&]+/", "$serial"); 
           $cols=count($keywords);


	 /* Test results are obtained from here */
	 for ($rol=2; $rol < $cols; $rol+=2){
     $rname = $rname.$keywords[$rol].'#';         
     }
	 
	/* the nr numbers are obtained from here       */
    for ($kol=1; $kol < $cols; $kol+=2){
                    
       $peck = $peck .$keywords[$kol].'#';      
     }
	 
	 	
	 $i++; 
	 }      



                                       # To retrieve the nr numbers as an array    
					 $nval = preg_split ("/[\#]+/", "$peck "); 
					 array_pop( $nval); 
					 
					 
					
					 $jack = 285285;
					 $rvalue = str_replace('-', 285285, "$rname");   

                                         # To retrieve the test results            
					 $rval = preg_split ("/[\#]+/", "$rvalue");
					 array_pop( $rval); 
					 

                                             $parameterselect = $param_gp; 
                                             $notes =  $notes;
                 



         $lastgroup = '';
					 
		$depts = $db->Execute("SELECT $dbtable.serial_value,$dbtable.type,$dbtable.group_id,$dbtable.notes,$table.name,$table.nr,$table.normals,$table.msr_unit  FROM $dbtable INNER JOIN $table ON $dbtable.group_id = $table.group_id WHERE  $dbtable.job_id = '$batch_nr' ORDER BY group_id ASC, torder"); 
										  
		   while($rows= $depts->FetchRow()){
		   
		   
		   
		   
		        // Print Group header
    if ($rows['group_id'] != $lastgroup) {
	
	        
	//Display last subtotal if not first group
				if ($lastnotes) {
				
	 				   echo '<tr>
			           <td width="160" height="10"></td> 
                       </tr>'; 
					   echo  '<tr>'; 
	                   echo '<td height=10><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif">Notes/Comments</FONT></td>'."\n"; 
					   echo '<td height=10 width=300><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif">'.nl2br($lastnotes).'</FONT></td>'."\n";       
                       echo '</tr>';
				}
		
				
				$lastnotes = $rows['notes'];
				//Reset the subtotal
				$subtotal = 0;
				// Print Group Name
    echo '<tr>
	<td width="160" height="10"></td> 
    </tr>';
	
	 echo '<tr class=wardlistrow2 >';
     echo '<td><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif" ><u> '.$parametergruppe[$rows['group_id']].'</u></FONT></td>'."\n";   
	 echo '<td> </td>'."\n";  
	 echo '<td>  </td>'."\n";   
	 echo '<td>  </td>'."\n";      
     echo  '</tr>';
		
		
    }
		   
           
           
		   
		   if ($rval[$counter] != $jack)  { 

                  /* Match up of the nr numbers   */
                 if ($nval[$counter] == $rows['nr'])  { 
                  
		   ?>
		<tr  >
		
		<td><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif" > <?php echo $rows['name']; ?> </FONT></td>
		<td><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif" ><?php echo nl2br ($rval[$counter]); ?></FONT></td> 
		<td><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif" > <?php echo $rows['msr_unit']; ?></FONT> </td> 
        <td><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif" > <?php echo $rows['nr']; ?></FONT> </td>
        <td><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif" > <?php echo $nval[$counter]; ?></FONT> </td>
		<td width="149" valign="top"><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif" > <?php echo $rows['normals']; ?></FONT></td>  
		</tr>
		
		<?php 
		 }   
           }  
			//Add to subtotal
			$subtotal++;
		
			//Set the lastgroup value
			$lastgroup = $rows['group_id'];
		    $counter++;
	}
	           echo '<tr>
			               <td width="160" height="10"></td> 
               </tr>'; 
			
		
			// Show the last subtotal row
		    echo  '<tr>'; 
	        echo '<td height=10><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif">Notes/Comments</FONT></td>'."\n";   
			echo '<td height=10 width=300 ><FONT SIZE=2 FACE="Arial, Helvetica, sans-serif">'.nl2br($lastnotes).'</FONT></td>'."\n";    
            echo  '</tr>';
		
		  
       ?>
  	
  </form>
   </table>



Guys, The problem is that if I add an nr value  then none of the tests show correctly they are mismatched.

Do you follow ?????

Re: Matching up Arrays

Posted: Sun Sep 23, 2007 9:27 am
by superdezign
softcode wrote:I would like to make it as clear as crystal, please inform me where you feel confused.
... What is your question...?
Your code isn't very pleasing to the eye, therefore it's not easy to read or pinpoint what it is that you are trying to accomplish. The HTML inside of it doesn't help much either.

Posted: Sun Sep 23, 2007 9:49 am
by feyd
Lovely choices in variable names. :roll:

Can you explain what this "nr value" is?

Posted: Mon Sep 24, 2007 4:19 am
by softcode
Feyd,
Is it possible to attach mysql tables here ?

Posted: Mon Sep 24, 2007 4:44 am
by aceconcepts
Seriously, do you want help with anything??????

Posted: Mon Sep 24, 2007 7:31 am
by superdezign
softcode wrote:Feyd,
Is it possible to attach mysql tables here ?
We don't want the tables. Pinpoint your problem, then ask questions. We aren't psychic and most of us don't spend too much time on one post, so unless you are specific, you're not going to receive much assistance.

Posted: Mon Sep 24, 2007 8:11 am
by softcode
Hey there,

I've made the code above a lot simpler so I can pin point the problem

Code: Select all

$deps = $db->Execute("SELECT * FROM $dbtable WHERE job_id='$batch_nr' AND group_id='$rowb[group_id]'" );                 
             while($rowz = $deps->FetchRow())
            { 
                     // get the test group
                   $pio =  $rowz['group_id'];
                  

                    // get the test data values and the test param name
                   $test_vals =  $rowz['serial_value']; 
                   $values = preg_split ("/[\&]+/", "$test_vals");  
                   array_shift($values);
                

                   $new_strs = implode("=", $values);  
                   $vals = preg_split ("/[\=]+/", "$new_strs"); 
                   array_shift($vals);
              

                   
                  $numitems2 = count($vals);
                  $notes = $rowz['notes']; 
      
                      
                    
                for ($col=0; $col <$numitems2; $col+=2){ 
                         $fname2 =$fname2.$vals[$col].'#';          
                 } 
                  
                 for ($rol=1; $rol <$numitems2; $rol+=2){             
                         $rname2 = $rname2.$vals[$rol].'#';          
                 }      
            }
            
            $counterup=0;
            
            $nval2 = preg_split ("/[\#]+/", "$fname2");
            array_pop($nval2); 
             
            
            
            
            $jack = 285285;   
            $numname = count($nval2);  
            $rvalues = str_replace( "-", 285285, "$rname2"); 
            $rvals = preg_split ("/[\#]+/", " $rvalues"); 
            

     
             $disp = $db->Execute("SELECT  * FROM $table WHERE  group_id='$rowb[group_id]' ORDER by torder");
             while($rows3 = $disp->FetchRow())               
             {  
              
                                 
           [color=red] if ($rows3['nr'] == $nval2[$counterup])[/color]
            {
            if ($rvals[$counterup] != $jack)
         
         {
                $cache .= '<tr class=wardlistrow1'; 
                $cache .= '>';
                $cache.='<td class=va12_n > '.$rows3['name'].' </td>'."\n";
                $cache.= "<td width=149 valign=top class=va12_n > $rvals[$counterup] </td>";
                $cache.='<td class=va12_n> '.$row3['msr_unit'].'</td>'."\n"; 
                $cache.='<td width="149" valign="top" class=va12_n> '.$rows3['normals'].' </td>'."\n";  
               
                   
                $cache .= '</tr>';
 
         }    
                   
                 
             }
             $counterup++;  
              
          }   
          
              
              $cache .= '<tr><td width="160" height="10"></td> </tr>'; 
          }


echo $cache;


the two arrays $rows3['nr'] and $nval2[$counterup] need to match up to display the data in between the if brackets. The problem is they cannot match up unless they are exactly of the same order. But this is not possible as the data needs to be printed out in torder.$nval2[$counterup] should not neccesarily be in torder and torder can change.

So the question is, Is it possible to match them up while respecting the torder and still out put the data btwn the brackets correctly?

take this as sample : These two don't printout anything.

$rows3['nr'] = '125','136','143','148','149','159','160','161','191','201','205','212','221','247','251','253','254','255','327','256','239','262','257','337','338','339','340'

$nval2[$counterup] = '125','136','143','148','149','159','160','161','191','201','205','212','221','247','251','253','254','255','256','239','262',257