pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 23, 2007 Share Posted June 23, 2007 <?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]; ?> Quote Link to comment Share on other sites More sharing options...
chigley Posted June 23, 2007 Share Posted June 23, 2007 if (trim($req[0]) != "") $statement .= "{$f[$req[0]} {$req[1]}"; Try that? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Author Share Posted June 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Author Share Posted June 23, 2007 Any other ideas? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 25, 2007 Share Posted June 25, 2007 Any other ideas? Well, you get the idea -- use preg_match to find the event in lines containing the parameters you expect. That's the idea. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.