用php显示SQLite数据库如果语句不起作用?

时间:2015-03-18 16:49:26

标签: php database sqlite filter

我有一个我能够显示的sqlite书籍数据库,但是如果用户点击特定类别,我希望能够过滤此显示。我有以下代码来加载类别

echo "<div> <a href='index.php?category1=1'> The PHP Programming Series </a> <a href='index.php?category2=1'> TCP-IP for Beginners Series </a> <a href='index.php?category3=1'> Client Side Programming Series </a> <a href='index.php?category4=1'> The Web Design Series – an HCI focus </a> </div>";

正确地将用户重定向到特定页面。然后,对于相关页面(在此示例中,类别1),我有以下代码

else if(isset($_GET['category1'])) {
//The PHP Programming Series
$category = 'The PHP Programming Series';

echo "<p>
        <a href='./index.php'>Bookshop</a></p>";

    echo "<h3>Search Results</h3>";

    echo "<table style='width:500px;' cellspacing='0'>";
    echo "
    <tr>
        <th style='border-bottom:1px solid #000000;'></th>
        <th style='border-bottom:1px solid #000000;'>Title</th>
        <th style='border-bottom:1px solid #000000;'>Brief Description</th>
        <th style='border-bottom:1px solid #000000;'>Author</th>
        <th style='border-bottom:1px solid #000000;'>Price</th>
    </tr>";

    if(sqlite_num_rows($result) !=0) {
    foreach($products as $id => $product) {
        if($product['cat1'] = $category) {
        echo "<tr>
            <td style='border-bottom:1px solid #000000;'><a href='./index.php?view_product=$id'><img src='/img/" . $product['imgNumber'] . ".jpg' /></a></td>
            <td style='border-bottom:1px solid #000000;'><a href='./index.php?view_product=$id'>" . $product['Title'] . "</a></td> 
            <td style='border-bottom:1px solid #000000;'>" . $product['Brief_Synopsis'] . "</td>
            <td style='border-bottom:1px solid #000000;'>" . $product['Author'] . "</a></td>
            <td style='border-bottom:1px solid #000000;'>&#163;" . $product['price'] . "</td> 
            </tr>";
            } else {
            continue;}
    } } else {
    echo "<p> No Results </p>";}
    echo "</table>";

}

然而,它就像if语句if($ product [&#39; cat1&#39;] = $ category)每次都是真的,因为我的数据库中的每本书仍然显示而else继续不起作用。

这是我数据库中前两本书的一个例子

sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber, price, cat1, cat2) VALUES ( 'Mike McGrath', 'C Programming In Easy Steps 4th Edition','C Programming in easy steps has an easy-to-follow style that will appeal to anyone who wants to begin programming in C', 1840785446, 'In Easy Steps Limited', '001', 24.99, 'Client Side Programming Series', 'Programming')");
sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber, price, cat1, cat2) VALUES ( 'Robin Nixon', 'Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5','Build interactive data-driven websites with the potent combination of open-source technologies and web standards', 9781491918661, 'O&#39Reilly', '002', 40.99, 'The PHP Programming Series','Programming')");

但是我的数据库中有20本书,用户可以添加更多。

如何才能显示具有匹配类别的图书?

2 个答案:

答案 0 :(得分:1)

if($product['cat1'] == $category)

你需要一个双等于而不是一个。

Comparison Operators

答案 1 :(得分:1)

您应该更改if语句,问题是您现在只有一个=

所以改变这一行:

if($product['cat1'] = $category) {

对此:

if($product['cat1'] == $category) {

另外,您可以更改自己的sql,这样您只能通过这种方式获取基于类别的记录,而不需要在foreach中使用if语句。

所以你的sql可以是这样的:

SELECT * FROM Books WHERE cat1='The PHP Programming Series'