I have created a page that populates a sidebar with links(categories) using a database table. The table contains names(names of the categories) and their ids.
e.g
| **id** |**name**|
| 1 | Men |
| 2 | Women |
I need to do the following things:
- When a link is clicked, a corresponding h1 tag changes it's value to the name of the link(the category name).
-
A div tag, that displays product images below the h1 tag, also changes. I have created a table to hold product data. It contains the following data fields:
**id**(id for the product)-INT[PRIMARY KEY] **category_id**(category id)-INT[FOREIGN KEY] **name**(product name)-VARCHAR **image**(image file name e.g "blu_dress.png" ) e.g | **id** |**categoty_id**| **name** | **image** | | 1 | 2 |black blouse |bla_blouse.png| | 2 | 2 | blue dress | blu_dress.png| | 3 | 1 | brown shirt | bro_shirt.png| | 4 | 2 | blue blouse |blu_blouse.png|
PHP CODE TO POPULATE SIDEBAR
<?php
$dynamicList = "";
$sql = mysql_query("SELECT * FROM categories ORDER BY id ASC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0)
{
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$category_name = $row["name"];
$sql2 = mysql_query("SELECT * FROM categories WHERE id='".$id."'");
$dynamicList .= '
<li id="categegoryLink">
<a href="category.php?ID="'$category_name.'"" onclick="">
'.$category_name.'
</a>
</li>
';
}
}
else
{
$dynamicList = "We have no categories listed in our store yet";
}
mysql_close();
?>
AJAX CODE
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("title").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","category.php",true);
xmlhttp.send();
}
</script>
category.php
<?php echo $category_id?>
FURTHER INFO I can't post images since I don't have the reputation but I have an image that explains what I need further, if you need it.