下面的PHP / AJAX / JS中的foreach
是多次显示一组结果,而不是每次显示两次结果。我必须使用foreach
来实现我正在寻找的最终结果,但这并不是显示所有信息。
$entries = [];
$entries['id'] = get_the_id();
$entries['fname'] = get_field('fname');
$entries['lname'] = get_field('lname');
$entries['custid'] = get_field('customer_id');
$entries['company'] = get_field('company');
$entries['addr1'] = get_field('address_line_1');
$entries['addr2'] = get_field('address_line_2');
$entries['city'] = get_field('city');
$entries['state'] = get_field('state');
$entries['zip'] = get_field('zip');
<script>
function populateFields() {
alert('Gathering representatives...');
$.ajax({
type: "POST",
url: "form-shipping.php",
data: <?php foreach($entries as $entry){?>
{
"id": <?php echo $entry['id'];?>,
"fname": <?php echo $entry['fname'];?>,
"lname": <?php echo $entry['lname'];?>,
"custid": <?php echo $entry['custid'];?>,
"company": <?php echo $entry['company'];?>,
"addr1": <?php echo $entry['addr1'];?>,
"addr2": <?php echo $entry['addr2'];?>,
"city": <?php echo $entry['city'];?>,
"state": <?php echo $entry['state'];?>,
"zip": <?php echo $entry['zip'];?>,
} ;
<?php } ?>
dataType: 'text';
success function (){
}
});
//if option value is equal to $ID, echo fields that match $ID
...
我的结果是
<script>
function populateFields() {
alert('Gathering representatives...')
$.ajax({
type: "POST",
url: "form-shipping.php",
data: {
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
{
"id": 136,
"fname": John,
"lname": Franklin,
"custid": 00100120141,
"company": Bonetta Inc.,
"addr1": 123 Bonetta Ln,
"addr2": ,
"city": Indianopolis,
"state": Indiana,
"zip": 1234,
} ;
dataType: 'text';
success function (){
}
});
//if option value is equal to $ID, echo fields that match $ID
...
</script>
`
答案 0 :(得分:0)
如果您的数组示例准确,则您只创建了一维数组。 foreach
期望迭代多维数组。
例如:
$entries[0]['id'] = get_the_id();
$entries[0]['fname'] = get_field('fname');
$entries[1]['id'] = get_the_id();
$entries[1]['fname'] = get_field('fname');
$entries[2]['id'] = get_the_id();
$entries[2]['fname'] = get_field('fname');
等
和/或,如果你正在构建一个多维数组(但没有告诉我们你是为了简洁),那么如果你用$entries = [];
开始每个构建,你就可以有效地重置数组回空。