Jump to content

Recommended Posts

At the moment i have a script where i can add products to a site and put them into a catagory.

 

i am trying to thing of the best way to add sub catagories into the equation.

 

these are my tables;

 

catagory

id

view

name

 

products

id

code

catagory

make

name

price

description

sold

colour

cotw

size

age

safe

flow

 

when i add a product i use a drop down list to selct its catagory, same when i view on main site, i just list the catagories on the left and select, then it views all products with the "catagory name" on the right.

 

how should i approach this?

 

i am no expert on .php so any details explanations, code snippets will be greatly appreciated.  ;D

Link to comment
https://forums.phpfreaks.com/topic/107078-catagories-and-sub-cats/
Share on other sites

sorry if i was vague,

 

basically i cant get my head round how to do it?

 

i can do drop downs etc my self, was trying to explain how i am currently adding products to a catagory.

 

This is my current code for adding a catagory to a site; (apologise for the amount of code, its a up and running site of mine!)

 

<title>Cpanel - Add A Catagory</title>
<? include("includes/header.php") ?>
<table width="100%" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="FFFFFF" bordercolor="#999999">
  <tr>
    <td><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" background="images/subbar_bg.gif">
      <tr background="images/subbar_bg.gif">
        <td width="4" height="52"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font size="1"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#3399FF"><img src="images/subbar_left.gif" width="4" height="52" /></font></b></font></b></font></td>
        <td height="52" align="left" valign="middle" background="images/subbar_bg.gif"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="20%"><img src="images/icons/add_catagory.gif" /></td>
              <td width="80%"><div align="right"><a href="help.php" target="_self"><img src="images/icons/help.gif" alt="Help" width="38" height="38" border="0" /></a></div></td>
            </tr>
        </table></td>
        <td width="4" height="52"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font size="1"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#3399FF"><img src="images/subbar_right.gif" alt="" /></font></b></font></b></font></td>
      </tr>
    </table>
      <br />
      <table width="100%" border="0" cellspacing="1" cellpadding="4">
        <tr>
          <td width="20%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_left_corner.gif" width="4" height="4" /></td>
              <td height="4" background="images/top_menu.gif" bgcolor="#f2f3f5"><img src="images/top_menu.gif" width="1" height="4" /></td>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_right_corner.gif" width="4" height="4" /></td>
            </tr>
            <tr>
              <td width="4" background="images/left_menu.gif" bgcolor="#f2f3f5"><img src="images/left_menu.gif" width="4" height="1" /></td>
              <td bgcolor="#f2f3f5"><? include("includes/menu.php") ?></td>
              <td width="4" background="images/right_menu.gif" bgcolor="#f2f3f5"><img src="images/right_menu.gif" width="4" height="1" /></td>
            </tr>
            <tr>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_left_corner.gif" width="4" height="4" /></td>
              <td height="4" background="images/bottom_menu.gif" bgcolor="#f2f3f5"><img src="images/bottom_menu.gif" width="1" height="4" /></td>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_right_corner.gif" width="4" height="4" /></td>
            </tr>
          </table></td>
          <td valign="top"><table width="75%" border="0" align="left" cellpadding="0" cellspacing="0">
            <tr>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_left_corner.gif" width="4" height="4" /></td>
              <td height="4" background="images/top_menu.gif" bgcolor="#f2f3f5"><img src="images/top_menu.gif" width="1" height="4" /></td>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_right_corner.gif" width="4" height="4" /></td>
            </tr>
            <tr>
              <td width="4" background="images/left_menu.gif" bgcolor="#f2f3f5"><img src="images/left_menu.gif" width="4" height="1" /></td>
              <td bgcolor="#f2f3f5"><table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
                  <tr>
                    <td><form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" method="post">
                        <table width="100%" border="0" cellspacing="1" cellpadding="3">
                          <tr>
                            <td>NAME:</td>
                            <td><font style="text">
                              <input type="text" name="name" value="" />
                            </font> </td>
                            <td>Show:</td>
                            <td><font style="text">
                              <select name="view">
                                <?  
// Query to pull information from the "view" Database  
$result = mysql_query("select * from $table8 order by id ASC");  
while ($row = mysql_fetch_object($result)){  
?>
                                <option value="<?php echo $row->view; ?>"> <?php echo $row->view; ?> </option>
                                <?}?>
                              </select>
                            </font> </td>
                          </tr>
                        </table>
                      <div align="center"><br />
                            <input type="submit" name="submit" value="SUBMIT" />
                        </div>
                    </form>
                        <?php 

if (isset($_POST['submit'])) {
$name = $_POST['name'];

$insert = "INSERT INTO catagory SET 
ID='$id',
view='$view',
name='$name'"; 

if (@mysql_query($insert)) { 
echo('A new Catagory has been added.<br><a href="catagory.php">Click here to return to Catagory page.</a>'); } 
else { 
echo('Error in submission:' . mysql_error()); 
}} 

?>
                    </td>
                  </tr>
              </table></td>
              <td width="4" background="images/right_menu.gif" bgcolor="#f2f3f5"><img src="images/right_menu.gif" width="4" height="1" /></td>
            </tr>
            <tr>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_left_corner.gif" width="4" height="4" /></td>
              <td height="4" background="images/bottom_menu.gif" bgcolor="#f2f3f5"><img src="images/bottom_menu.gif" width="1" height="4" /></td>
              <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_right_corner.gif" width="4" height="4" /></td>
            </tr>
          </table></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<? include("includes/footer.php") ?>

 

and this is code for add product (again lenghty, sorry :();

 

<title>Add A Product</title>
<script src="nicEdit.js" type="text/javascript"></script>
<script type="text/javascript">
bkLib.onDomLoaded(nicEditors.allTextAreas);
</script>
<? include("includes/header.php") ?>
<style type="text/css">
<!--
.style1 {
color: #94C00E;
font-weight: bold;
}
.style4 {
font-size: 9px;
color: #94C00E;
font-weight: bold;
}
-->
</style>
<table width="100%" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="FFFFFF" bordercolor="#999999">
  <tr>
    <td>
      <?php
if(isset($_POST['submit']))
{

$name = $_POST['name'];
$make = $_POST['make'];
$catagory = $_POST['catagory'];
$price = $_POST['price'];
$description = $_POST['description'];
$sold = $_POST['sold'];
$colour = $_POST['colour'];
$size = $_POST['size'];
$age = $_POST['age'];
$safe = $_POST['safe'];
$flow = $_POST['flow'];

// Query for insert
$sql = "INSERT INTO $table1 (name, make, catagory, price, description, sold, colour, size, age, safe, flow) VALUES ('$name', '$make', '$catagory', '$price', '$description', '$sold', '$colour', '$size', '$age', '$safe', '$flow')";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());

// new code by hxbro
// sort out the uploaded images

$upload_dir = "../images/products/";
$insert_id = mysql_insert_id() or die("Unable to get insert id for image name.<br>" . mysql_error());
if (!empty($imageOne))
{
// we have an image in image one check we have a valid type (jpeg only ?)
if ($_FILES['imageOne']['type'] != "image/jpeg" && $_FILES['imageOne']['type'] != "image/pjpeg")
{
	die("Image type is not allowed for Image One. Type found : " . $_FILES['imageOne']['type']);
}
else
{
	// valid image make up a new filename
	$new_image_name = $insert_id . "one.jpg";

	if (!copy($_FILES['imageOne']['tmp_name'], $upload_dir . $new_image_name))
	{
		// file uploaded add to table row
		die("Unable to upload Image One");
	}
}
}
if (!empty($imageTwo))
{
// we have an image in image one check we have a valid type (jpeg only ?)
if ($_FILES['imageTwo']['type'] != "image/jpeg" && $_FILES['imageTwo']['type'] != "image/pjpeg")
{
	die("Image type is not allowed for Image Two. Type found : " . $_FILES['imageTwo']['type']);
}
else
{
	// valid image make up a new filename
	$new_image_name = $insert_id . "two.jpg";

	if (!copy($_FILES['imageTwo']['tmp_name'], $upload_dir . $new_image_name))
	{
		die("Unable to upload Image Two");
	}
}
}

if (!empty($imageThree))
{
// we have an image in image one check we have a valid type (jpeg only ?)
if ($_FILES['imageThree']['type'] != "image/jpeg" && $_FILES['imageThree']['type'] != "image/pjpeg")
{
	die("Image type is not allowed for Image Three. Type found : " . $_FILES['imageThree']['type']);
}
else
{
	// valid image make up a new filename
	$new_image_name = $insert_id . "three.jpg";

	if (!copy($_FILES['imageThree']['tmp_name'], $upload_dir . $new_image_name))
	{
		die("Unable to upload Image Three");
	}
}
}

if (!empty($imageFour))
{
// we have an image in image one check we have a valid type (jpeg only ?)
if ($_FILES['imageFour']['type'] != "image/jpeg" && $_FILES['imageFour']['type'] != "image/pjpeg")
{
	die("Image type is not allowed for Image Four. Type found : " . $_FILES['imageFour']['type']);
}
else
{
	// valid image make up a new filename
	$new_image_name = $insert_id . "four.jpg";

	if (!copy($_FILES['imageFour']['tmp_name'], $upload_dir . $new_image_name))
	{
		die("Unable to upload Image Four");
	}
}
}
if (!empty($imageFive))
{
// we have an image in image one check we have a valid type (jpeg only ?)
if ($_FILES['imageFour']['type'] != "image/jpeg" && $_FILES['imageFive']['type'] != "image/pjpeg")
{
	die("Image type is not allowed for Image Five. Type found : " . $_FILES['imageFive']['type']);
}
else
{
	// valid image make up a new filename
	$new_image_name = $insert_id . "five.jpg";

	if (!copy($_FILES['imageFive']['tmp_name'], $upload_dir . $new_image_name))
	{
		die("Unable to upload Image Five");
	}
}
}
if (!empty($imageSix))
{
// we have an image in image one check we have a valid type (jpeg only ?)
if ($_FILES['imageSix']['type'] != "image/jpeg" && $_FILES['imageSix']['type'] != "image/pjpeg")
{
	die("Image type is not allowed for Image Six. Type found : " . $_FILES['imageSix']['type']);
}
else
{
	// valid image make up a new filename
	$new_image_name = $insert_id . "six.jpg";

	if (!copy($_FILES['imageSix']['tmp_name'], $upload_dir . $new_image_name))
	{
		die("Unable to upload Image Six");
	}
}
}
if (!empty($imageThumb))
{
// we have an image in image one check we have a valid type (jpeg only ?)
if ($_FILES['imageThumb']['type'] != "image/jpeg" && $_FILES['imageThumb']['type'] != "image/pjpeg")
{
	die("Image type is not allowed for Image Thumb. Type found : " . $_FILES['imageThumb']['type']);
}
else
{
	// valid image make up a new filename
	$new_image_name = $insert_id . "thumb.jpg";

	if (!copy($_FILES['imageThumb']['tmp_name'], $upload_dir . $new_image_name))
	{
		die("Unable to upload Image Thumb");
	}
}
}

// end of new code by hxbro
} //ADD
?>
<form enctype="multipart/form-data" action="<? echo $PHP_SELF ?>" method="post">
      <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr class=table3>
      <td><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" background="images/subbar_bg.gif">
        <tr background="images/subbar_bg.gif">
          <td width="4" height="52"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font size="1"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#3399FF"><img src="images/subbar_left.gif" width="4" height="52" /></font></b></font></b></font></td>
          <td height="52" align="left" valign="middle" background="images/subbar_bg.gif">
            <table width="100%"  border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="20%"><img src="images/icons/add_product_logo.gif" width="173" height="39" /></td>
                <td width="80%"><div align="right"><a href="add.php" target="_self"><img src="images/icons/add.gif" alt="Add a product" width="38" height="38" border="0" /></a><a href="help.php" target="_self"><img src="images/icons/help.gif" width="38" height="38" border="0" /></a></div></td>
              </tr>
          </table></td>
          <td width="4" height="52"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font size="1"><b><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#3399FF"><img src="images/subbar_right.gif" alt="" /></font></b></font></b></font></td>
        </tr>
      </table>        
        <br>
        <table width="98%"  border="0" align="center" cellpadding="3" cellspacing="1">
          <tr align="left" valign="top">
            <td width="20%" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_left_corner.gif" width="4" height="4" /></td>
                <td height="4" background="images/top_menu.gif" bgcolor="#f2f3f5"><img src="images/top_menu.gif" width="1" height="4" /></td>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_right_corner.gif" width="4" height="4" /></td>
              </tr>
              <tr>
                <td width="4" background="images/left_menu.gif" bgcolor="#f2f3f5"><img src="images/left_menu.gif" width="4" height="1" /></td>
                <td bgcolor="#f2f3f5"><? include("includes/menu.php") ?></td>
                <td width="4" background="images/right_menu.gif" bgcolor="#f2f3f5"><img src="images/right_menu.gif" width="4" height="1" /></td>
              </tr>
              <tr>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_left_corner.gif" width="4" height="4" /></td>
                <td height="4" background="images/bottom_menu.gif" bgcolor="#f2f3f5"><img src="images/bottom_menu.gif" width="1" height="4" /></td>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_right_corner.gif" width="4" height="4" /></td>
              </tr>
            </table></td>
            <td width="80%"><table width="75%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_left_corner.gif" width="4" height="4" /></td>
                <td height="4" background="images/top_menu.gif" bgcolor="#f2f3f5"><img src="images/top_menu.gif" width="1" height="4" /></td>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/top_right_corner.gif" width="4" height="4" /></td>
              </tr>
              <tr>
                <td width="4" background="images/left_menu.gif" bgcolor="#f2f3f5"><img src="images/left_menu.gif" width="4" height="1" /></td>
                <td bgcolor="#f2f3f5"><table width="100%" border="0" cellspacing="1" cellpadding="3" align="center">
                    <tr>
                      <td><strong>Product Name:</strong></td>
                      <td colspan="2"><font style="text">
                        <input name="name" type="text" value="" size="60">
                      </font></td>
                    </tr>
                    <tr>
                      <td width="26%"><strong>Catagory:</strong></td>
                      <td colspan="2"><select name="catagory">
                          <?  
// Query to pull information from the "catagory" Database  
$result = mysql_query("select * from $table13 order by id DESC");  
while ($row = mysql_fetch_object($result)){  
?>
                          <option value="<?php echo $row->name; ?>"> <?php echo $row->name; ?> </option>
                          <? }?>
                      </select></td>
                      </tr>
                    <tr>
                      <td valign="top"><strong>Manufacturer:</strong></td>
                      <td colspan="2"><select name="make">
                        <?  
// Query to pull information from the "Manufacturers" Database  
$result = mysql_query("select * from $table2 order by id DESC");  
while ($row = mysql_fetch_object($result)){  
?>
                        <option value='<?php echo $row->make; ?>'> <?php echo $row->make; ?> </option>
                        <?}?>
                      </select></td>
                    </tr>
                    <tr>
                      <td valign="top"><strong>Description:</strong></td>
                      <td colspan="2"><font style="text">
                        <textarea name="description" cols="50" rows="6"></textarea>
                      </font></td>
                    </tr>
                    <tr>
                      <td><span style="font-weight: bold"><font style="text">Price: </font></span></td>
                      <td width="36%"><font style="text">
                        <input name="price" type="text" value="" size="20">
                      </font><font style="text">                       </font></td>
                      <td width="38%"> </td>
                    </tr>
                    <tr>
                      <td><span style="font-weight: bold"><font style="text">Colour: </font></span></td>
                      <td colspan="2"><font style="text">
                        <input name="colour" type="text" value="" size="30">
                      </font><span class="style4">Size is displayed in inches!!</span></td>
                      </tr>
                    <tr>
                      <td><strong>Sold:</strong></td>
                      <td><select name="sold">
                        <?  
