tobmes
Goto Top

PHP Dropdownmenü mit SQL-Abfrage

Hi Experten,

ich glaube mein Titel ist etwas verwirrend, aber mir fällt gerade nichts besseres ein face-smile

Also hier mein Problem. Ich habe mir per PHP ein HTML-Dropdownmenü erstellt. Die Einträge dieses Menüs werden aus einer Datenbank abgefragt. Hier mal der Code zu dem Ganzen.

protected function dropDownMenu(){
    $dropDown = $this->_database->query("SELECT DISTINCT Lagerort FROM Artikel ORDER BY Lagerort ASC");  
      echo "<select>";  
      echo "<option> Bitte einen Lagerort auswählen </option>";  
      while($ergebnis = $dropDown->fetch_assoc()) {
      echo "<option>" . htmlspecialchars($ergebnis["Lagerort"]) . "</option>";  
    }
    echo "</select>";  

    }

Jetzt hätte ich gerne die Möglichkeit, wenn ich auf einen Eintrag klicke, dann soll mir eine Liste ausgegeben werden, die alle Artikel in diesem Lager enthält. Mir ist jetzt nicht so ganz klar, wie ich erkennen kann, auf was geklickt wurde. Für ein normales Textinput Feld habe ich es bis jetzt mit if (isset(....)) gemacht.

Danke euch schon mal für die Hilfe.


Gruß
-Tobmes


P.S Zum evtl. besseren Verständnis hier mal der ganze Code
<?php	// UTF-8 marker äöüÄÖÜ߀
/**
 * Class PageTemplate for the exercises of the EWA lecture
 * Demonstrates use of PHP including class and OO.
 * Implements Zend coding standards.
 * Generate documentation with Doxygen or phpdoc
 *
 * PHP Version 5
 *
 * @category File
 * @package  Pizzaservice
 * @author   Bernhard Kreling, <b.kreling@fbi.h-da.de>
 * @author   Ralf Hahn, <ralf.hahn@h-da.de>
 * @license  http://www.h-da.de  none
 * @Release  1.2
 * @link     http://www.fbi.h-da.de
 */

// to do: change name 'PageTemplate' throughout this file 
require_once './page.php';  

/**
 * This is a template for top level classes, which represent
 * a complete web page and which are called directly by the user.
 * Usually there will only be a single instance of such a class.
 * The name of the template is supposed
 * to be replaced by the name of the specific HTML page e.g. baker.
 * The order of methods might correspond to the order of thinking
 * during implementation.

 * @author   Bernhard Kreling, <b.kreling@fbi.h-da.de>
 * @author   Ralf Hahn, <ralf.hahn@h-da.de>
 */
class bestand extends Page
{
    // to do: declare reference variables for members
    // representing substructures/blocks

    /**
     * Instantiates members (to be defined above).
     * Calls the constructor of the parent i.e. page class.
     * So the database connection is established.
     *
     * @return none
     */
    protected function __construct()
    {
        parent::__construct();
        // to do: instantiate members representing substructures/blocks
    }

    /**
     * Cleans up what ever is needed.
     * Calls the destructor of the parent i.e. page class.
     * So the database connection is closed.
     *
     * @return none
     */
    protected function __destruct()
    {
        parent::__destruct();
    }

    /**
     * Fetch all data that is necessary for later output.
     * Data is stored in an easily accessible way e.g. as associative array.
     *
     * @return none
     */
    protected function getViewData()
    {
        // to do: fetch data for this view from the database

        echo <<<showHTML
        <form method="POST" action="bestand.php">  
        <input type="text" name="artikel" class="inputField" placeholder="Artikel">  
        <input type="submit" name="anzeigen" class="submit" value="Anzeigen">  
        </form>





showHTML;

if (isset($_POST["artikel"])){  
  $artikel = $_POST["artikel"];  
}
if (isset($_POST["anzeigen"])) {  
  $this->info($artikel);
}
    }



