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


	}

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

	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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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