如果我仅在第一次循环访问foreach循环时进入if并获取变量值,那么每次下载时它仍然具有旧值。为什么呢?
如果我print_r($i)
并杀了它就会响应:
A0A0A0A0A0A0A0A0A0A0A0A0A0A0TXTTXT16
实际金额是它的结尾(16)。这意味着它循环循环16次,但只进入if $type
更改的第一个循环。因此$type
每次循环时都不会获得新值。
我的代码:
$recordTypes = array('CnameRecord', 'ARecord', 'MxRecord', 'TxtRecord');
foreach ($recordTypes as $type) {
$modelName = $type::model()->tableName();
$record = $type::model()->findAllbySql("SELECT * FROM $modelName WHERE vhost_id LIKE $id");
if ($record) {
foreach ($record as $key=>$rec) {
if ($type == 'ARecord'){
$type = 'A' + $i;
$hostname = $rec['sub_vhost_name'];
$points_to = $rec['points_to'];
$ttl = $rec['ttl'];
} else if ($type == 'CnameRecord') {
$type = 'CNAME';
$hostname = $rec['hostname'];
$points_to = $rec['points_to'];
$ttl = $rec['ttl'];
} else if ($type == 'MxRecord') {
$type = 'MX';
$priority = $rec['priority'] . ' ';
$points_to = $rec['points_to'];
$ttl = $rec['ttl'];
$hostname = $rec['host'];
} else if ($type == 'TxtRecord') {
$type = 'TXT';
$hostname = $rec['hostname'];
$points_to = '"' . $rec['txt_value'] . '" ';
$ttl = $rec['ttl'];
}
$i++;
print_r($type);
$custom_content = $hostname . ' ' . $ttl . ' ' . 'IN' . ' ' . $type . ' ' . $priority . $points_to . "\n";
}
}
}
print_r($i);die;
答案 0 :(得分:1)
你正在使用:
foreach ($recordTypes as $type) { ...
在foreach中你正在修改“type”var,所以在通过$ record的下一个循环中,你将失败任何if语句。 将$ type var(if语句内部)更改为另一个var名称,然后重试。