Jump to content

PHP and MySQL: This should be so easy, but... I don't know how


susanv

Recommended Posts

I'm just learning php and I've created a test shopping website. Everything is working well, but I need help with the following:

 

This is my products table in the database:

 

"CREATE TABLE products (

id int(11) NOT NULL auto_increment,

product_name varchar(255) NOT NULL,

price varchar(16) NOT NULL,

details text NOT NULL,

category varchar(16) NOT NULL,

subcategory varchar(16) NOT NULL,

date_added date NOT NULL,

PRIMARY KEY (id),

UNIQUE KEY product_name (product_name)

) ";

 

I have 3 subcategories: Hats, Pants and Shirts.

 

On my index page I have links to these three items.

 

This is my products.php code.  When run it shows all the items in the 3 subcategories.  How do I change the code so that this page display only the Hats when someone clicks on the Hats link?

 

<?php

// Connect to the MySQL database 

include "storescripts/connect_to_mysql.php";

$dynamicList = "";

$sql = mysql_query("SELECT * FROM products ORDER BY subcategory DESC");

$productCount = mysql_num_rows($sql); // count the output amount

if ($productCount > 0) {

while($row = mysql_fetch_array($sql)){

            $id = $row["id"];

$product_name = $row["product_name"];

$price = $row["price"];

$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">

        <tr>

          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>

          <td width="83%" valign="top">' . $product_name . '<br />

            $' . $price . '<br />

            <a href="product.php?id=' . $id . '">View Product Details</a></td>

        </tr>

      </table>';

    }

} else {

$dynamicList = "We have no products listed in our store yet";

}

mysql_close();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Store Home Page</title>

<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />

</head>

<body>

<div align="center" id="mainWrapper">

  <?php include_once("template_header.php");?>

  <div id="pageContent">

  <table width="100%" border="0" cellspacing="0" cellpadding="10">

  <tr>

    <td width="32%" valign="top"><h3>xxx</h3>

      <p>xxx</p></td>

    <td width="35%" valign="top"><h3>Latest Designer Fashions</h3>

      <p><?php echo $dynamicList; ?><br />

        </p>

      <p><br />

      </p></td>

    <td width="33%" valign="top"><h3>xxx</h3>

      <p>xxx</p></td>

  </tr>

</table>

 

  </div>

  <?php include_once("template_footer.php");?>

</div>

</body>

</html>

 

If someone can help me I'd be so grateful!  :shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.