根据先前的数据库选择显示数据库数据

时间:2014-11-03 11:00:52

标签: php mysql oop

我正在尝试从我的数据库中获取事件数据并在我的主页上显示 我的数据库表

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架构中):
控制器

function index()
    {
        $overview_model = $this->loadModel('Events');
        $this->view->events = $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_location = $event->event_location;
            $events[$event->event_id]->event_requirements = $event->event_requirements;
            $events[$event->event_id]->event_date = $event->event_date;
        }
        return $events;
    }

查看

        <!-------- WORKS --------------->
        <!-- start event type selection --> 
        <div class="event-selection">
           <label>Choose an event:</label>
           <select id="select-event-type">
              <?php foreach ($this->events as $event) {
                echo "<option value='" .$event->event_title. "'>" .$event->event_title."</option>";
            }?>
        </select>
        </div><!-- /.event-selection -->

        <!-------- DOES NOT WORK --------------->
        <!-- Start option 1 -->
        <div id="option1" class="group">
            <h2><?php $event->event_title ?></h2>
            <p><?php $event->event_desc ?></p>
            <p><?php $event->event_location ?></p>
            <p><?php $event->event_requirements ?></p>
            <p><?php $event->event_date ?></p>
        </div> <!-- /#option 1 -->              
    </div><!-- /.event-selection -->

我想要实现的目标,就是有一个下拉菜单,用户可以从中选择一个事件(该部分有效!)。然后在下拉菜单下方显示该事件的信息(该部分不起作用!) 我没有收到任何错误消息。我在浏览器中的结果是什么,在控制台中是这样的:

<!-- Start option 1 -->
<div id="option1" class="group">
     <h2></h2>
     <p></p>
     <p></p>
     <p></p>
     <p></p>
</div> <!-- /#option 1 -->

我对此非常陌生,我非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

更改

<?php $event->event_title ?>

<?php echo $event->event_title; ?>

您的结果不是echoing。您在PHP中使用了HTML,因此您必须回显结果。并在您的选择框中选择您需要检查的数据库结果像

<select id="select-event-type">
          <?php foreach ($this->events as $event) {

            if($event->event_title == $db_event_title) {
                 echo "<option value='" .$event->event_title. "' selected='selected'>" .$event->event_title."</option>";
            } else {
                 echo "<option value='" .$event->event_title. "'>" .$event->event_title."</option>";
            }
        }?>
</select>