/    Sign up×
Community /Pin to ProfileBookmark

Reading Text File – If statement issue

Hi,

I’m reading a tab-delimited text file via PHP. This text holds an array of 13 elements. When the available column is less than or equal to zero, I highlight the entire table row in red. A new function I’m implementing is if the LECTURE to a science course has a lab component I added a Y/N column to the text file to check against. Here are the problems:

The yellow doesn’t highlight the LECTURE row when I have a Y value
If one of the science courses is closed, the other science times don’t remain yellow if they’re still open.

Here’s my code:

[code=php]<?php
$theFile=fopen(“branson_fall_test.txt”, “r”);
$firstLine=fread($theFile,19);

//error check to ensure we can open the text file and get results back, if not, display a //warning.
if(!$theFile)
{
echo “Couldn’t open the data file. Try again later.”;
}
else
{
//read the contents of the text file
while(!feof($theFile))
{
//split the tab delimited file up
$data=explode(“t”, fgets($theFile));
if (sizeof($data) == 13)
{

if ($data[12] == “Y” && $data[6]==”LECTURE”){
//-if you want the lab line to be yellow too then you could tack on a or here
//-if ($data[12] == “Yn” || $data[6] == ” LAB”){
print “<tr “;
print “class=”lab””;
print “>”;
}
else if ($data[5] <= 0 && $data[6] != ” LAB”){
print “<tr “;
print “class=”closed””;
print “>”;
}
else{
print “<tr>”;

}
for ($i=0; $i<12; $i++)
{
print “<td>$data[$i]</td>n”;

}
print “</tr>n”;
}
}

//done with the file, call the fclose function, passing variable
fclose($theFile);
}
?>
[/code]

Here’s an address:

[url]http://midwestwebdesign.net/bransontest.php[/url]

It has to be something simple I’m missing, any tips would be appreciated.

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@gtzpowerApr 15.2007 — I downloaded your code and added the following line right before your for loop outputs the <td> statments

[CODE]echo "<td>'$data[12]' '$data[6]'</td>";[/CODE]

This revealed the problem. the result output looked like this:

[CODE]<tr><td>'Y
' 'LECTURE'</td>[/CODE]


Notice the line break after the Y. change your if statement that looks for a "Y" to check for "Yn". this fixed the problem, and i have colored rows now. The line break shows because that is where it lies in the text file. (your Y/N is at the end of the rows followed by a return)

cheers!
Copy linkTweet thisAlerts:
@ryanbutlerauthorApr 15.2007 — Thanks for the help. Unfortunately, I did that yesterday, and for some reason, it still doesn't work for me. My revised code:

[code=php]
<?php
$theFile=fopen("branson_fall_test.txt", "r");
$firstLine=fread($theFile,19);
//error check to ensure we can open the text file and get results back, if not, display a //warning.
if(!$theFile)
{
echo "Couldn't open the data file. Try again later.";
}
else
{
//read the contents of the text file
while(!feof($theFile))
{
//split the tab delimited file up
$data=explode("t", fgets($theFile));
if (sizeof($data) == 13){

if ($data[12] == "Yn"){
//-if you want the lab line to be yellow too then you could tack on a or here
//if ($data[12] == "Yn" || $data[6] == " LAB"){
print "<tr ";
print "class="lab"";
print ">";
}
else if($data[5] <= 0 && $data[6] != " LAB"){
print "<tr ";
print "class="closed"";
print ">";
}
else{
print "<tr>";

}
for ($i=0; $i<12; $i++)
{
print "<td>$data[$i]</td>n";

}
print "</tr>n";
}
}

//done with the file, call the fclose function, passing variable
fclose($theFile);
}

?>
[/code]
Copy linkTweet thisAlerts:
@rafApr 15.2007 — 
[CODE]echo "<td>'$data[12]' '$data[6]'</td>";[/CODE]

[/QUOTE]

would give the output <td>$data[12] $data[6]</td> because the single quotes would cause PHP to not parse $data[12] as a variable.

