Jump to content

Archived

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

spires

form dropdowns, creating submenues.

Recommended Posts

Hi,

Can anyone tell me how to create a submenu?

What i am trying to do is, have one dropdown with all of the categorys in, once you select the category,
the second dropdown will have only the submenus to the selected category.

E.G
Crew = riggers
           bouncers
           door staff
laser = installtion
        = porduction
etc.

at the moment i can only get the sub category dropdown to have the full list, not not a list based on the main cat.

Any idears please.
[code]
<?php
$query = "SELECT * FROM category order by id DESC";
$result = mysql_query($query) or die ("query 1 failed");
$count = mysql_num_rows($result);

        $cat = '<select name="cat">
<option value="NULL" selected>- - - - - - - </option>';

while ($row = mysql_fetch_array($result)) {
$category = $row['cat'];
                 $cat .= '<option value="'.$category.'">'.$category.'</option>';
  }

                 $cat .= '</select>';


$query1 = "SELECT * FROM categories";
$result1 = mysql_query($query1) or die ("query 3 failed");
$count1 = mysql_num_rows($result1);

$sub = '<select name="sub">
<option value="NULL" selected>- - - - - - - </option>';
while ($row1 = mysql_fetch_array($result1)) {
$sub .= '<option value="'.$row1['sub'].'">'.$row1['sub'].'</option>';
}
$sub .= '</select>';
?>
[/code]

http://www.theeventorganiser.com/register.php


Thanks for your help

Share this post


Link to post
Share on other sites
I think i just need a way to tell what category has been selected without hitting the submit button.

Share this post


Link to post
Share on other sites
Sounds like a job for javascript. Here's a tutorial I found in google:

http://www.quirksmode.org/js/options.html

Share this post


Link to post
Share on other sites
Or use Barand's script, BaaSelect.

http://members.aol.com/barryaandrew/baaselect/baaselectguide.html

Share this post


Link to post
Share on other sites
Thanks for the plug, Hitman

There are limitations with baaSelect. You need a db structure like this
[pre]
category          subcategory
----------        -------------
catID      --+    subcatID
category    |    subcategory
            +-<  catID[/pre]

and catID and subcatID need to be numeric (int)

Share this post


Link to post
Share on other sites
First off: "baaselect" rocks!!  ;D
One question:
How do you create 3 drop-downs from that code? That would really make my day!!  ;D ;)

Share this post


Link to post
Share on other sites
Try this
[code]
<?php
include 'db.php'; // connection stuff
include 'baaselect.php';

$sel = new baaSelect();
$sel->addSelect('cat', 'category', 'id', 'catname' , '', 1 , '-select category-' );
$sel->addSelect('subcat', 'subcategory', 'subcat_id', 'subcategory' , 'cat_id', 1 , '-select subcategory-' );
$sel->addSelect('product', 'prodname', 'prodno', 'proddesc' , 'subcat_id', 1 , '-select product-' );

?>
<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>Sample baaSelect</title>
<meta name="author" content="Barand">

<?php
    $sel->makeScript();
?>

</head>
<body>
<form>

<?php
    $sel->makeSelect('cat');
    $sel->makeSelect('subcat');
    $sel->makeSelect('product');
?>
<br />
<input type="submit" name="action" value="Submit">
</form>
</body>
</html>
[/code]

This will create the test data
[code]
-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version 5.0.18-nt


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;


--
-- Create schema test
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ test;
USE test;

--
-- Table structure for table `test`.`category`
--

DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
  `id` int(11) NOT NULL auto_increment,
  `catname` varchar(30) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `test`.`category`
--

/*!40000 ALTER TABLE `category` DISABLE KEYS */;
INSERT INTO `category` (`id`,`catname`) VALUES
(1,'Cat 1'),
(2,'Cat 2'),
(3,'Cat 3'),
(4,'Cat 4'),
(5,'Cat 5');
/*!40000 ALTER TABLE `category` ENABLE KEYS */;


--
-- Table structure for table `test`.`prodname`
--

DROP TABLE IF EXISTS `prodname`;
CREATE TABLE `prodname` (
  `prodno` int(11) NOT NULL auto_increment,
  `proddesc` varchar(20) default NULL,
  `subcat_id` int(11) default NULL,
  PRIMARY KEY  (`prodno`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `test`.`prodname`
--

/*!40000 ALTER TABLE `prodname` DISABLE KEYS */;
INSERT INTO `prodname` (`prodno`,`proddesc`,`subcat_id`) VALUES
(1,'a',11),
(2,'b',11),
(3,'cc',21),
(4,'dd',21),
(5,'eee',31),
(6,'fff',31),
(7,'gggg',41),
(8,'hhhh',12),
(9,'jjjjjj',12),
(10,'kkkkkk',13),
(11,'mmmm',13),
(12,'nnnnn',22),
(13,'ppppp',22),
(14,'qqq',32),
(15,'rrrrrrr',31),
(16,'ssssssss',41),
(17,'ttttttt',41),
(18,'uuuuuuuu',51),
(19,'vvvvvvvvv',51);
/*!40000 ALTER TABLE `prodname` ENABLE KEYS */;


--
-- Table structure for table `test`.`subcategory`
--

DROP TABLE IF EXISTS `subcategory`;
CREATE TABLE `subcategory` (
  `subcat_id` int(10) unsigned NOT NULL,
  `subcategory` varchar(45) NOT NULL,
  `cat_id` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`subcat_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `test`.`subcategory`
--

/*!40000 ALTER TABLE `subcategory` DISABLE KEYS */;
INSERT INTO `subcategory` (`subcat_id`,`subcategory`,`cat_id`) VALUES
(11,'subcat 11',1),
(12,'subcat 12',1),
(13,'subcat 13',1),
(21,'subcat 21',2),
(22,'subcat 22',2),
(31,'subcat 31',3),
(32,'subcat 32',3),
(41,'subcat 41',4),
(51,'subcat 51',5);
/*!40000 ALTER TABLE `subcategory` ENABLE KEYS */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
[/code]

Share this post


Link to post
Share on other sites
Thank you so much Barand, that works great! ;D  ;D
Hmm, I don't want to be pushing my luck here, but can you create 2 series of drop-downs on the same page?
I could then use separate submit buttons for each group to define the resulting query... and save me an extra web page.

Share this post


Link to post
Share on other sites
Is this [code]$sel = new baaSelect();[/code] etc. and [code]$sel2 = new baaSelect();[/code] and so on? Sorry, I'm not into the jargon yet...

Share this post


Link to post
Share on other sites

×

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.