    protected function info($artikel) {
      $bestand = $this->_database->query("SELECT Lagerort,Bestand,NameKunde,NameBearbeiter FROM Artikel WHERE Artikel='$artikel'");  
      echo "<table>";  
      echo "<th>Lagerort</th><th>Bestand</th><th>Kunden Name</th><th>Weitere Infos</th>";  
      while($ergebnis = $bestand->fetch_assoc()) {
        echo "<tr><td>" . htmlspecialchars($ergebnis["Lagerort"]) . "</td>"  
                  . "<td>" . htmlspecialchars($ergebnis["Bestand"]) . "</td>"  
                  . "<td>" . htmlspecialchars($ergebnis["NameKunde"]) . "</td>"  
                  . "<td>" . htmlspecialchars($ergebnis["NameBearbeiter"]) . "</td>"  
                  . "</tr>";  
      }
      echo "</table>";  


    }

    protected function dropDownMenu(){

    $dropDown = $this->_database->query("SELECT DISTINCT Lagerort FROM Artikel ORDER BY Lagerort ASC");  
      echo "<select>";  
      echo "<option> Bitte einen Lagerort auswählen </option>";  
      while($ergebnis = $dropDown->fetch_assoc()) {
      echo "<option>" . htmlspecialchars($ergebnis["Lagerort"]) . "</option>";  
    }
    echo "</select>";  

    }

    /**
     * First the necessary data is fetched and then the HTML is
     * assembled for output. i.e. the header is generated, the content
     * of the page ("view") is inserted and -if avaialable- the content of 
     * all views contained is generated.
     * Finally the footer is added.
     *
     * @return none
     */
    protected function generateView()
    {
        $this->getViewData();
        $this->generatePageHeader('Bestand');  
        // to do: call generateView() for all members
        // to do: output view of this page
        $this->generatePageFooter();
    }

    /**
     * Processes the data that comes via GET or POST i.e. CGI.
     * If this page is supposed to do something with submitted
     * data do it here.
     * If the page contains blocks, delegate processing of the
	 * respective subsets of data to them.
     *
     * @return none
     */
    protected function processReceivedData()
    {
        parent::processReceivedData();
        //bestand::dropDownMenu();


        // to do: call processReceivedData() for all members
    }

    /**
     * This main-function has the only purpose to create an instance
     * of the class and to get all the things going.
     * I.e. the operations of the class are called to produce
     * the output of the HTML-file.
     * The name "main" is no keyword for php. It is just used to 
     * indicate that function as the central starting point.
     * To make it simpler this is a static function. That is you can simply
     * call it without first creating an instance of the class.
     *
     * @return none
     */
    public static function main()
    {
        try {
            $page = new bestand();
            $page->processReceivedData();
            $page->generateView();
        }
        catch (Exception $e) {
            header("Content-type: text/plain; charset=UTF-8");  
            echo $e->getMessage();
        }
    }
}

// This call is starting the creation of the page.
// That is input is processed and output is created.
bestand::main();

// Zend standard does not like closing php-tag!
// PHP doesn't require the closing tag (it is assumed when the file ends). 
// Not specifying the closing ? >  helps to prevent accidents
// like additional whitespace which will cause session
// initialization to fail ("headers already sent"). 
//? >

Verstösst wahrscheinlich gegen alle guten Dinge face-smile

Content-Key: 306802

Url: https://administrator.de/contentid/306802

Printed on: April 19, 2024 at 05:04 o'clock

Member: Arano
Arano Jun 10, 2016 at 21:33:17 (UTC)
Goto Top
Hallo tobmes,

das simpelste dürft wohl sein wenn du dem <select> einen "name" verpasst (z.B.: LagOrt) und jeder <option> einen "value" (z.B.: die LagOrt-ID aus der DB oder den LagOrtNamen selbst).
Dann kannst du in PHP wieder mit if isset() arbeiten.

Dem <select> kann man auch um ein "onchange="this.form.submit()"" (o.ä.) erweitern, dann ist es etwas konfortabler.

Tatsächlich würde sich hier auch anbieten extas mehr Javascript zu integrieren: ein AJAX-Request der durch das onchange des selects oder durch ein onclick der options getriggert wird. Mit dem Request werden dann die LagOrt-Artikel nachgeladen und angezeigt.


~Arano
Member: tobmes
tobmes Jun 13, 2016 at 09:14:42 (UTC)
Goto Top
OK, super vielen Dank für deine Antwort. Das werde ich mal testen.

