根据所选数据库条目显示数据库中的数据

时间:2014-10-29 13:18:41

标签: php mysql pdo

我正在使用数据库表中的条目填充下拉列表选择 现在根据这个选择,我想从数据库中显示其他信息 数据库表

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`)

我希望实现的示例
选择了event_title ='Event1'的数据库条目,现在我想显示与Event1关联的event_desc,event_location和event_requirements。
选择了event_title ='Event2'的数据库条目,现在我想显示与Event2相关的event_desc,event_location和event_requirements。等等......

在下拉列表选择中显示event_title的代码(在MVC架构中)正在运行 但是,我无法显示其他条目。

控制器

    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 * 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;
        $events[$event->event_id]->event_desc = $event->event_desc;
        $events[$event->event_id]->event_desc = $event->event_location;
        $events[$event->event_id]->event_desc = $event->event_requirements;
        $events[$event->event_id]->event_desc = $event->event_date;
    }
        return $events;
    }

查看

   <script>
        $(document).ready(function () {
            $('.group').hide();
            $('#option').show();
            $('#select-event-type').change(function () {
                $('.group').hide();
                $('#'+$(this).val()).show();
            })
        });
    </script>

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

    <div class="group">
        <h2><?php $this->event->event_title ?></h2>
        <p><?php $this->event->event_desc ?></p>
        <p><?php $this->event->event_location ?></p>
        <p><?php $this->event->event_reuqirements ?></p>
        <p><?php $this->event->event_date ?></p>
    </div> 

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

1 个答案:

答案 0 :(得分:1)

public function getEventTypes()
{
    $sth = $this->db->prepare("SELECT event_id, event_title FROM events");
//change the above query to ->"SELECT * FROM events"
//as you are only selecting 2 elements
    $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;
}

模型部分

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

    $events = array();

    foreach ($sth->fetchAll() as $key => $value) {
        $events[$key] = $value//new stdClass();
              }
    return $events;
}