Jump to content

Comparing data from a .txt file to DB info.


pocobueno1388

Recommended Posts

I have a txt file that stores separate lines of information. Here is an example of one of the lines.

Western Pleasure || height:>= 15, discipline:== 'western'

 

That is just telling me the qualifications of the "Western Pleasure" show type, which is that the height has to be greater than 15 and the discipline has to be western.

 

Now I am trying to take these qualification statements and compare them to values in the database, if  both qualifications match up, I would like to add the word "western pleasure" to an array.

 

Here is my current code:

<?php

   //get horses info
   $funInfo = mysql_query("SELECT discipline, height FROM horses WHERE horseID='$horseID'");
   $f = mysql_fetch_assoc($funInfo);
   
   //prepare array to start being populated
   $shows = array();
   
   $file = file("web_files/shows.txt");
   
      foreach ($file as $line){

           /*
           Start splitting the lines that look like this up: 
           Western Pleasure || height:>= 15, discipline:== 'western'
           */

         //split event type from require, event=0; require=1
         $event = explode("||", $line);
         $event[0] = strtolower($event[0]);
         echo '<b>'.$event[0].'</b><br>';
         
         //split require into separate parts
         $require = explode(",", $event[1]);
         
         //split require into separate pieces
         $req = explode(":", $require[0]);
         $req2 = explode(":", $require[1]);
         
         //put together if statements
         $statement = "";
         if (trim($req[0]) != "") $statement .= "\$f[$req[0]] $req[1]";
         if (trim($req2[0]) != "") $statement .= " && \$f[$req2[0]] $req2[1]";
         echo '<p>'.$statement.'<p><hr>';
         
         if ($statment){
            $shows[] = $event[0];
         }
      }
?>

 

I am having issues trying to store variables in another variable on this line:

<?php
if (trim($req[0]) != "") $statement .= "\$f[$req[0]] $req[1]";
?>

 

If I don't escape the dollar sign I get an error.

Parse error: syntax error, unexpected '[', expecting ']'

 

When I echo my statement variable I get:

$f[ height] >= 15 && $f[ discipline] == 'western'

 

But obviously I want the variables to print out their value....

 

 

 

I'm thinking if I can get all the variables to come out properly to build my if statment, then the code will be fine. I'm really not confident in my method of doing this though, it seems like there would be a better method.

 

Any suggestions is greatly appreciated, thanks =] I really hope I made myself clear enough, it was tough to explain.

 

 

Link to comment
Share on other sites

<?php
   //get horses info
   $funInfo = mysql_query("SELECT discipline, height FROM horses WHERE horseID=$horseID");
   list($discipline,$height) = mysql_fetch_row($funInfo);

   //prepare array to start being populated
   $shows = array();   
   $file = file("web_files/shows.txt");

   foreach ($file as $line)
      if (preg_match("/height:>=\s*$height/i", $line)
          && preg_match("/^(.*?)\\s*||.*discipline:==\\s*'$discipline'/i",$line,$match))
          $shows[] = $match[1];
?>

Link to comment
Share on other sites

Wildbug - Your code returns an empty array =/

I forgot to mention this, but not every line in the txt file has a height qualification, but they all have a discipline.

 

chigley - Your idea didn't work either. Instead of printing out the variable, literally, it just printed nothing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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