[英]set select in option php: Undefined property
Can someone explain me why I can't list items in select control? 有人可以解释一下我为什么不能在选择控件中列出项目吗? If I disable public $team_id in my Team class, and make new object
$selectedHome_id
, and after that correct this line of code to $selectedHome_id == $team->team_id
I can list data, but I can't select the item. 如果我在Team类中禁用了公共$ team_id,并创建了新对象
$selectedHome_id
,然后将这一行代码更正为$selectedHome_id == $team->team_id
我可以列出数据,但不能选择该项目。
matches.php matches.php
<?php include("config.php");
$selectedTeam = new Team();
if (isset($_GET['htid'])) {
$selectedHome_id = Team::getById($_GET['htid']);
}
?>
<select class="custom-select-sm" id="inputGroupSelect01" aria-label="Small" aria-describedby="inputGroup-sizing-sm" onchange="window.location='?htid='+this.value" name="selHomeTeam">
<option value="-1">Choose...</option>
<?php
$selectedHome_id = new Team();
$teams = Team::getAll();
foreach ($teams as $team) {
echo "<option " .($selectedHome_id->team_id== $team->team_id?"selected":""). " value='{$team->team_id}'>{$team->name}</option>";
}
?>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
Team.php Team.php
class Team extends ActiveRecord {
public $team_id;
public $name;
public static $table = "teams";
public static $key = "team_id";
}
So you check to see if you have been passed a $_GET['htid']
and, if you have, then you get that Team
. 因此,您检查是否已通过
$_GET['htid']
传递,如果已经传递,则得到该Team
。 Then, within your options, you promptly replace this with a new Team
-- what do you think the team_id
would be set to, with this new Team
? 然后,你的选择中,你及时用新的替换这个
Team
-你觉得什么team_id
将被设置为,与这个新的Team
?
Perhaps, rather than 也许,而不是
if (isset($_GET['htid'])) {
$selectedHome_id = Team::getById($_GET['htid']);
}
you may want to 你可能想
if (isset($_GET['htid'])) {
$selectedHome_id = Team::getById($_GET['htid']);
} else {
$selectedHome_id = new Team();
}
By doing this, when you get to the ternary comparison in your option, $selectedHome_id
is set to something. 这样,当您进入选项的三元比较时,会将
$selectedHome_id
设置为某值。
Without seeing the full code, though, we lack both intent and context. 但是,在没有看到完整代码的情况下,我们缺乏意图和上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.