try concatenating or escaping
[code=php]
echo "<td>"$data[12]"</td>";
//or
echo '<td>' . $data[12] . ' ' . $data[6]. '</td>';
[/code]
Copy linkTweet thisAlerts:
@ryanbutlerauthorApr 15.2007 — That's not the problem. The problem is that "Y" is a carriage return but it's not being recognized as that in my IF statement:

[code=php]if(trim($data[12])=="Yn")[/code]

Everything else if working fine.
Copy linkTweet thisAlerts:
@NogDogApr 15.2007 — [code=php]
if(trim($data[12])=="Yn")
[/code]
[/quote]

trim() will get rid of any leading/trailing newlines characters (as well as spaces, tabs, and carriage-returns), so that if condition will never evaluate as true. Try:
[code=php]
if(trim($data[12])=="Y")
[/code]
Copy linkTweet thisAlerts:
@gtzpowerApr 15.2007 — would give the output <td>$data[12] $data[6]</td> because the single quotes would cause PHP to not parse $data[12] as a variable.

try concatenating or escaping
[code=php]
echo "<td>"$data[12]"</td>";
//or
echo '<td>' . $data[12] . ' ' . $data[6]. '</td>';
[/code]
[/QUOTE]


Nope, php will parse these just fine. try it. I suppose it is possible that there is a server setting that could disable this, but my local PHPdev parses it fine, and so does my live hosting server (servage.net).

Your way would work too though, but the point is that they both work.
Copy linkTweet thisAlerts:
@gtzpowerApr 15.2007 — here's the exact code im using right now, and it works. I don't have your stylesheet, so i defined the style in the <td>, but you should be able to work it out from there.

[code=php]<?php
$theFile=fopen("branson_fall_test.txt", "r");
$firstLine=fread($theFile,19);
//error check to ensure we can open the text file and get results back, if not, display a //warning.
if(!$theFile)
{
echo "Couldn't open the data file. Try again later.";
}
else
{
//read the contents of the text file
while(!feof($theFile))
{
//split the tab delimited file up
$data=explode("t", fgets($theFile));
if (sizeof($data) == 13){

if (trim($data[12]) == "Y"){
//-if you want the lab line to be yellow too then you could tack on a or here
//if ($data[12] == "Yn" || $data[6] == " LAB"){
print "<tr ";
print "style="background: #FFFF00"";
print ">";
}
else if($data[5] <= 0 && $data[6] != " LAB"){
print "<tr ";
print "class="closed"";
print ">";
}
else{
print "<tr>";

}
for ($i=0; $i<12; $i++)
{
print "<td>$data[$i]</td>n";

}
print "</tr>n";
}
}

//done with the file, call the fclose function, passing variable
fclose($theFile);
}

?> [/code]
Copy linkTweet thisAlerts:
@rafApr 15.2007 — i downloaded XAMPP, and it wont parse any variables without concatination it is using the standard options set by PHP
Copy linkTweet thisAlerts:
@gtzpowerApr 15.2007 — Hmm, i dont know anything about XAMPP. must be different config settings.
Copy linkTweet thisAlerts:
@NogDogApr 15.2007 — i downloaded XAMPP, and it wont parse any variables without concatination it is using the standard options set by PHP[/QUOTE]
First, this should be in it's own thread (or at least in a related thread). Please do not "hijack" other people's threads with unrelated questions.

Second, you've not shown us your code, so it is hard for us to determine what you may have done wrong. My guess is that you have used single quotes instead of double quotes, as variables are not interpolated inside of single-quoted strings. See http://www.php.net/manual/en/language.types.string.php for more information.
Copy linkTweet thisAlerts:
@ryanbutlerauthorApr 16.2007 — It was the use of trim and the n (new line character). This works fine:

[code=php]if(trim($data[12]=="Y")[/code]

Thanks for the help!
×

Success!

Help @ryanbutler spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...