Jump to content

Categories


HektoR

Recommended Posts

I will not develop the whole system. But since I needed a script to do categories for myself here is a basic usage script. This will get categories in a table in an array in the order specified.

 

<?php
/*
* My attempt at a category script.
*/

/*
create table categories (
catid INT(11) NOT NULL auto_increment,
parentid INT(11) NOT NULL default '0',
catname varchar(50) NOT NULL,
catseoname varchar(50) NOT NULL,
disporder INT(3) NOT NULL default '0',
primary key(catid)
);
*/

mysql_connect("localhost", "root", "");
mysql_select_db("cat");

/*
my test data
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('0', 'Test1', 'test1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('1', 'Test1-1', 'test1-1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('1', 'Test1-2', 'test1-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('5', 'Test2-1', 'test2-1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('0', 'Test2', 'test2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('5', 'Test2-2', 'test2-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('7', 'Test2-2-2', 'test2-2-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('7', 'Test2-2-1', 'test2-2-1', '0');
*/

function retrieveCategoryList() {
$query = mysql_query("SELECT catid, parentid, catname, catseoname, disporder FROM categories WHERE parentid = 0 ORDER BY disporder, catname");

while ($row = mysql_fetch_assoc($query)) {
	$catArray[$row['catname']]['id'] = $row['catid'];
	$catArray[$row['catname']]['parentid'] = $row['parentid'];
	$catArray[$row['catname']]['catseoname'] = $row['catseoname'];
	$catArray[$row['catname']]['disporder'] = $row['disporder'];
	$catArray[$row['catname']]['subcats'] = fetchSubCat($row['catid']);
}

return $catArray;
}

// recursive
function fetchSubCat($parentid) {
$query = mysql_query("SELECT catid, parentid, catname, catseoname, disporder FROM categories WHERE parentid = " . $parentid . " ORDER BY disporder, catname");
$numRows = mysql_num_rows($query);
$catArray = "none";
if ($numRows > 0) {
	$catArray=array();
	while ($row = mysql_fetch_assoc($query)) {
		$catArray[$row['catname']]['id'] = $row['catid'];
		$catArray[$row['catname']]['parentid'] = $row['parentid'];
		$catArray[$row['catname']]['catseoname'] = $row['catseoname'];
		$catArray[$row['catname']]['disporder'] = $row['disporder'];
		$catArray[$row['catname']]['subcats'] = fetchSubCat($row['catid']);
	}
}

return $catArray;
}
?>

 

You will need to figure out how to use this to add more categories/edit/ get a category etc. Like I said it is very basic but hopefully it will help you on your way.

Link to comment
Share on other sites

And edit to the above test data:

 

/*
my test data
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('0', 'Test1', 'test1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('1', 'Test1-1', 'test1-1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('1', 'Test1-2', 'test1-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('5', 'Test2-1', 'test2-1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('0', 'Test2', 'test2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('5', 'Test2-2', 'test2-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('6', 'Test2-2-2', 'test2-2-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('6', 'Test2-2-1', 'test2-2-1', '0');
*/

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.