dousha1831 2015-03-18 16:49
浏览 34
已采纳

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

I have an sqlite database of books that i am able to display, however I want to be able to filter this display if the user clicks a spesific category. I have the following code to load the categorys

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>";

which correctly redirects the user to the spesific page. then, for the relevant page (in this example, category1) i have the following code

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>";

}

However, its like the if statement if($product['cat1'] = $category) is true everytime because every book in my database still displays and the else continue does not work.

here is an example of the first two books in my database

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')");

however there are 20 books in my database and users can add more.

How can i get this to only display books with matching category?

  • 写回答

2条回答 默认 最新

  • douluan1533 2015-03-18 16:57
    关注

    You should change your if statement, the problem is that you have now only one =

    so change this line:

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

    To this:

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

    Also you you could already changes you sql so you will only get the records based on the category by this way you don't need a if statement in the foreach.

    So your sql can be something like this:

    SELECT * FROM Books WHERE cat1='The PHP Programming Series'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c