views:

60

answers:

1

Hi guys,

ok, so I have a database comprising of two tables, products and suppliers.

All suppliers fill in a form and their data is then stored in the suppliers table, and the products table contains a list of all of the products, so when the supplier fills in the form, he can choose as many products as he wishes as I use jQuery JSON and AJAX to get the list of all of the products and then populate a drop down list with all of them in it, which can then be cloned as many times as is needed.

The problem I am sitting with now is, how do I insert all of the different products the supplier chooses into the supplier table, or should I rather just relate all of the products he chooses to the one supplier for better normalization since all the products are already there?

I will be using jQuery $.ajax to POST the form data in JSON format to a waiting PHP file, which will then parse it and insert the data into the database.

So basically, I need to figure out how to relate the data in the database to achieve the best normalization possible, and I need to figure out a way of inserting a variable amount of products into the suppliers table or find a way to relate the many products he chooses to the one supplier.

I am very new to relational databases, so any advice on how to proceed would be a great help, so would any other advice you guys may have!

The jQuery code I use to populate clone and POST the products the supplier chooses:

$(document).ready(function() {

        var count = 0;      

        //when clicked it will remove the closest div with a class of 'container'
        $("span.remove").live('click', function(){
            $(this).closest("div.container").fadeOut(400, function(){
                $(this).remove();
                $('#button').attr('disabled','');
            });
        });

        //initialize the button
        $('#button').attr('disabled','');
        $('#button').click(function(){

            var count = $("#systems_wrapper > .container").size();
            var lastID = $("#systems_wrapper > .container:last").attr('id');
            var exploded = lastID.split("_");
            var increment = Number(exploded[1])+1;

            //if the user has selected 5 products, disable the 'add' button
            if(count >= 5){
                $('#button').attr('disabled','disabled');
            }else {
                $('#button').attr('disabled','');
            }

            //clone the first drop down and give it a different ID, as well as it's child elements
            var test = $('#systems_0.container').clone().attr('id', 'system_' + increment).appendTo('#systems_wrapper');
            test.children(':nth-child(2)').append('<span class="remove"></span>');
            test.children(':nth-child(2)').children(':first').attr('id', 'mail_' + increment).attr('class','dropDowns').attr('onchange','test();');


            });


    //get the products JSON object returned from test_post.php and run the necessary functions on the returned data
    $.getJSON("test_post.php", function(data){

    //clean out the select list
    $('#box').html('');

        //run the loop to populate the drop down list
        $.each(data, function(i, products) {
            $('#box').append(
                $('<option></option>').html(products.products)
            );
        });
    });
});


//this gets all of the products chosen and then gets each ones value and ID, and then posts it to the qwer.php file

function test(){
    var sections = $('#systems_wrapper').find('.dropDowns');
    var newArray = new Array();

    sections.each(function(){
        var id = $(this).attr('id');
        var val = $(this).val();
        var o = { 'id': id, 'value': val };

        newArray.push(o);
    });

    alert(newArray);

    $.ajax({
            type: "POST",
            url: "qwer.php",
            dataType: 'json',
            data: { json: JSON.stringify(newArray) }
        });

}

Thanx in advance!

+2  A: 

If i understand the problem correctly from a database level, should you be using an intermediate table called something like ProductSupplier containing a Product_ID and Supplier_ID column.

Then when a supplier selects a product, add both the supplier and product id to a new column in this table.

This will allow multiple suppliers to pick the same product and multiple products to be picked by the same supplier.

EDIT: I meant to say "add both the supplier and product id to a new ROW in this table"

Andy
...and it's called "many-to-many relationship".
Constantin
Any idea on how to enter each of the products into that table then? Can I use something like a foreach loop?
I have never used jQuery before, but by the looks of your code, you have an array of products ids (as integers) called newArray which contains all of the selected products.In c# the syntax would be "foreach (int id in newArray) { /*Code to insert single row*/ }"
Andy
Thanx, will give it a go.
The php for this would be something like "foreach ($newArray as $Product_ID) { /*Write line to db*/ }". Apologies for not being able to help with the jQuery side of it. ( http://php.net/manual/en/control-structures.foreach.php )
Andy
Not a problem, thanx alot!