php.de

Zurück   php.de > Webentwicklung > PHP Einsteiger > PHP Tipps 2005

 
 
LinkBack Themen-Optionen Thema bewerten
Alt 11.05.2005, 14:47  
Gast
 
Beiträge: n/a
Standard chainedSelector

Hi Phreakz!

Ich habe 2 „Selection Boxes“ die miteinander verkettet sind wobei die werte beider aus einer Mysql DB ausgelesen werden. D.h. wenn man einen wert in der ersten auswählt werden in der zweiten nur werte angezeigt die in Verbindung stehen zu der ersten. Mein Problem ist das ich eine dritte „Selection Box“ habe die nicht verkettet ist und dessen werte ich zusammen mit der verketteten und noch einem wert aus einem Eingabe Feld in eine Tabelle einfügen will!
Dies ist mein erstes PHP Projekt deshalb wende ich mich an euch mit der Hoffnung Hilfe zu finden, danke im Voraus!
PHP-Code:
<?php
    
/*
    ** get chainedSelectors class
    */
    
require("chainedSelectors.php");
    require_once 
"dbstart.inc.php";

    
/*
    ** create input data
    */

    //prepare names
    
$selectorNames = array(
        
CS_FORM=>"DetUpload",
        
CS_FIRST_SELECTOR=>"ProdGrp",
        
CS_SECOND_SELECTOR=>"Product");

    
//query database, assemble data for selectors
    
$Query "SELECT s.ProdGrpId, s.ProdGrpName, a.ProdName " .
        
"FROM Product a, ProdGrp s " .
        
"WHERE a.ProdGrpId = s.ProdGrpId " .
        
"ORDER BY s.ProdGrpName, a.ProdName";
    if(!(
$DatabaseResult mysql_query($Query$DatabaseLink)))
    {
        print(
"The query failed!
\n"
);
        exit();
    }

    while(
$row mysql_fetch_object($DatabaseResult))
    {
        
$selectorData[] = array(
            
CS_SOURCE_ID=>$row->ProdGrpId,
            
CS_SOURCE_LABEL=>$row->ProdGrpName,
            
CS_TARGET_ID=>$row->ProdId,
            
CS_TARGET_LABEL=>$row->ProdName);
    }

    
//instantiate class
    
$Product = new chainedSelectors(
        
$selectorNames,
        
$selectorData);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html40/loose.dtd">
<html>
<head>
<title>Detail Upload</title>
<script type="text/javascript" language="JavaScript">
<?php
    $Product
->printUpdateFunction();
?>
</script>
</head>
<body>
<form name="DetUpload" action="<? echo $PHP_SELF ?>">
<?php
    $Product
->printSelectors();
?>
<script type="text/javascript" language="JavaScript">
<?php
    $Product
->initialize();
?>
</script>
<?php

