Jump to content

sql in php form


andrej13

Recommended Posts

As you can see, my array contains drinks  $dranken = array("cola", "fanta", "bier", "koffie", "thee");

but how can I call the drinks up from an sql table instead of typing them in like I did now?

<?php if (!isset($_POST['submit'])) { ?>

    <html>

        <form method="post" action="<?php echo $PHP_SELF; ?>">

        <?php
        $dranken = array("cola", "fanta", "bier", "koffie", "thee");
        $prijzen = array("2", "2", "1.80", "2.20", "2.20");


        $i = 0;

        echo "<table>";
        while ($dranken[$i]) {
            $listnaam = $dranken[$i] . "_aantal";
            $optionlist = "<select name= '$listnaam'><option>0</option><option>1</option><option>2</option><option>3</option></select>";

            echo "<tr><td >" . $dranken[$i] . "</td>";
            echo "<td>" . $optionlist . "</td>";
            echo "<td>" . $prijzen[$i] . "</td></tr>";

            $i++;
        }

        echo "</table>";
        ?>

        <input type="submit" value="Toon Output" name="submit"/>

    </form>

    <?php
    }
    
    $dranken = array("cola", "fanta", "bier", "koffie", "thee");
    $prijzen = array("2", "2", "1.80", "2.20", "2.20");
    
    $i = 0;
    $totaalPrijs = 0;
    while ($dranken[$i]) {
        $aantal = $_POST[$dranken[$i] . "_aantal"];
        if ($aantal > 0) {
            $prijsperDrank = $aantal * $prijzen[$i];
            echo $dranken[$i] . ":" . $aantal . "Prijs:" . $prijsperDrank . "</br>";
            $totaalPrijs += $prijsperDrank;
            echo "totaal: $totaalPrijs";
        }
        $i++;
    }
    ?>

</html>

 

Link to comment
Share on other sites

try this

 

<?php

 

// connect to your database

 

$DrinkArray=array() ;

 

$DrinkResult=mysql_query("select flavour from drinks ")or die(mysql_error());

 

while($DrinkRow=mysql_fetch_assoc($DrinkResult)){

$DrinkArray[]=$DrinkRow[flavour];

}

 

//check your output with something like this

foreach($DrinkArray as $value){

echo $value.'<br />';

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.