I create a json encode in PHP like this:
$get = mysql_query("SELECT * FROM $table") or die(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($get)){
$data[$row['id']][0] = $row['g'];
$data[$row['id']][1] = $row['w'];
$data[$row['id']][2] = $row['s'];
$data[$row['id']][3] = $row['name'];
$data[$row['id']][4] = $row['type'];
}
$data4 = json_encode($data);
I assign it to a js variable: var bdata;
So what im trying to do is now loop the content filtered by [4] = type.
This is my loop:
for(var key in bdata){
//stuff here
}
But this will loop all of them… but I want to loop only ones that have a [4] (type) of a certain value lets say the value was “2”.
Is there a way to do that?
You are looping through your rows. Not your columns. For each row there is a certain column named type. If you want to process it you can fetch it. They way you are constructing the JSON, it’ll soon be enough unreadable at JS side that you’ll pull you hair.
So Its better to have named keys.
You should have done it in this way,
On the JS end.