【问题标题】:How do i use a query to display products from a query onto a product layout page如何使用查询将查询中的产品显示到产品布局页面上
【发布时间】:2013-07-24 18:57:18
【问题描述】:

函数product() 用于名为products.php 的页面。如何使从该搜索返回的每个产品(耐克运动鞋)成为指向名为product.php 的页面的可点击链接,并仅在product.php 上的divs 上显示该产品信息?

/*
 * This function is used for products.php page,(search page)
 */
function product ()
{
    $get = mysql_query ("SELECT id, name, price, size, imgcover FROM products WHERE brand='nike'");
    if (mysql_num_rows($get) == FALSE)
        { echo " There are no products to be displayed!";}
    else
        { while ($get_row = mysql_fetch_assoc($get)){echo "<div id='productbox'><a href='product.php'>".$get_row['name'].'<br /> £'.$get_row['price']. '  UK'.$get_row['size']. '<br />' .$get_row['imgcover']. "</a></div>" ;} 
    }
}

/* The function productlayout () I am using for the product.php page and I am echoing this onto this page.  */

function productlayout ()
{
    $get = mysql_query ("SELECT id, name, price, size, buyfor, exfor, grade, brand, Basecolour,     imgcover FROM products WHERE id ='1'");
    if (mysql_num_rows($get) == FALSE)
        { echo " There are no products to be displayed!";}
    else
        { while ($get_row = mysql_fetch_assoc($get))
            { echo "<div id='layouttitle'>".$get_row['name']."</div>
                <br /> <div id='layoutprice'>£".$get_row['price']. "</div>
                <div id='layoutsize'>UK".$get_row['size']. "</div>
                <br /><div id='layoutgrade'>Condition  " .$get_row['grade']. "</div>
                <div id='layoutbrand'>" .$get_row['brand']. "</div>
                <div id='layoutbcolour'>Base Colour - " .$get_row['Basecolour']. "</div>
                <div id='givecash'>" .$get_row['buyfor']. "</div>
                <div id='giveexchange'>" .$get_row['exfor']. "</div>";
        } 
    }
}

任何帮助都会很棒,现在已经为此苦苦挣扎了一段时间!

【问题讨论】:

    标签: php html arrays variables product


    【解决方案1】:

    在你的函数中传递你的搜索参数并像下面这样自定义它

      function product ($product =NULL)
     {
    
     if(!empty($product)){
          $get = mysql_query ("SELECT id, name, price, size, imgcover FROM products
     WHERE brand='nike' AND `name` LIKE '%".$product."%'");
     }else{
     $get = mysql_query ("SELECT id, name, price, size, imgcover FROM products 
     WHERE brand='nike'");
     }
     if (mysql_num_rows($get) == FALSE)
     { echo " There are no products to be displayed!";}
     else
     {while ($get_row = mysql_fetch_assoc($get)){echo "<div id='productbox'><a href='product.php'>".$get_row['name'].'<br /> £'.$get_row['price']. '  UK'.$get_row['size']. '<br />' .$get_row['imgcover']. "</a></div>" ;} 
     }
     }
    

    相同的if else 块和productlayout ($product) 的参数并在查询中使用它


    Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多