I have a category table and I have subcategories that are defined by parent_id which links to the main category id which is stored in cat_id
table category
cat_id PK int
parent_id int
category char
I want to add something to only show pets in cat X and in sub X
<?php
include 'config.php';
$domain = "
http://www.rksdesignstudios.com/clients/critterclassifieds/pets.php";
?>
<p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="action">
<label><b>Sort by:</b></label>
<select name="sort">
<option value="name">Name</option>
<option value="date">Date</option>
<option value="id">ID</option>
<option value="owner">Owner</optio
n>
<option value="category">Category<
/option>
</select>
then
<select name="ascdesc">
<option value="DESC">Descending</o
ption>
<option value="ASC">Ascending</opt
ion>
</select>
and show
<select name="max_results">
<option value="5">5</option>
<option value="10" selected="selected">10</op
tion>
<option value="20">20</option>
<option value="50">50</option>
</select>
<input name="action" type="hidden" value="go">
<input name="submit" type="submit" style="float:right;" value="Submit!">
</form>
<br />
<label for="pages"><b>Page:</b></
label>
</p>
<?php
if(!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}
$max_results = 10;
$from = (($page * $max_results) - $max_results);
$total_results = mysql_result(mysql_query("
SELECT COUNT(*) as Num FROM pet"),0);
if ($total_pages%$perpage)
{
// has remainder so add one page
$total_pages++;
}
$total_pages = intval($total_results / $max_results);
if ($total_pages%$perpage)
{
// has remainder so add one page
$total_pages++;
}
if($page > 1)
{
$prev = ($page - 1);
echo "<a href=\"$domain?page=$prev\
">« Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
echo "$i ";
}else{
echo "<a href=\"$domain?page=$i\">$
i</a> ";
}
}
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"$domain?page=$next\
">Next »</a>";
}
// alternating row colors
$color1 = "#f8f8f8";
$color2 = "#cccccc";
$row_count = 0;
if (isset($_POST['action']))
{
echo "<h1>SORTING ENABLED</h1>";
$sort = $_POST['sort'];
$ascdesc = $_POST['ascdesc'];
$max_results = $_POST['max_results'];
$category = $_POST['category'];
$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = c.category ORDER BY p." . $sort . " $ascdesc LIMIT $from, $max_results";
echo "<h2>" . $sql . "</h2>";
$result = mysql_query($sql);
}
else
{
$sql = "SELECT p.*, c.category FROM pet p, categories c WHERE p.category = c.category ORDER BY p.id DESC LIMIT $from, $max_results";
$result = mysql_query($sql) or die("Bad Query: " . mysql_error());
}
$num_rows = mysql_num_rows($result);
echo "<br /><b>Results returned:</b> $num_rows\n";
echo "</div><table width=\"100%\" cellpadding=\"2\" cellspacing=\"0\">";
if ($num_rows > 0)
{
while ($row = mysql_fetch_array($result)
)
{
$row_color = ($row_count % 2) ? $color1 : $color2;
echo '<tr bgcolor="'. $row_color .'">
<td>
<h1><a href="pet-detial.php?id='.
$row['id'] .'" target="_blank">'. $row['title'] .'</a></h1><br />
<h2><b>Pet Name:</b> '.$row['name'].'</h2><br />
<h2><b>Posted By:</b> <a href="profile.php?id='. $row['id'] .'" target="_blank">'.$row['us
ername'].'
</a></h2><
br />
<p>'.$row['description'].'
</p>
<b>Added on:</b> '. date("F j, Y, g:i a", strtotime($row[date])) .' · <b>Category:</b> '.$row['category'].' ·
</td>
</tr>'."\n";
$row_count++;
}
}
else
{
echo "No resources in this category.";
}
echo "</table>";
?>
</body>
</html>
That is my code for pets.php which displays them.
Would I do something like?
<label>Select Parent Category:</label>
<select name="parent">
<?php
$query = "SELECT cat_id, category FROM categories WHERE parent_id=0";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_object($result
))
{
?><option value="<?php echo $row->cat_id; ?>"><?php echo $row->category; ?></option>
<?php
}
?>
</select><br />
Which will displays all of my main categories which is stored in cat_id as the ID # and category char as text.
Then do the same for maybe? subcategories?
Any suggestions, thanks.