iPixel Posted February 11, 2011 Share Posted February 11, 2011 Unless i'm mistaken, the part within the IF statement should only occur if $table_res['Field'] is found within $data but instead it pushes to the array no matter what! #Read the file that was just uploaded for use $csvfile = "csv/datafile.csv"; $fh = fopen($csvfile,"r") or die("Could not open rename.csv file !"); #GET ALL THE FIELDS WITHIN THE TABLE STRUCTURE #USE IT TO COMPARE AGAINST THE COLUMNS IN THE CSV FILE #MAP WHICH COLUMN BELONGS TO WHAT FIELD $tbls_sql = "SHOW COLUMNS FROM $tablename"; $tbls_qry = mysql_query($tbls_sql) or die(mysql_error()); $fieldarray = array(); $keyarray = array(); $data = fgetcsv($fh); while($tbls_res = mysql_fetch_assoc($tbls_qry)) { #echo $tbls_res['Field'] . "<BR>"; if($getkey = array_search($tbls_res['Field'],$data)) { array_push($fieldarray,$tbls_res['Field']); array_push($keyarray,$getkey); } else { echo ""; } } Quote Link to comment https://forums.phpfreaks.com/topic/227358-why-does-my-if-statement-fire-improperly/ Share on other sites More sharing options...
Nodral Posted February 11, 2011 Share Posted February 11, 2011 Hi Try putting an extra = sign in the if test. currently you are redefining the variable whereas if you have == this will compare the two. ie if($getkey == array_search($tbls_res['Field'],$data)) Quote Link to comment https://forums.phpfreaks.com/topic/227358-why-does-my-if-statement-fire-improperly/#findComment-1172698 Share on other sites More sharing options...
iPixel Posted February 11, 2011 Author Share Posted February 11, 2011 That actually made things more confusing, now not only does it occur improperly, it also array_pushes randomized info as opposed to what it should. Quote Link to comment https://forums.phpfreaks.com/topic/227358-why-does-my-if-statement-fire-improperly/#findComment-1172699 Share on other sites More sharing options...
Nodral Posted February 11, 2011 Share Posted February 11, 2011 Ok, i'm pretty new to this, but it seems like you are trying to perform an IF test and define a variable in the same line. See if the following makes sense. Perform the test, then if TRUE, define the $getkey variable? if(array_search($tbls_res['Field'],$data)) { $getkey = $tbls_res['Field']; array_push($fieldarray,$tbls_res['Field']); array_push($keyarray,$getkey); } else { echo ""; } } Quote Link to comment https://forums.phpfreaks.com/topic/227358-why-does-my-if-statement-fire-improperly/#findComment-1172701 Share on other sites More sharing options...
iPixel Posted February 11, 2011 Author Share Posted February 11, 2011 Yea i tried that as well beforehand, same results... Got it working like this .... $getkey = array_search($tbls_res['Field'],$data); if($getkey != "") { array_push($fieldarray,$tbls_res['Field']); array_push($keyarray,$getkey); } Quote Link to comment https://forums.phpfreaks.com/topic/227358-why-does-my-if-statement-fire-improperly/#findComment-1172707 Share on other sites More sharing options...
iPixel Posted February 11, 2011 Author Share Posted February 11, 2011 Now however my issue with that is, that if $getkey = 0 it omits it. ARGH! Quote Link to comment https://forums.phpfreaks.com/topic/227358-why-does-my-if-statement-fire-improperly/#findComment-1172709 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2011 Share Posted February 11, 2011 There's a note in the array_search documentation that will help. You need to test if the value is exactly (===) or not-exactly (!==) a FALSE value. Quote Link to comment https://forums.phpfreaks.com/topic/227358-why-does-my-if-statement-fire-improperly/#findComment-1172720 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.