如何搜索和显示 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);
}
模型
来自两个表的模型从 user 表和 book 表中查询,选择 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;
}