gpt4 book ai didi

php - 从数据库填充下拉列表

转载 作者:行者123 更新时间:2023-11-29 07:50:18 25 4
gpt4 key购买 nike

我正在尝试使用数据库表中的条目填充下拉选择。
数据库表:

TABLE `events` (
`event_id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
`event_title` VARCHAR(255) NOT NULL,
`event_desc` TEXT,
`event_location` VARCHAR(255) NOT NULL,
`event_requirements` TEXT DEFAULT NULL,
`event_date` DATETIME NOT NULL,
PRIMARY KEY (`event_id`)

代码(MVC架构):
Controller

function index()
{
$overview_model = $this->loadModel('Events');
$this->view->dog_types = $overview_model->getEventTypes();
$this->view->render('events/index');
}

型号

public function getEventTypes()
{
$sth = $this->db->prepare("SELECT event_id, event_title FROM events");
$sth->execute();

$events = array();

foreach ($sth->fetchAll() as $event) {
$events[$event->event_id] = new stdClass();
$events[$event->event_id]->event_title = $event->event_title;
}
return $events;
}

查看

    <select>
<?php foreach ($this->events as $event) {
echo "<option value='" .$event->event_title. "'>" .$event->event_title."</option>";
}?>
</select>

错误我收到:

Notice: Undefined property: View::$events

Warning: Invalid argument supplied for foreach()

我对这一切真的很陌生,并且非常乐意提供任何帮助!谢谢!!

最佳答案

这个错误非常明显。您还没有名为 $events 的属性。您填充了一个名为

的属性
$this->view->dog_types

在您看来,您将能够像这样访问该对象:

  <select>
<?php foreach ($this->dog_types as $event) { //or $events as $event depending what you indexed as
echo "<option value='" .$event->event_title. "'>" .$event->event_title."</option>";
}?>
</select>

关于php - 从数据库填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26629478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com