Some help converting mysql data to Javascript format below?

I have 2 mysql tables, one parent table (75 rows) and one child table (650+ rows) that has a foreign key connecting each child to a parent. I done numerous attempts to try to get them in this format but I keep failing:

// object literal holding data for option elements
var Select_List_Data = {
    
    'choices': { // name of associated select box
        
        // names match option values in controlling select box
        parent_js: {
            text: ['Scrolling Divs', 'Tooltips', 'Rotate Images', 'Scrollers', 'Banner Rotator'],
            value: ['scroll', 'tooltips', 'rotate', 'scrollers', 'banner']
        },
        parent_php: {
            text: ['Random Image', 'Form Class', 'Table Class', 'Order Form'],
            value: ['random', 'form', 'table', 'order']
        },
        parent_tuts: {
            // example without values
            text: ['Iframes', 'PHP to JS', 'Object Literals', 'Initializing JS']
        }
    
    }    
};

This is my code so far:

$program_makes = "";
$sql = "SELECT c.program_makes_id make_id, c.program_makes_model_name make, d.program_models_make, d.program_models_model model
FROM program_makes AS c
LEFT JOIN program_models AS d ON c.program_makes_id = d.program_models_make
ORDER BY c.program_makes_id ASC, d.program_models_make ASC;";
$stmt = $db->prepare($sql);
$result = $stmt->execute();
$stmt_result = $stmt->get_result();

$data = array(); 

if ($stmt_result->num_rows > 0) {
	while ($row = $stmt_result->fetch_assoc()) {
	     $data[] = array("make"=>$row["make"], "model"=>$row["model"]); 
	     $post_data = json_encode(array('max' => 46, 'data' => $data));
		// $program_makes .= "<option value='".$row['program_models_model']."'>".$row['program_models_model']."</option>";     					
	}
}

    echo $post_data;


?>
	<script>
	
jQuery.extend({
getValues: function(url) {
    var result = null;
    $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            result = JSON.stringify(data);
        }
    });
   return result;
}
});

var testData = $.getValues("test.php");
</script>

You have to give more details, we neither know what exactly you did, which result you got, nor what “failing” means in terms of your project. But at least you are overwriting $post_data again and again, json_encode hast to be the last step before you echo it, so everything must be in an array already.

1 Like

I don’t know what you are ultimately after, but a few things stick out.

Now what is the max column? If that is the max records you want back, you are going to want that included in you SQL query to prevent from over selecting what won’t be used to begin with.

Sponsor our Newsletter | Privacy Policy | Terms of Service