Click here to Skip to main content
15,851,514 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this i fetch data from mysql database and store into $row[] array then try to compare array value but it doesn't work:

mytable:
bloodbank_id|date  |time |bloodgroup|quantity
   1        |1-1-12|12:00| A+       | 10
   1        |1-1-12|12:00| B+       | 1
   1        |1-1-12|12:00| O+       | 9
   2        |1-1-12|12:00| A+       | 1
   2        |1-1-12|12:00| B+       | 10

i try to compare bloodbank_id but not work

this is my code with output:
this my code:

PHP
while($row=mysqli_fetch_assoc($select)){
			$toarr[]=$row;
			foreach($row as $value){
			echo $value.'<br>';
		}
		}
		
		$count=count($toarr);
		/*echo 'Array size'.$count;*/
		/*print_r($toarr);*/
		
		
		For($i=0,$j=0;$i<$count;$i++){
			if($toarr[$j]['bloodbank_id']===$toarr[$i+1]['bloodbank_id']){
				echo 'match==>'.$toarr[$j]['bloodbank_id'].'='.$toarr[$i+1]['bloodbank_id'].'<br>';
			}else{
				echo 'not match==>'.$toarr[$j]['bloodbank_id'].'='.$toarr[$i+1]['bloodbank_id'].'<br>';
				$j=$i;
				echo 'j='.$j;
			}
		}

output:
// print data which store in array
1
2014-04-27
14:30:09
A+
10

1
2014-04-27
14:30:24
B+
10

1
2014-04-27
14:46:05
AB+
1

3
2014-04-27
14:32:14
AB+
10

match==>1=1// first row match with next row
match==>1=1
not match==>1=3 //if not match then assign $i value to $j
j=2
Notice: Undefined offset: 4 in C:\xampp\htdocs\AES\blood.php on line 24

Notice: Undefined offset: 4 in C:\xampp\htdocs\AES\blood.php on line 27
not match==>1=
j=3


error in for loop condition not exit after $i=4
Posted
Updated 28-Apr-14 19:38pm
v3
Comments
Sergey Alexandrovich Kryukov 29-Apr-14 0:11am    
In a loop, elements one by one. What have you tried?
—SA
priyanshbhaliya 29-Apr-14 0:39am    
here i put my code please check it correct little error.. solve it

1 solution

i solve it:
PHP
$count=count($toarr);
		/*echo 'Array size'.$count;*/
		/*print_r($toarr);*/
		For($i=1,$j=0;$i<$count;$i++){
			echo 'j='.$j.'  i='.$i.'     ';
			echo $toarr[$j]['bloodbank_id'].' '.$toarr[$i]['bloodbank_id'];
			if($toarr[$j]['bloodbank_id']==$toarr[$i]['bloodbank_id']){
				echo '   match <br>';
			}else{
				echo ' not match  ';
				$j=$i;
				echo 'j='.$j.'<br>';
			}
			
		}
</br></br>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900