Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Mysql数据在Php中构建多维数组_Php_Javascript_Jquery_Mysql - Fatal编程技术网

使用Mysql数据在Php中构建多维数组

使用Mysql数据在Php中构建多维数组,php,javascript,jquery,mysql,Php,Javascript,Jquery,Mysql,我有以下多维javascript数组 var accounts = [ { name: '069070474', crd_no: [ {name: '0215020174357351', ssn_no: ['582238797'] } ]}, { name: '089255135', crd_no: [ {name: '0215020178346863', ss

我有以下多维javascript数组

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']}
        ]}
    ];
我在mysql表中也有上述数据,其模式如下:

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`)
)
我尝试使用php从mysql表中检索数据,并创建一个多维数组或一个与现有javascript数组匹配的json编码字符串。我希望在循环中构建数组,以便在向表中添加新记录时,数组将不断更新

确切的代码不是必须的(但它是受欢迎的),我只是想寻求关于如何实现这一点的建议。我可以在acct_no和crd_no的独特组合上构建和过滤数组,或者三者都可以,但是我还没有让php中构建的数组看起来像javascript中的数组。最终目标是将js数组提供给一组三个下拉框。每个框的选定值将决定下一个框的数据选择列表。现在我知道,当从每个框中进行选择时,我可以通过使用ajax查询db来进行设置,但我避免了在多个场合访问服务器。关于完成这项工作的任何其他建议也是一个优点

下面是操纵数据的javascript代码

    $(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);
                });
            });
    });
$(文档).ready(函数()
{
document.soapFormSetup.reset();
$(函数()
{
var start='';
变量选项='--------------------------'+'';
对于(变量i=0;i对于(var i=0;i我相信以下代码将生成一个相同的JSON编码字符串:

$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);

据推测,一个帐户名可能与多张卡关联(否则,为什么crd_no会是一个数组),因此请尝试以下方法:

// 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);

为什么不使用所有对象而不是混合对象和数组呢?这将更接近json语法,并且更具视觉吸引力和可读性。每个帐号都有一个或多个cc号,每个cc号都有一个或多个ss号?数据的关系总是正确的吗?我越想这个问题,它就越有吸引力足踝suggestion@chris...the正确的关系是,一个帐户可以有多个cc号,但每个cc号都有一个唯一的ssn号。这是针对联合帐户的情况,其中您有两个个人共享同一个银行帐户,他们每个人都有一个与该帐户绑定的卡号,但他们有单独的ssn号,这是冲突的ICT与示例数据一起显示ssn_no:是一个带有2个独立条目的数组。这很接近,但仍然没有显示确切的关系…您可以拥有一个帐户号,该帐户号具有相同的ard no,但不同的ssn no。谢谢,尽管这指向正确的方向。好的,您能告诉我db的哪些字段映射到哪个部分吗你的JSON?