【问题标题】:Displaying SQLite database with php if statement not working?如果语句不起作用,则使用 php 显示 SQLite 数据库?
【发布时间】:2015-03-18 16:49:25
【问题描述】:

我有一个可以显示的书籍 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>";

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

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['cat1'] = $category) 每次都为真,因为我数据库中的每本书仍然显示并且 else continue 不起作用。

这是我数据库中前两本书的示例

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 本书,用户可以添加更多。

我怎样才能让它只显示具有匹配类别的书籍?

【问题讨论】:

    标签: php database sqlite filter


    【解决方案1】:

    你应该改变你的if语句,问题是你现在只有一个=

    所以改变这一行:

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

    到这里:

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

    你也可以改变你的sql,这样你就只能通过这种方式获取基于类别的记录,你不需要foreach中的if语句。

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

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

    【讨论】:

      【解决方案2】:
      if($product['cat1'] == $category)
      

      您需要双等号而不是单等号。

      Comparison Operators

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-31
        • 2012-12-26
        • 2013-06-20
        • 2013-09-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多