pagegen Posted March 24, 2010 Share Posted March 24, 2010 Hi guys, I have a double nested loop.. The issue here is, if(str_replace(" ", "", $row_dataset['numbers']) == str_replace(" ", "", $tps_num)) { $row_dataset['numbers'] is "01125336538" and $tps_num is also "01125336538" but the if statment wont echo what its spose too.. while($row_dataset = mysql_fetch_array($result_get_dataset)){ while($row_tps = mysql_fetch_array($result_get_tps)){ $test++; $tps_num = $row_tps['numbers']; if(str_replace(" ", "", $row_dataset['numbers']) == str_replace(" ", "", $tps_num)) { $tps_numbers = $tps_numbers . $row_tps['numbers']; echo $current_number . " | " . $row_tps['numbers'] . "<br/>"; } } } Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/ Share on other sites More sharing options...
pagegen Posted March 24, 2010 Author Share Posted March 24, 2010 All help will be usefull guys, any suggestions at all? Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031123 Share on other sites More sharing options...
teamatomic Posted March 24, 2010 Share Posted March 24, 2010 str_replace does not return 1/0 or TRUE/FALSE, it returns the string it work on. So if it does not work as you expected its possible that your string are not ==. Depending on where tps_num comes from perhaps trimming it would help. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031134 Share on other sites More sharing options...
pagegen Posted March 24, 2010 Author Share Posted March 24, 2010 Hi I have changed the code while($row_dataset = mysql_fetch_array($result_get_dataset)){ $current_number = str_replace(" ", "", $row_dataset['numbers']); while($row_tps = mysql_fetch_array($result_get_tps)){ $test++; $tps_num = str_replace(" ", "", $row_tps['numbers']); if($current_number == $tps_num) { $tps_numbers = $tps_numbers . $row_tps['numbers']; echo $current_number . " | " . $row_tps['numbers'] . "<br/>"; echo "match found"; } echo $current_number . " | " . $row_tps['numbers'] . "<br/>"; } } without the if it does echo the both numbers 01125336538 | 01125336538 bit with the if, nothing happens.. Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031140 Share on other sites More sharing options...
priti Posted March 24, 2010 Share Posted March 24, 2010 you are printing echo $current_number . " | " . $row_tps['numbers'] . "<br/>"; try echo $current_number . " | " . $tps_num . "<br/>"; Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031143 Share on other sites More sharing options...
pagegen Posted March 24, 2010 Author Share Posted March 24, 2010 Hi, just tried that, no luck Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031147 Share on other sites More sharing options...
pagegen Posted March 24, 2010 Author Share Posted March 24, 2010 i have also tried $current_number = "01125336538"; if($row_tps['numbers'] == $current_number) no luck with that either.. but if I echo $row_tps['numbers'] its the same number... Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031150 Share on other sites More sharing options...
teamatomic Posted March 24, 2010 Share Posted March 24, 2010 Change the if comparison to != and see what happens. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031152 Share on other sites More sharing options...
priti Posted March 24, 2010 Share Posted March 24, 2010 01125336538 | 01125336538 Can you remove those leading zero and try... It may be possible that these number are internally taken as octal... so either you can cast your these two number in int and then compare.. or just give a try removing those leading zeros. Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031155 Share on other sites More sharing options...
pagegen Posted March 24, 2010 Author Share Posted March 24, 2010 while($row_dataset = mysql_fetch_array($result_get_dataset)){ $current_number = str_replace(" ", "", $row_dataset['numbers']); if ( $current_number{0} == "0" ) $current_number = substr($current_number, 1); while($row_tps = mysql_fetch_array($result_get_tps)){ $test++; $tps_num = str_replace(" ", "", $row_tps['numbers']); if ( $tps_num{0} == "0" ) $tps_num = substr($tps_num, 1); if($current_number == $tps_num) { $tps_numbers = $tps_numbers . $row_tps['numbers']; echo $current_number . " | " . $tps_num . "<br/>"; echo "match found"; } echo $current_number . " | " . $tps_num . "<br/>"; } } Just removed the 1st character of both when echoed without if() 1125336538 | 1125336538 when if used, nothing Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031158 Share on other sites More sharing options...
pagegen Posted March 24, 2010 Author Share Posted March 24, 2010 Change the if comparison to != and see what happens. HTH Teamatomic Tried this if($current_number != $tps_num) { the following is the echo.. 1125336538 | 1125336538 match found Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031160 Share on other sites More sharing options...
teamatomic Posted March 24, 2010 Share Posted March 24, 2010 Then trim the vars before you use them HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031163 Share on other sites More sharing options...
pagegen Posted March 24, 2010 Author Share Posted March 24, 2010 Then trim the vars before you use them HTH Teamatomic That seems to work Thanks alot guys for all your help,, really really helped Link to comment https://forums.phpfreaks.com/topic/196336-nested-while-loop-with-if-statment-not-working/#findComment-1031166 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.