How to put mysql queries into different arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amitblack
Forum Newbie
Posts: 2
Joined: Sun Jun 21, 2015 3:23 am

How to put mysql queries into different arrays

Post 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"}]}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to put mysql queries into different arrays

Post 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;
}
amitblack
Forum Newbie
Posts: 2
Joined: Sun Jun 21, 2015 3:23 am

Re: How to put mysql queries into different arrays

Post by amitblack »

Thanks requinix
Post Reply