if ($_POST['detsubmit']) {
  
$DetGrpId = isset($_POST['DetGrpId']) ? $_POST['DetGrpId'] : '';
  
$DetName = isset($_POST['DetName']) ? $_POST['DetName'] : '';
  


  if (
$DetName != '' && $DetGrpName != '') {
    
$query "INSERT INTO Detail (ProdId, DetGrpId, DetName)
                VALUES ('" 
$ProdId "', '" $DetGrpId "','$DetName')";
    
$result mysql_query($query) or die("Fehler:
$query
mysql_error());

    if (
$result){
        echo 
"[b]Eintrag erfolgreich übernommen![/b]
"
;
        echo 
'[url="' $_SERVER['PHP_SELF'] . '"]Weiteres Produkt eintragen[/url]';
    }
    else {
        echo 
"[b]Fehler: Datensatz nicht eingefügt.[/b]";
    }
  }
}
else {

?>

    <select size='15' name='DetGrpName'>

<?php

$query 
"SELECT DetGrpId, DetGrpName
               FROM DetGrp"
;
$result mysql_query($query) or die("Fehler:
$query1
mysql_error());
while(
$row mysql_fetch_assoc($result)) {
    echo 
"<option value='" $row['DetGrpId'] . "'>" $row['DetGrpName'] . "</option>";
}
echo 
"</select></p>";
echo 
"<input name='DetName' type='text' size='25' maxlength='50' maxsize='50'>";
echo 
"

<input type='submit' name='detsubmit' value='Detail Uploaden'></p>"
;
echo 
"</form>";
}
?>
</body>
</html>
Ja und noch die chainedSelectors Klasse:
PHP-Code:
<?php
    
/*
    ** Class: chainedSelectors
    ** Description: This class allows you to create two selectors.  Selections
    ** made in the first selector cause the second selector to be updated.
    ** PHP is used to dynamically create the necessary JavaScript.
    */

    //These constants make the code a bit more readable.  They should be
    //used in in the creation of the input data arrays, too.
    
define("CS_FORM"0);
    
define("CS_FIRST_SELECTOR"1);
    
define("CS_SECOND_SELECTOR"2);

    
define("CS_SOURCE_ID"0);
    
define("CS_SOURCE_LABEL"1);
    
define("CS_TARGET_ID"2);
    
define("CS_TARGET_LABEL"3);

    class 
chainedSelectors
    
{
        
/*
        ** Properties
        */

        //Array of names for the form and the two selectors.
        //Should take the form of array("myForm", "Selector1", "Selector2")
        
var $names;

        
//Array of data used to fill the two selectors
        
var $data;

        
//Unique set of choices for the first selector, generated on init
        
var $uniqueChoices;

        
//Calculated counts
        
var $maxTargetChoices;
        var 
$longestTargetChoice;


        
/*
        ** Methods
        */

        //constructor
        
function chainedSelectors($names$data)
        {
            
/*
            **copy parameters into properties
            */
            
$this->names $names;
            
$this->data $data;

            
/*
            ** traverse data, create uniqueChoices, get limits
            */
            
foreach($data as $row)
            {
                
//create list of unique choices for first selector
                
$this->uniqueChoices[($row[CS_SOURCE_ID])] = $row[CS_SOURCE_LABEL];

                
//find the maximum choices for target selector
                
$maxPerChoice[($row[CS_SOURCE_ID])]++;

                
//find longest value for target selector
                
if(strlen($row[CS_TARGET_LABEL]) > $this->longestTargetChoice)
                {
                    
$this->longestTargetChoice=strlen($row[CS_TARGET_LABEL]);
                }
            }

            
$this->maxTargetChoices max($maxPerChoice);
        }

        
//prints the JavaScript function to update second selector
        
function printUpdateFunction()
        {
            
/*
            ** Create some variables to make the code
            ** more readable.
            */
            
$sourceSelector "document." $this->names[CS_FORM] . "." .
                
$this->names[CS_FIRST_SELECTOR];
            
$targetSelector "document." $this->names[CS_FORM] . "." .
                
$this->names[CS_SECOND_SELECTOR];

            
/*
            ** Start the function
            */
            
print("function update" .$this->names[CS_SECOND_SELECTOR] . "()\n");

            print(
"{\n");

            
/*
            ** Add code to clear out next selector
            */
            
print("\t//clear " $this->names[CS_SECOND_SELECTOR] . "\n");
            print(
"\tfor(index=0; index < $this->maxTargetChoices; index++)\n");
            print(
"\t{\n");
            print(
"\t\t" $targetSelector ".options[index].text = '';\n");
            print(
"\t\t" $targetSelector ".options[index].value = '';\n");
            print(
"\t}\n\n");
            print(
"\t" $targetSelector ".options[0].selected = true;\n\n");

            
/*
            ** Add code to find which was selected
            */
            
print("whichSelected = " $sourceSelector ".selectedIndex;\n");

            
/*
            ** Add giant "if" tree that puts values into target selector
            ** based on which selection was made in source selector
            */

            //loop over each value of this selector
            
foreach($this->uniqueChoices as $sourceValue=>$sourceLabel)
            {
                print(
"\tif(" $sourceSelector .
                    
".options[whichSelected].value == " .
                    
"'$sourceValue')\n");
                print(
"\t{\n");

                
$count=0;
                foreach(
$this->data as $row)
                {
                    if(
$row[0] == $sourceValue)
                    {
                        
$optionValue $row[CS_TARGET_ID];
                        
$optionLabel $row[CS_TARGET_LABEL];

                        print(
"\t\t" $targetSelector .
                            
".options[$count].value = '$optionValue';\n");
                        print(
"\t\t" $targetSelector .
                            
".options[$count].text = '$optionLabel';\n\n");

                        
$count++;
                    }
                }

                print(
"\t}\n\n");
            }

            print(
"\treturn true;\n");
            print(
"}\n\n");

        }

        
//print the two selectors
        
function printSelectors()
        {
            
/*
            **create prefilled first selector
            */
            
$selected=TRUE;
            print(
"<select size='15' name=\"" $this->names[CS_FIRST_SELECTOR] . "\" " .
                
"onChange=\"update".$this->names[CS_SECOND_SELECTOR]."();\">\n");
            foreach(
$this->uniqueChoices as $key=>$value)
            {
                print(
"\t<option value=\"$key\"");
                if(
$selected)
                {
                    print(
" selected=\"selected\"");
                    
$selected=FALSE;
                }
                print(
">$value</option>\n");
            }
            print(
"</select>\n");

            
/*
            **create empty target selector
            */
            
$dummyData str_repeat("X"$this->longestTargetChoice);

            print(
"<select size='15' name=\"".$this->names[CS_SECOND_SELECTOR]."\">\n");
            for(
$i=0$i $this->maxTargetChoices$i++)
            {
                print(
"\t<option value=\"\">$dummyData</option>\n");
            }
            print(
"</select>\n");

        }

        
//prints a call to the update function
        
function initialize()
        {
            print(
"update" .$this->names[CS_SECOND_SELECTOR] . "();\n");
        }
    }
?>
?>[/php]
 
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 11.05.2005, 15:53  
Gast
 
Beiträge: n/a
Standard

Übrigens es würde mir reichen wenn jemand in der Lage ist mir zu erklären wie ich die ausgewählten Werte des "chainedSelectors" in eine Variable bekomme um sie weiterverwenden zu können! Danke
 
Alt 11.05.2005, 16:08  
Moderator
 
Benutzerbild von robo47
 
Registriert seit: 03.09.2004
Beiträge: 11.792
PHP-Kenntnisse:
Fortgeschritten
robo47 kann auf vieles stolz seinrobo47 kann auf vieles stolz seinrobo47 kann auf vieles stolz seinrobo47 kann auf vieles stolz seinrobo47 kann auf vieles stolz seinrobo47 kann auf vieles stolz seinrobo47 kann auf vieles stolz seinrobo47 kann auf vieles stolz sein
Standard

da stellt sich mir doch die frage
1) -> selbst geschrieben? wohl nicht
2) woher ?
3) ham die vieleicht ne faq zu der klasse
4) versteh ich nciht was genau du bei deinem formular verknüpft hast / haben willst
robo47 ist offline  
Alt 12.05.2005, 11:14  
Gast
 
Beiträge: n/a
Standard

die Js klasse nicht Rest schon!
hab vergessen die Id abzufragen, Problem gelöst Danke trotzdem!!!
manchmal reichts eine Nacht drüber zu schlafen.
 
 


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu


Alle Zeitangaben in WEZ +2. Es ist jetzt 13:34 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum

Creative Commons License
Dieser Inhalt ist unter einer Creative Commons-Lizenz lizenziert.