Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2003, 03:04 PM   PM User | #1
NanoEntity
New to the CF scene

 
Join Date: Sep 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
NanoEntity is an unknown quantity at this point
Generating external js subcategory list from MySQL using PHP

Quote:
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.

PHP Code:
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
PHP Code:
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 Code:
<?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
PHP Code:
CREATE TABLE `forum` (
  `
forumidsmallint(5unsigned NOT NULL auto_increment,
  `
titlevarchar(100NOT NULL default '',
  `
displayordersmallint(6NOT NULL default '0',
  `
parentidsmallint(6NOT NULL default '0',
  `
parentlistvarchar(250NOT NULL default '',
  `
childlistvarchar(250NOT NULL default '',
  
PRIMARY KEY  (`forumid`)
TYPE=MyISAM AUTO_INCREMENT=;

#
# Dumping data for table `forum`
#

INSERT INTO `forumVALUES (1'Main Category 1'1, -1'1,-1''1,2,5,-1');
INSERT INTO `forumVALUES (2'SubCategory 1 order 1'11'2,1,-1''2,-1');
INSERT INTO `forumVALUES (5'SubCategory 1 order 2'21'5,1,-1''5,-1');
INSERT INTO `forumVALUES (3'Main Category 2'2, -1'3,-1''3,4,6,-1');
INSERT INTO `forumVALUES (4'SubCategory 2 order 1'13'4,3,-1''4,-1'); 
----------------------------------------------------------

Last edited by NanoEntity; 12-15-2003 at 03:24 PM..
NanoEntity is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:00 AM.


Advertisement
Log in to turn off these ads.