Page 1 of 1

checking a variable to see if it matches an entry in a CSV.

Posted: Fri Feb 26, 2010 11:25 am
by wilded1
Hi I have been looking at this on and off all day long and can't seem to find a solution.

I have a DB query and in the result is [zonrp_cs] (this is a callsign)

I also have a CSV file listing a few callsigns.

I am printing out a table fine, but, when a callsign in the query matches a callsign in the CSV list I want that callsign to be printed as a URL.

Please have a look and tell me where I am going wrong.

Code: Select all

 
while ($row = pg_fetch_array($result)) {
$last_mod_time = get_time($row['last_mod_time']);
$last_mod_date = get_date($row['mod_date']);
$reg_date = get_date($row['reg_date']);
$callsign = $row['zonerp_cs']; // this is the callsign we will check and display as either plain text or a link.
$cslinked="";
echo '<tr class="trHiLite">';
########################################
#####This part is checking for a match in the CSV #####
########################################
$row = 1;
if (($handle = fopen("cslist.txt", "r")) !== FALSE) {
   while (($data = fgetcsv($handle, 10, ",")) !== FALSE) {
        $num = count($data);
       $row++;
      for ($c=0; $c < $num; $c++) {
        if ($callsign == $data[$c]){
           $cslinked = $callsign; // If $callsign matches an item in the CSV then set $cslinked to equal $callsign and break out.
          break;
        }
     }
  }
}
    fclose($handle);
########################################
// Now decide how to display our callsigns //
if ($cslinked != $callsign){
echo "<td>$callsign</td>";
}
else{
echo "<td><a href='gateways.php?gateway=$callsign' target='_blank'>$callsign</a></td>";
}
?>    
      <td><?php echo $last_mod_date;?></td>
      <td><?php echo $last_mod_time;?></td>
      <td><?php echo $reg_date;?></td>
<!--<td><?php //echo $row['del_flg'];?></td>-->
  </tr>
  <?php
} // The while loop ends here, lets tidy up and end the script and html.
 
All I am getting is the table I want but the callsigns listed in the CSV are not written as links?

TIA for any help
Dave

Re: checking a variable to see if it matches an entry in a CSV.

Posted: Fri Feb 26, 2010 1:22 pm
by wilded1
I have tried approaching this from a different angle using a regex.

Now if i write this and the callsign matches in the CSV file it echoes perfectly:

Code: Select all

 
<table border="1"><tr>
<?php
$callsign = "1234";
########################################
$data = file_get_contents("cslist.txt");
$pattern = "/\b$callsign\b/";
preg_match($pattern, $data, $linkcs);
#######################################
if($linkcs[0] == $callsign){
echo "<td><a href='gateways.php?gateway=$callsign' target='_blank'>$callsign</a></td>";
}
else{
echo "<td>$callsign</td>";
}
?>
</tr></table>
 
However when inserted into my while loop I get an error for each row in the DB query result:
"Notice: Undefined offset: 0 in /var/www/html/trust.php on line 84"
Line 84 in my script is line 13 below.

Code: Select all

 
$data = file_get_contents("cslist.txt");
while ($row = pg_fetch_array($result)) {
$last_mod_time = get_time($row['last_mod_time']);
$last_mod_date = get_date($row['mod_date']);
$reg_date = get_date($row['reg_date']);
$callsign = $row['zonerp_cs'];
echo '<tr class="trHiLite">';
########################################
$data = file_get_contents("cslist.txt");
$pattern = "/\b$callsign\b/";
preg_match($pattern, $data, $linkcs);
if($linkcs[0] == $callsign){
echo "<td><a href='gateways.php?gateway=$callsign' target='_blank'>$callsign</a></td>";
}
else{
echo "<td>$callsign</td>";
}
// and so on creating table data for other fields
 
Regards
Dave

Re: checking a variable to see if it matches an entry in a CSV.

Posted: Fri Feb 26, 2010 1:46 pm
by AbraCadaver
If there are no matches, then $linkcs is empty. Use the return of preg_match() which is either 0 or 1. If 1, then you know there was a match:

Code: Select all

if(preg_match($pattern, $data)) {
   echo "<td><a href='gateways.php?gateway=$callsign' target='_blank'>$callsign</a></td>";
}