Jump to content

[SOLVED] Check for match?


tj99ay

Recommended Posts

Hi All,

 

I only dabble in PHP, thus my urgent need for help. I currently have this which works fine:

 

<?  


$readfile = "UnitPrice_2.txt";

if (file_exists($readfile)) {

$readfile = file("UnitPrice_2.txt"); 
  
  $readfile_sort = array_reverse($readfile);
  
for ($k=0; $k<=count($readfile_sort)-2; $k++) {
    $fields = split("\t",$readfile_sort[$k]);

print("<tr onMouseOver=this.className='rowHighlight'; onMouseOut=this.className='rowEven';><td class=plain> $fields[0]</td> <td align=Center class=plain>$fields[1]</td> <td class=plain align=Center>$fields[2]</td> <td class=plain align=Center>$fields[3]</td></tr>");
   
}


} else {
   print ("<p><span class=subheading>Sorry data temporarily unavailable, please try again later.</span></p>");
}
?> 

 

 

but I want to change the code to match two fields - 1 from each external tab delimited text file. If $fields[0] and $field2[0] match (it's actualy checking for a matching date) display something in $showtext.

 

I know what I want to do with the code, but I don't have enough knowledge to actually write it properly. This is what I have done thus far which does not work:

 

 

<?  

$readfile = "UnitPrice_2.txt";
$readfile2 = "ExDist_2.txt";

if (file_exists($readfile) || file_exists($readfile2)) {

$readfile = file("UnitPrice_2.txt"); 
$readfile2 = file("ExDist_2.txt"); 
  
  $readfile_sort = array_reverse($readfile);
    $readfile_sort2 = array_reverse($readfile2);
  
for ($k=0; $k2=0; $k<=count($readfile_sort)-2; $k2<=count($readfile_sort2)-2; $k++; $k2++) {

    $fields = split("\t",$readfile_sort[$k]);	
    $fields2 = split("\t",$readfile_sort2[$k2]);

	if ($fields[0] == $fields2[0]) {	
	$showtext = "Ex-Distribution";
	} else {
$showfields = " ";	
}

print("<tr onMouseOver=this.className='rowHighlight'; onMouseOut=this.className='rowEven';><td class=plain> $fields[0]</td> <td align=Center class=plain>$fields[1]</td> <td class=plain align=Center>$fields[2]</td><td class=plain align=Center><strong>$showtext</strong></td></tr>");
  
  } else {
   print ("<p><span class=subheading>Sorry data temporarily unavailable, please try again later.</span></p>");
}
?> 

 

Any urgent guidance would be appreciated.

 

Many thanks

 

tj99ay

 

 

Link to comment
https://forums.phpfreaks.com/topic/67174-solved-check-for-match/
Share on other sites

Well, your for loop doesn't make sense. You can't have two conditions within your for loop.

 

I assume that by "does not work" you mean you end up with a blank page? Or do you actually get the error message? Either way, it would be helpful to be a bit more descriptive of the problem.

 

Anyways, i would suggest constructing your for loop as something like:

 

<?php
for($k=0;$k<=count($readfile_sort)-2;$k++{
$k2 = $k+1;
//rest of your code here
}
?>

 

That should be ok if the only matches you want to find are those where the 1st tab delimited value in file 1 is the same as the 3rd tab delimited value in file 2 and are on the same line within their respective files. If thats not what you're after, you'll need to explain a bit more fully with what you are trying to achieve.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.