Gruß
tobmes
Member: tobmes
tobmes Jun 13, 2016 at 18:34:18 (UTC)
Goto Top
So ich nochmal,

ich habe den Code jetzt etwas abgeändert.

protected function getViewData() {
parent::generateMenu();
echo "<form method='post' action='einlagern.php'>";  
$dropDown = $this->_database->query("SELECT DISTINCT Lagerort FROM Artikel ORDER BY Lagerort ASC");  
  echo "<select name='selLagerOrt'>";  
  echo "<option> Bitte einen Lagerort auswählen </option>";  
  while($ergebnis = $dropDown->fetch_assoc()) {
  echo "<option value='.$ergebnis'>" . htmlspecialchars($ergebnis["Lagerort"]) . "</option>";  
  echo $ergebnis;
}
echo "</select>";  
//einlagern::dropDownMenu();
        echo <<<showHTML

<!--<form method="post" action="index.php">-->  

<!--<input type="text" name="LagerOrt" id="lagerort" class="inputField" placeholder="lagerort">-->  
<input type="text" name="Artikel" id="artikel" class="inputField" placeholder="Artikel">  
<input type="text" name="Bestand" id="menge" class="inputField" placeholder="Menge">  
<input type="text" name="NameKunde" id="nameKunde" class="inputField" placeholder="Name des Kunden">  
<input type="text" name="NameBearbeiter" id="name" class="inputField" placeholder="Name oder Info">  
<input type="submit" name="speichern" id="btnSpeichern" class="formBtn"value="Neuen Artikel anlegen">  
<input type="submit" name="einlagern" value="einlagern">  
</form>
showHTML;


// if (isset($_POST["LagerOrt"])){ 
// $lagerOrt = $_POST['LagerOrt']; 
// }
if (isset($_POST["Artikel"])){  
$artikel = $_POST['Artikel'];  
}
if (isset($_POST["Bestand"])){  
$menge = $_POST['Bestand'];  
}
if (isset($_POST["NameKunde"])){  
$nameKunde = $_POST['NameKunde'];  
}
if (isset($_POST["NameBearbeiter"])){  
$name = $_POST['NameBearbeiter'];  
}

if (isset($_POST["speichern"])){  
  if (isset($_POST['selLagerOrt'])) {  
      $lagerOrt = $_POST['selLagerOrt'];  
      
    //$lagerOrt =mysql_real_escape_string($_POST[$ergebnis]);
  }
$this->lagerBuchung($lagerOrt,$artikel,$menge,$nameKunde,$name);
}

if (isset($_POST["einlagern"])){  
$this->datenSatzAendern($artikel,$menge);
}
if (isset($_POST["auslagern"])){  
$this->auslagern($artikel,$menge);
}
if (isset($_POST["anzeigen"])){  
$this->anzeigen($artikel);
}

}

Ich habe das DoprDown-Menü nicht mehr in einer extra Methode stehen, da ich damit dann garnicht mehr klar kam, würde mir aber irgendwie besser gefallen. Wenn ich alles ausfülle und etwas aus dem Dropdown-Menü auswähle und auf speichern Klicke, dann wird mir anstelle des Lagerorts einfach .Array übergeben, das verstehe ich leider nicht. Kann mir jemand erklären warum das so ist?

wieder einmal vielen Dank für die Hilfe
Member: Arano
Arano Jun 14, 2016 at 19:18:24 (UTC)
Goto Top
Hi,
habe jetzt den ersten Fehler genommen den ich gefunden habe. Wollte den Rechner nicht extra hochfahren.

s. Zeile 7+8 !
In der Schleife definierst du $ergebnis per fetch_assoc() welches dir ein assoziatives ARRAY zurückgibt.
Für das "value" des <option>s müsstest du demnach $ergebnis['Lagerort'] verwenden. Weil du allerdings nur den Variablennamen verwendest wird nur der Typ ausgegeben.

Schau mal in den Quelltext der generiertenn Seite, da solltest du an entsprechender Stelle bzw. an ALLEN Stellen <option value="Array"> finden.


~Arano