我正在處理與this one類似的事情。但我試圖根據是否有人加入該組來分配按鈕「加入」或「輸入」。問題是我不知道如何將變量從類別ID($cats_id
)傳遞到視圖文件。如何將視圖中的變量傳遞給mvc(PHP)中的模型
我在模型中創建了一個函數來檢查行是否存在並返回true。
// check if joined the group
public static function checkIfJoined($cats_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$users_id = Session::get('user_id');
$sql = "SELECT cats_id,users_id FROM categories_joined WHERE users_id = :users_id AND cats_id = :cats_id";
$query = $database->prepare($sql);
$query->execute(array(':users_id' => $users_id, ':cats_id' => $cats_id));
// fetchAll() is the PDO method that gets all result rows
if ($query->rowCount() >= 1 || Session::get('user_account_type') == 7) {
return true;
}
}
然後在控制器中,我將模型渲染到視圖中。
public function index()
{
$cats_id = ""; // this doesn't work right obviously
$this->View->render('dashboard/index', array(
'categories' => DashboardModel::getAllCategories(),
'joined' => DashboardModel:: checkIfJoined($cats_id)
));
}
在視圖中,我將預覽函數'categories'中的變量傳遞給視圖。
<?php if ($this->categories) { ?>
<?php foreach($this->categories as $key => $value) { ?>
...
<?php $cats_id = $value->cat_id; if ($this->joined == true): ?>Enter
<?php else: ?>Join
<?php endif; ?>
您是否試圖將視圖中的數據傳遞給控制器? – BugHunterUK