Hi…
I’m trying to put data from 2 mysql tables into a javascript associated array…Pray how may I achieve this.
This is my SQL and database structure:
http://sqlfiddle.com/#!9/9fba42/1
This is my expected output:
var Select_List_Data = {
'choices[]': {
'Abarth': {
text: ['500'],
value: [1]
},
'AC': {
text: ['Cobra'],
value: [2]
},
'Alfa Romeo': {
text: ['4C', '156', 'Brera', '159'],
value: [5, 3, 6, 4]
}
}
};
I am terribly confused with while/for/foreach loops and just going around in circles trying to nest the loops and everything just goes haywire. Please recommend the proper approach to do this?
I mention loops because there are 2 tables, one parent and one child table, and looping through each parent and then looping through the children isn’t providing the correct results:
$sql = "SELECT c.car_makes_id parent_id, c.car_makes_model_name parent_name, d.car_models_id child_id, d.car_models_model child_name
FROM car_makes AS cars
LEFT JOIN car_models AS models ON cars.car_makes_id = models.car_models_make
ORDER BY cars.car_makes_id ASC, models.car_models_make ASC
";
$stmt = $db->prepare($sql);
$result = $stmt->execute();
$stmt_result = $stmt->get_result();
if ($stmt_result->num_rows > 0) {
while ($row = $stmt_result->fetch_assoc()) {
/*I have the results by calling $row['example'] but how to make an array in the above format for using in JavaScript?*/
}
}