如何搜索和显示userid = bookid的结果? bookids 是外键,而 userid 是两个不同表中的主键。这是通过MVC和使用PDO实现的。如何将参数从视图传递到控制器到模型以查询SELECT * FROM user,Book WHERE user_id=Book_id
查看:
if(!isset($_GET['id']))
{
exit;
}
$user_id=$_GET['id'];
?>
<div id="BookItem">
<div id = "thumbview_Book_title">
Books
</div>
<?php
$BookArray = $this->_dispResult['book'];
$num = count($BookArray);
for($i=0;$i<$num;$i++)
{
$row = $BookArray[$i];
}
<img src="<?php echo $row['booksrcpath'];?>">
<?php echo $row['bookName'];?>
控制器:
$bookId= isset($_GET["id"])?$_GET["id"]:'';
if($bookId)
{
$result['bookbyuserid']=$this->_userModel->getBookByUserId();
$BookRow = $this->_bookModel->getbookById($bookId);
if(!$bookRow)
{
gotoUrl('/error/?error=lost');
exit;
}
}
$result['book_row'] = $bookRow;
$result['book_array'] = $this->_bookModel->getAllBooks($bookId, $bookRow['username']);
$this->_showPage('books', 'books.php',$result);
}
来自两个表的模型从用户表和书籍表中查询选择bookid等于userid的所有书籍。它与SELECT *FROM USER, BOOK WHERE BOOK_ID = USER_ID
基本相同。
public function getBookByUserId()
{
$BookByUserIdArray = array();
$this->setTable('book,user');
$select = 'user.username,user.id as user_id, //$select = string to search db
book.id as book_id,book.book_name';
$where ="user.id=book.user_id"; //string to search where in db
$result= $this->query($select, $where); //query db
while($Result && $row =$this->nextRow() )
{
$user_name = $row['username'];
$book_id = $row['book_id'];
if($this->_BookImgSr->getBookUrl($user_name, $book_id)) // folder where image files are found and searched from
{
$row['user_photo'] = $this->_BookImgSr->getUserPhoto($user_name);
$row['Book_path'] = $this->_BookImgSr->getBookPath($user_name, $Book_id);
array_push( $BookByUserIdArray, $row);
}
}
return $BookByUserIdArray;
}
答案 0 :(得分:2)
如何将参数从视图传递到控制器到模型到查询
这是错误的道路 没有必要从视图中传递 控制器必须调用模型,获取数据,然后将其传递给视图。
所以,你走了:
首先,调用模型来获取数据
$bookId= isset($_GET["id"])?$_GET["id"]:'';
$BookRow = $this->_bookModel->getbookById($bookId);
最后,调用一个视图:
$this->_showPage('books', 'books.php',$result);