【发布时间】:2020-06-07 13:32:46
【问题描述】:
所以,我正在混合 html/php 项目中创建一个表单,我想要一个选择标签,作为选项提供所有国家/地区列表。 我将列表存储在数组“国家”下,如下所示:
$countries = array("Afghanistan", "Albania", "Algeria", ...."Zambia", "Zimbabwe");
并尝试在我的选择标签中循环,以便每个国家/地区都会弹出一个选项标签。
<div class="form-group">
<label for="countries" class="col-md-3 control-label">Countries</label>
<div class="col-md-6">
<select class="form-control" id="countries" name="countries">
<?php foreach ($countries as $country) { ?>
<option value="<?= $countries->country ?>"><?= $countries->country ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-3 error">
<?php error('countries'); ?>
</div>
</div>
但我得到的是“试图获取非对象的属性”
因此,因为我在 php 方面没有进步,所以如果有人愿意帮助我,我会被正在发生的事情所困扰。谢谢
【问题讨论】:
-
$countries->country表示一个对象。但是您将$countries定义为一个数组。因此,您应该使用<option value="<?= $country ?>"><?= $country ?></option>...$countries->country将是一个不存在的方法country()。您将$countries定义为一个字符串数组,按外观。因此试图获取非对象的属性。