Jump to content

nested while loop with if statment not working


pagegen

Recommended Posts

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/>";
				}
		}


	}

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

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..

 

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.

	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 :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.