...

View Full Version : Generating external js subcategory list from MySQL using PHP



NanoEntity
12-15-2003, 03:04 PM
What I want to generate is a list to something like this, this is only an example what I want, if anyone would be so kind and help me here that would be great.


var subcatArray = new Array(2);
subcatArray[1] = new Array(
new Array('2', 'SubCategory 1 order 1'),
new Array('5', 'SubCategory 1 order 2')
);
subcatArray[2] = new Array(
new Array('4', 'SubCategory 2 order 1'),
);


----------------------------------------------------------
Generated JS currentually

var subcatArray = new Array(3);
subcat[] = new subcat(2, 'SubCategory 1 order 1');
subcat[] = new subcat(5, 'SubCategory 1 order 2');
subcat[] = new subcat(4, 'SubCategory 2 order 1');



SCRIPT RUNING - .php?type=js

<?php
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');
require_once('./includes/functions_external.php');

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################
// chosen forums
$forums = $DB->query("
SELECT title, forumid
FROM " . TABLE_PREFIX . "forum
WHERE parentid > -1
ORDER BY parentid,displayorder
");

$forumcache = array();
while ($forum = $DB_site->fetch_array($forums))
{ // fetch the forums
$forumcache[] = $forum;
}

$_REQUEST['type'] = strtoupper($_REQUEST['type']);
switch ($_REQUEST['type'])
{
case 'JS':
case 'XML':
case 'RSS2':
break;
default:
$_REQUEST['type'] = 'JS';
}

if ($_REQUEST['type'] == 'JS')
{ // javascript output

echo "var subcatArray = new Array(" . sizeof ($forumcache) . ");\r\n";
if (!empty($forumcache))
{
foreach ($forumcache AS $subcat)
{
$subcat['title'] = addslashes_js($subcat['title']);
echo "\tsubcat[] = new subcat($subcat[forumid], '$subcat[title]');\r\n";
}
}

}

?>

SQL Query


CREATE TABLE `forum` (
`forumid` smallint(5) unsigned NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`displayorder` smallint(6) NOT NULL default '0',
`parentid` smallint(6) NOT NULL default '0',
`parentlist` varchar(250) NOT NULL default '',
`childlist` varchar(250) NOT NULL default '',
PRIMARY KEY (`forumid`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;

#
# Dumping data for table `forum`
#

INSERT INTO `forum` VALUES (1, 'Main Category 1', 1, -1, '1,-1', '1,2,5,-1');
INSERT INTO `forum` VALUES (2, 'SubCategory 1 order 1', 1, 1, '2,1,-1', '2,-1');
INSERT INTO `forum` VALUES (5, 'SubCategory 1 order 2', 2, 1, '5,1,-1', '5,-1');
INSERT INTO `forum` VALUES (3, 'Main Category 2', 2, -1, '3,-1', '3,4,6,-1');
INSERT INTO `forum` VALUES (4, 'SubCategory 2 order 1', 1, 3, '4,3,-1', '4,-1');

----------------------------------------------------------



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum