简体   繁体   中英

Build Multidimensional Array in Php With Mysql Data

I have the following multidimensional javascript array.

var accounts =
    [
        { name: '069070474', crd_no:
        [
            {name: '0215020174357351', ssn_no: ['582238797'] }
        ]},
        { name: '089255135', crd_no:
        [
            {name: '0215020178346863', ssn_no: ['583872782','874514213']}
        ]},
        { name: '123456789', crd_no:
        [
            {name: '8888888888888888', ssn_no: ['122121212']}
        ]},
        { name: '131274740', crd_no:
        [
            {name: '0215020178888432', ssn_no: ['478498545','584586942']}
        ]},
        { name: '454296191', crd_no:
        [
            {name: '0215020178896484', ssn_no: ['582214564']}
        ]},
        { name: '987654321', crd_no:
        [
            {name: '8888888888888888', ssn_no: ['122121212']}
        ]}
    ];

And I also have the above data in a mysql table with the following schema:

TABLE `profile_id`
(
`acct_no` varchar(19) NOT NULL COMMENT 'Customer account no',
`crd_no` varchar(19) NOT NULL COMMENT 'Customer card no',
`ssn_no` varchar(9) NOT NULL COMMENT 'Customer social security number',
PRIMARY KEY  (`acct_no`,`crd_no`,`ssn_no`)
)

What I'm trying to do is using php retrieve the data from the mysql table, and create either a multidimensional array or a json encoded string that matches my existing javascript array. I want to have the array built in a loop so as new records are added to the table, the array will constantly be updated.

Exact code is not a necessity (but it is welcome), I am merely looking for suggestions on how to approach this. I can get the array built and filtered on unique combinations of the acct_no and crd_no or all three, but I have yet to get the array built in php looking exactly like how I have it in javascript. The ultimate goal is to feed the js array to a set of three drop down boxes. Each box's selected value will determine the next box's data selection list. Now I know I can set this up by using ajax to query the db when a selection is made from each box, but I am avoiding having to hit the server on multiple occasions. Any other recommendations on getting this done is also a plus.

The following is the javascript code that manipulates the data.

    $(document).ready(function()
    {
        document.soapFormSetup.reset();

        $(function()
        {
            var start = '';
            var options = '<option selected value="'+ start +'">-----------------------' + '</option>' ;

            for (var i = 0; i < accounts.length; i++)
            {
                var opt = accounts[i].name ;

                options += '<option value="' + opt + '">xxxxxxxxxxxxxx' + opt.substring(5) + '</option>';
            }

            $("#sms_acct_no").html(options);

            start = '';
            options = '<option selected value="'+ start +'">-----------------------' + '</option>' ;

            for (var i=0; i < accounts[0].crd_no.length; i++)
            {
                var opt = accounts[0].crd_no[0].name ;

                options += '<option value="' + opt + '">xxxxxxxxxxxxxx' + opt.substring(12) + '</option>';
            }

            $("#sms_crd_no").html(options);

            start = '';
            options = '<option selected value="'+ start +'">--------------' + '</option>' ;

            for (var i=0; i < accounts[0].crd_no[0].ssn_no.length; i++)
            {
                var opt = accounts[0].crd_no[0].ssn_no[i] ;

                options += '<option value="' + opt + '">xxx-xx-' + opt.substring(5) + '</option>';
            }

            $("#sms_ssn_no").html(options);

            document.soapFormSetup.sms_ssn_no.disabled=true;
            document.soapFormSetup.sms_crd_no.disabled=true;

            $("#sms_acct_no").bind("change",
            function()
            {
                if ( $(this).children(":selected").val() !== "" )
                    document.soapFormSetup.sms_crd_no.disabled=false;
                else
                {
                    document.soapFormSetup.sms_crd_no.value="";
                    document.soapFormSetup.sms_ssn_no.value="";

                    document.soapFormSetup.sms_crd_no.disabled=true;
                    document.soapFormSetup.sms_ssn_no.disabled=true;
                }

                for(var i=0; i<accounts.length; i++)
                {
                    if (accounts[i].name == this.value)
                    {
                        start = '';
                        var crd_nos = '<option selected value="'+ start +'">-----------------------' + '</option>' ;

                        for (var j=0; j < accounts[i].crd_no.length; j++)
                        {
                            var opt= accounts[i].crd_no[j].name ;

                            crd_nos += '<option value="' + opt + '">xxxxxxxxxxxxxx' + opt.substring(12) + '</option>';
                        }

                        break;
                    }
                }

                $("#sms_crd_no").html(crd_nos);

                for(var i=0; i<accounts.length; i++)
                {
                    for(var j=0; j<accounts[i].crd_no.length; j++)
                    {
                        if(accounts[i].crd_no[j].name == $("#sms_crd_no").val())
                        {
                            start = '';
                            var crd_ssn_nos = '<option selected value="'+ start +'">--------------' + '</option>' ;

                            for (var k=0; k < accounts[i].crd_no[j].ssn_no.length; k++)
                            {
                                var opt = accounts[i].crd_no[j].ssn_no[k] ;

                                crd_ssn_nos += '<option value="' + opt + '">xxx-xx-' + opt.substring(5) + '</option>';
                            }

                            break;
                        }
                    }
                }

                $("#sms_ssn_no").html(crd_ssn_nos);

                document.soapFormSetup.sms_ssn_no.disabled=true;
                document.soapFormSetup.sms_ssn_no.value="";
            });

            $("#sms_crd_no").bind("change",
                function()
                {
                    if ( $(this).children(":selected").val() !== "" )
                        document.soapFormSetup.sms_ssn_no.disabled=false;
                    else
                    {
                        document.soapFormSetup.sms_ssn_no.value="";

                        document.soapFormSetup.sms_ssn_no.disabled=true;
                    }

                    for(var i=0; i<accounts.length; i++)
                    {
                        for(var j=0; j<accounts[i].crd_no.length; j++)
                        {
                            if(accounts[i].crd_no[j].name == this.value)
                            {
                                start = '';
                                var ssn_nos = '<option selected value="'+ start +'">--------------' + '</option>' ;

                                for (var k=0; k < accounts[i].crd_no[j].ssn_no.length; k++)
                                {
                                    var opt = accounts[i].crd_no[j].ssn_no[k] ;

                                    ssn_nos += '<option value="' + opt  + '">xxx-xx-' + opt.substring(5)  + '</option>';
                                }

                                break;
                            }
                        }
                    }

                    $("#sms_ssn_no").html(ssn_nos);
                });
            });
    });

I believe the following code will produce an identical, JSON encoded string:

$accounts = array();
while($row = mysql_fetch_assoc($query)) {
    $accounts[] = array(
        "name"   => $row["acct_no"],
        "crd_no" => array(
            "name"   => $row["crd_no"],
            "ssn_no" => $row["ssn_no"]
        ) 
    );
}
echo json_encode($accounts);

Presumably, a single account name may be associated with multiple cards (otherwise, why would crd_no be an array) so try this:

// Create an array keyed by acct_no, containing arrays of crd_no data
$accounts = array();
while ($row = mysql_fetch_assoc($query)) {
    if (!isset($accounts[$row["acct_no"]])) {
       $accounts[$row["acct_no"]] = array();
    }
    $accounts[$row["acct_no"]][] = array(
        "name"   => $row["crd_no"],
        "ssn_no" => $row["ssn_no"]
    );
}

// but it's not the format you want so now...
$accounts_formatted = array();
foreach ($accounts as $name => $account) {
    $accounts_formatted[] = array(
        "name" => $name,
        "crd_no" => $account
    );
}
echo json_encode($accounts_formatted);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM