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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