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