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
https://forums.phpfreaks.com/topic/133361-categories/#findComment-693684
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
https://forums.phpfreaks.com/topic/133361-categories/#findComment-693707
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.