Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Sep 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Generating external js subcategory list from MySQL using PHP

    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.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •