lundi 24 août 2015

PHP: If not selecting correctly

I am building a list based off of a CSV file. Here are the two most common lines in the CSV file:

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

The file is read line-by-line and stored into the list. Here is the code:

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

I look at the value of each item in the list, and print if the value is not 0. Here is the 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 are the results of the code after it is run on a CSV file with the above two 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 be showing up based on the value of $weight6 and $weight7. I have printed the values of these two variables, and they appropriately show up as 0. I can also confirm through testing that the if-statement for $weight7 is the culprit, but I have no idea what is causing the issue. Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire