Page 1 of 1

How to put mysql queries into different arrays

Posted: Sun Jun 21, 2015 3:53 am
by amitblack
My Question related to arrays

$Parray=[1,2];

Table Structure:-

BrandName BrandType PostID
sss top 1
fsdds top 1
sr bottom 2


My Query Is:-
for($i=0;$i<sizeof($Parray);$i++){
$sql=mysql_query("SELECT BrandName,BrandPeople from BrandDetails where PostID='Parray[$i]'");
while($row=mysql_fetch_assoc($sql)){
$out[]=$row;
}
}
print json_encode($out);

my out is:- [{"BrandName":"sss","BrandType":"top"},{"BrandName":"fsdds","BrandType":"top"},{"BrandName":"sr","BrandType":"bottom"}]

BUT I NEED tow diffrent arrays for different postids:- {[{"BrandName":"sss","BrandType":"top"},{"BrandName":"fsdds","BrandType":"top"}],[{"BrandName":"sr","BrandType":"bottom"}]}

Re: How to put mysql queries into different arrays

Posted: Sun Jun 21, 2015 6:09 am
by requinix
As long as the outer {}s are supposed to be []s,

Code: Select all

[[{"BrandName":"sss","BrandType":"top"},{"BrandName":"fsdds","BrandType":"top"}],[{"BrandName":"sr","BrandType":"bottom"}]]
then use two arrays: one for the end result like the $out you have now, and another for just the items you get from each query.

Code: Select all

$out=array(); // initialize your variables!
for($i=0;$i<sizeof($Parray);$i++){
	$sql=mysql_query("SELECT BrandName,BrandPeople from BrandDetails where PostID='Parray[$i]'");
	$rows=array();
	while($row=mysql_fetch_assoc($sql)){
		$rows[]=$row;
	}
	$out[]=$rows;
}

Re: How to put mysql queries into different arrays

Posted: Sun Jun 21, 2015 11:16 pm
by amitblack
Thanks requinix