-1

如何搜索和显示 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;
}
4

1 回答 1

2

如何将参数从视图传递到控制器到模型以进行查询

那是错误的道路。
没有什么可以视图中传递出去。
控制器必须调用模型,获取数据,然后将其传递给视图。

所以,你去吧:
首先,调用一个模型来获取数据

$bookId= isset($_GET["id"])?$_GET["id"]:'';
$BookRow = $this->_bookModel->getbookById($bookId);

最后,调用视图:

$this->_showPage('books', 'books.php',$result); 
于 2013-03-10T11:20:44.170 回答