// Query to pull information from the "product" Database  
$result = mysql_query("select * from $table5 order by id DESC");  
while ($row = mysql_fetch_object($result)){  
?>
                        <option value='<?php echo $row->sold; ?>'> <?php echo $row->sold; ?> </option>
                        <?}?>
                      </select></td>
                      <td> </td>
                    </tr>
                    <tr class=table2>
                      <td><strong>Size:</strong></td>
                      <td colspan="2"><font style="text">
                        <input name="size" type="text" value="" size="30" />
                      </font></td>
                    </tr>
                    <tr class=table2>
                      <td><strong>Age:</strong></td>
                      <td colspan="2"><font style="text">
                        <input name="age" type="text" value="" size="30" />
                      </font></td>
                    </tr>
                    <tr class=table2>
                      <td><strong>Reef Safe:</strong></td>
                      <td colspan="2"><font style="text">
                        <input name="safe" type="text" value="" size="30" />
                      </font></td>
                    </tr>
                    <tr class=table2>
                      <td><strong>Flow:</strong></td>
                      <td colspan="2"><font style="text">
                        <input name="flow" type="text" value="" size="30" />
                      </font></td>
                    </tr>
                    <tr class=table2>
                      <td><b>IMAGES:</b><br>
                          <span class="style1"><font size="1">Please select the images (jpeg only):</font></span></td>
                      <td colspan="2"> 1: Upload :
                          <input name="imageOne" type="file" class="bginput" id="imageOne">
                          <br>
            2: Upload :
            <input name="imageTwo" type="file" class="bginput" id="imageTwo">
            <br>
            3: Upload :
            <input name="imageThree" type="file" class="bginput" id="imageThree">
            <br>
            4: Upload :
            <input name="imageFour" type="file" class="bginput" id="imageFour">
            <br>
            5: Upload :
            <input name="imageFive" type="file" class="bginput" id="imageFive">
            <br>
            6: Upload :
            <input name="imageSix" type="file" class="bginput" id="imageSix">
            <br>
            7: Upload :
            <input name="imageThumb" type="file" class="bginput" id="imageThumb">
            - Thumbnail Image</td>
                    </tr>
                    <!-- end of new code -->
                    <tr class=table1>
                      <td colspan="3" align="center" valign="top">
                        <input type="image"  src="../images/submit.gif" name="submit" value="Done!">
                        <input type="hidden" name="ID" value="<?php echo $ID; ?>">
                        <input type="image"  src="../images/reset.gif" name="reset" value="Reset">                      </td>
                    </tr>
                </table></td>
                <td width="4" background="images/right_menu.gif" bgcolor="#f2f3f5"><img src="images/right_menu.gif" width="4" height="1" /></td>
              </tr>
              <tr>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_left_corner.gif" width="4" height="4" /></td>
                <td height="4" background="images/bottom_menu.gif" bgcolor="#f2f3f5"><img src="images/bottom_menu.gif" width="1" height="4" /></td>
                <td width="4" height="4" bgcolor="#f2f3f5"><img src="images/bottom_right_corner.gif" width="4" height="4" /></td>
              </tr>
            </table></td>
          </tr>
        </table>        <br>        </td>
    </tr>
  </table>
      </FORM>
</td>
  </tr>
</table>
<? include("includes/footer.php") ?>

 

i dont know where to start on adding sub catagories, and / or how i should go about adding a product to a sub catagory instead of the already in place catagory!

 

so instead of adding products to the fish catagory i want something like the following;

 

++ Fish

+Marine

+Freshwater

+Tropical

 

++Plants

+Corals

+Fresh Water

 

hope this explains better :(

 

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.