Не стал я всё усложнять и всётаки нашёл вроде не плохой образец. Файл с запросами для БД:
| Код | -- phpMyAdmin SQL Dump -- version 2.6.1 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generatie Tijd: 07 Aug 2005 om 10:02 -- Server versie: 4.1.9 -- PHP Versie: 4.3.10 -- -- Database: `jeugdfuif` --
-- --------------------------------------------------------
-- -- Tabel structuur voor tabel `hoofd_akt` --
CREATE TABLE `hoofd_akt` ( `idnumber` int(11) NOT NULL auto_increment, `akt_groep` varchar(30) collate latin1_general_ci NOT NULL default '', PRIMARY KEY (`idnumber`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=7 ;
-- -- Gegevens worden uitgevoerd voor tabel `hoofd_akt` --
INSERT INTO `hoofd_akt` VALUES (1, 'sport'); INSERT INTO `hoofd_akt` VALUES (2, 'gezelschapsspelen'); INSERT INTO `hoofd_akt` VALUES (4, 'concerten'); INSERT INTO `hoofd_akt` VALUES (5, 'tentoonstelling'); INSERT INTO `hoofd_akt` VALUES (6, 'fuif');
-- --------------------------------------------------------
-- -- Tabel structuur voor tabel `sub_akt` --
CREATE TABLE `sub_akt` ( `idnumber` int(11) NOT NULL auto_increment, `vr_sleutel` int(11) NOT NULL default '0', `akt_titel` varchar(30) collate latin1_general_ci NOT NULL default '', PRIMARY KEY (`idnumber`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=17 ;
-- -- Gegevens worden uitgevoerd voor tabel `sub_akt` --
INSERT INTO `sub_akt` VALUES (1, 2, 'schaken'); INSERT INTO `sub_akt` VALUES (2, 2, 'kaarten'); INSERT INTO `sub_akt` VALUES (3, 2, 'monopolie'); INSERT INTO `sub_akt` VALUES (4, 2, 'bingo'); INSERT INTO `sub_akt` VALUES (5, 1, 'zwemmen'); INSERT INTO `sub_akt` VALUES (6, 1, 'voetbal'); INSERT INTO `sub_akt` VALUES (7, 1, 'paarden'); INSERT INTO `sub_akt` VALUES (8, 1, 'atletiek'); INSERT INTO `sub_akt` VALUES (9, 4, 'klassiek'); INSERT INTO `sub_akt` VALUES (10, 4, 'modern'); INSERT INTO `sub_akt` VALUES (11, 5, 'kunst'); INSERT INTO `sub_akt` VALUES (12, 5, 'hobby'); INSERT INTO `sub_akt` VALUES (13, 5, 'historisch'); INSERT INTO `sub_akt` VALUES (14, 5, 'museum'); INSERT INTO `sub_akt` VALUES (15, 6, '<16 jaar'); INSERT INTO `sub_akt` VALUES (16, 6, 'volksfeest');
|
Файл с подключением к БД:
| Код | <?php /* conexxion data*/
$dbhost='localhost'; $loginname='db_user_name'; $loginpass='db_user_pass'; $dbname='db_name'; $id_link = @mysql_connect($dbhost, $loginname, $loginpass); if( ! $id_link ){ echo 'Cannot login'; }else{ //echo 'Succesfull conected to db'; } mysql_select_db($dbname, $id_link) or die(mysql_error); ?>
|
А также файл в котором можно посмотреть работу.
| Код | <html> <head> <title>Linked comboboxes with php-mysql</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <?php /** we need to have the previously made choice from a database or not if this form is accessed the first time change $passed_value and $passed_value2 to see the effect of the code 'ON LOAD' **/ if (!isset($passed_value)){ $passed_value=2;}// example if (!isset($passed_value2)){ $passed_value2=3;}// example // in the body onload sequence we call a function 'redirect' to set our comboboxes ?> <body onload="redirect(<?php echo $passed_value.','.$passed_value2?>,2)"> <?php
// standard conexxion see the included file conexxion.php include('conexxion.php'); // select the data from table 'hoofd_akt' for the main aktivities for the first combobox called "example" $sql="select * from hoofd_akt"; // select the data from 'sub_akt' for the linked sub category for the second combo box stage 2 $sql2="select * from sub_akt WHERE vr_sleutel=".$passed_value; $result=mysql_query($sql,$id_link) or die (mysql_error()); $result2=mysql_query($sql2,$id_link) or die (mysql_error()); ?> <!-- --><form name="doublecombo"> <select name="example" size="1" onChange="redirect(this.options.selectedIndex,<?php echo $passed_value.','.$passed_value2?>)"> <option>-select-</option>
<?php // echo the options for box 1 and 2 // as the idnumber is stored in the table with all results, here we compare to set SELECTED // this is a standard formula for retrieving stored info about a box selection while ($newarray=mysql_fetch_array($result)) { echo '<option value="'.$newarray['idnumber'].'" '; if ($newarray['idnumber']==$passed_value) {echo "SELECTED";}; echo '>'.$newarray['akt_groep'].'</option>'.'\n'; } ?> </select> <select name="stage2" size="1"> <?php // here we do the same for the second box while ($newarray=mysql_fetch_array($result2)) { echo '<option value="'.$newarray['idnumber'].'" '; if ($newarray['idnumber']==$passed_value2) {echo "SELECTED";}; echo '>'.$newarray['akt_titel'].'</option>'; $x=$x+1;} ?> </select>
<script> <!-- /* The original JScript can be found in nearly every JScript website I'm just mentioning one :) Courtesy of SimplytheBest.net - http://simplythebest.net */ var groups=document.doublecombo.example.options.length var group=new Array(groups) for (i=0; i<groups; i++){ group[i]=new Array
} <?php //Here comes the data from the two linked tables in a format suited for JScripting // into the script mentioned here above
$sql="select * from hoofd_akt";
$result=mysql_query($sql,$id_link) or die (mysql_error()); $x=0;$y=0;$m=0;$z=1;
while ($newarray=mysql_fetch_array($result)) { echo "group[0][$m]=new Option(\"".$newarray['akt_groep'].'","'.$newarray['idnumber'].'")'."\n"; $sql2="select * from sub_akt WHERE vr_sleutel=".$newarray['idnumber']." order by idnumber asc"; $result2=mysql_query($sql2,$id_link) or die (mysql_error()); while ($newarray2=mysql_fetch_array($result2)) { echo "group[$z][$y]=new Option(\"".$newarray2['akt_titel'].'","'.$newarray2['idnumber'].'")'."\n"; $y=$y+1; } $z=$z+1; $y=0; $m=$m+1; } ?>
var temp=document.doublecombo.stage2 function redirect(x,d,f){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){ temp.options[i]=new Option(group[eval(x)][i].text,group[eval(x)][i].value) } temp.options[d].selected=true
}
//--> </script> </form><!-- --> </body> </html>
|
уже потестил: http://test.art-coder.com/linked-combo4.php
Добавлено через 40 секунд Осталось только в этом разобраться... |