if statement - PHP: If not selecting correctly -


i building list based off of csv file. here 2 common lines in csv file:

10,11,12,13,14,15,16     12,13,14,15,16,0,0 

the file read line-by-line , stored list. here code:

while(($current_line = fgets($find_products, 4096)) !== false) {     list($weight1, $weight2, $weight3, $weight4, $weight5, $weight6, $weight7) = explode(",", $current_line); } 

i @ value of each item in list, , print if value not 0. here code:

if($weight1 != '0') {     print '<option>'.$weight1.' lb.</option>'; }  if($weight2 != '0') {     print '<option>'.$weight2.' lb.</option>'; }  if($weight3 != '0') {     print '<option>'.$weight3.' lb.</option>'; }  if($weight4 != '0') {     print '<option>'.$weight4.' lb.</option>'; }  if($weight5 != '0') {     print '<option>'.$weight5.' lb.</option>'; }  if($weight6 != '0') {     print '<option>'.$weight6.' lb.</option>'; }  if($weight7 != '0') {     print '<option>'.$weight7.' lb.</option>'; } 

here results of code after run on csv file above 2 lines:

10 lb. 11 lb. 12 lb. 13 lb. 14 lb. 15 lb. 16 lb. 

and

12 lb. 13 lb. 14 lb. 15 lb. 16 lb. 0 lb. 

the 0 lb. should not showing based on value of $weight6 , $weight7. have printed values of these 2 variables, , appropriately show 0. can confirm through testing if-statement $weight7 culprit, have no idea causing issue. appreciated!

the reason fgets() returns line newline @ end. $weight7 "0\r\n" (that's why var_dump says length 3), not "\0". should trim line before split it, remove this.

while(($current_line = fgets($find_products, 4096)) !== false) {     $current_line = trim($current_line);     list($weight1, $weight2, $weight3, $weight4, $weight5, $weight6, $weight7) = explode(",", $current_line); } 

or instead use fgetcsv() rather fgets(). automatically trims line , splits @ separator, returning array can iterate over.

while ($weights = fgetcsv($find_products)) {     foreach ($weights $w) {         if ($w != '0') {             echo "<option>$w lb.</option>";         }     } } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -