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 chipyard环境搭建问题
  • ¥20 matlab可以把多个二维图合成为三维瀑布图吗
  • ¥15 EEPROM,软件i2c
  • ¥500 求解读该段JS代码,需要知道是用干什么的
  • ¥20 qt4代码实现二进制文件读取显示,以及显示的内容进行搜索
  • ¥15 Labview获取LK-G3001数据
  • ¥15 我知道什么是混合树,但是怎么写代码啊
  • ¥50 开发板linux系统安装dpkg,apt函数库 有偿
  • ¥15 浏览器时间循环 交互事件和延时事件的 优先级与执行问题
  • ¥15 GD模块安装出错,libgd无法正常安装