Parobola Posted July 19, 2011 Share Posted July 19, 2011 Been beating my head against the wall over this problem for awhile and can't figure out what's going on with it. Consider the following code(I will bold the problem area): $newLine = fgets($tempData); WHILE(!feof($tempData)) { IF(!empty($newLine) ) { list($info, $amount, $tax) = explode(":" , $newLine); $tax = strtolower($tax); $pdf->Write(10, $tax); $pdf->Ln(10); $pdf->Cell(140, 10, ''.$info.'', 1, 0, "L"); IF ($amount == "N/A") { $pdf->Cell(20, 10, '', 1, 0); $pdf->Cell(20, 10, '', 1, 0); $pdf->Ln(10); } ELSE { $pdf->Cell(20, 10, '$'.number_format($amount, 2).'', 1, 0, 'R'); $subTotal = $subTotal + $amount; IF ($tax == "non-taxable") { $pdf->Cell(20, 10, '', 1, 0); $totalNonTaxable = $totalNonTaxable + $amount; $pdf->Ln(10); } ELSE { $taxAmount = $amount * 0.07; $totalTaxable = $totalTaxable + $amount; $pdf->Cell(20,10,'$'.number_format($taxAmount, 2).'', 1, 0, 'R'); $pdf->Ln(10); } } } $newLine = fgets($tempData); } The problem occurs on output. As you can see I'm reading from a text file with the fields of "Info field":"amount field":"Taxable field" on each line. The "Info field" contains a string of text, the "amount field" contains either "N/A" or a whole number amount i.e. 200.00, the "Taxable field" contains one of 3 possible strings either "N/T"(only there if amount field contains "N/A", "Taxable", or "Non-Taxable". Since I first test for the $amount variable to be "N/A" I don't need to test for "N/T" to be stored in $tax. What happens is when I test for IF ($tax == "non-taxable") I always get a false result meaning the program always calculates tax when there is a whole number stored in $amount. I've tried working this test several different ways and cannot get it to NOT calculate tax when the value in $tax is NOT "non-taxable". Other then that the program does exactly what I intended it to do which is read the data and print it out in the specified format to a new .pdf document to be saved or printed. Please help! Quote Link to comment https://forums.phpfreaks.com/topic/242295-need-help-with-nested-ifelse/ Share on other sites More sharing options...
dcro2 Posted July 19, 2011 Share Posted July 19, 2011 Have you tried outputting $tax to see what it actually contains? Because if these fields are wrapped in double quotes then $tax still has double quotes inside it, and it will not match "non-taxable", but will match "\"non-taxable\"" (or '"non-taxable"'). PS: please use or tags (although, you can't bold inside them). Quote Link to comment https://forums.phpfreaks.com/topic/242295-need-help-with-nested-ifelse/#findComment-1244384 Share on other sites More sharing options...
Parobola Posted July 19, 2011 Author Share Posted July 19, 2011 I apologize, did not know about the forum ability, but now I do and will do so in the future! Yes I have tried outputting the text in $tax by using the top portion of the WHILE loop as seen in the code. WHILE(!feof($tempData)) { IF(!empty($newLine) ) { list($info, $amount, $tax) = explode(":" , $newLine); $tax = strtolower($tax); $pdf->Write(10, $tax); //checking the value of $tax $pdf->Ln(10); //using a spacer to validate sorry if I was not as clear, the text file contains the following: We are testing my program:450:Non-Taxable This is just a test:N/A:N/T We like to test our code to make sure it works.:N/A:N/T Testing value again:200:Taxable Test Test Tes:N/A:N/T This is exactly what is inside the text file. As you can see it contains all three of the possibilities for my $tax field but generates the same result everytime, except when $amount field had N/A stored in it and then my output is correct as having two empty cells as place holders in the .pdf Quote Link to comment https://forums.phpfreaks.com/topic/242295-need-help-with-nested-ifelse/#findComment-1244392 Share on other sites More sharing options...
dcro2 Posted July 19, 2011 Share Posted July 19, 2011 fgets doesn't take out the ending newline in each line: Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line. Which is why it never matches "non-taxable", it's actually "non-taxable\n". A nice trim should take care of it: list($info, $amount, $tax) = explode(":" , trim($newLine)); Quote Link to comment https://forums.phpfreaks.com/topic/242295-need-help-with-nested-ifelse/#findComment-1244409 Share on other sites More sharing options...
Parobola Posted July 19, 2011 Author Share Posted July 19, 2011 fgets doesn't take out the ending newline in each line: Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line. Which is why it never matches "non-taxable", it's actually "non-taxable\n". A nice trim should take care of it: list($info, $amount, $tax) = explode(":" , trim($newLine)); Thanks a bunch man! Works like a charm! Quote Link to comment https://forums.phpfreaks.com/topic/242295-need-help-with-nested-ifelse/#findComment-1244410 Share on other sites More sharing options...
dcro2 Posted July 19, 2011 Share Posted July 19, 2011 You're welcome! Quote Link to comment https://forums.phpfreaks.com/topic/242295-need-help-with-nested-ifelse/#findComment-1244416 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.