0

The function product() is used on a page called products.php. How can I make each product returned from that search (nike trainers) a clickable link to a page called product.php and display only that products info onto the divs on product.php?

/*
 * 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>";
        } 
    }
}

Any help would be great, been struggling with this for a while now!

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
user2602038
  • 1
  • 1
  • 3

1 Answers1

0

Pass your search parameter in your function and customize it like below

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

Same if else blocks and a parameter to productlayout ($product) and use it in query


Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118