Jump to content

option list


andrej13

Recommended Posts

Hey guys, i have a small problemo. As you can see i have made an option list from 0 to 3.

 

How can I put this optionlist in a for loop so I can save some place? I have found an example but I dont know how to integrate it in my php

Example

for( $i = 0; $i < 4; $i++ ) {
	$optionlist .= "<option value=\"$i\">$i</option>\n";
}
$optionlist .= "</select>\n";

 

        <?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>" . $prijzen[$i] . "</td>";
		echo "<td>" . $optionlist . "</td></tr>";

            $i++;
        }

        echo "</table>";
        ?>

 

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/230053-option-list/
Share on other sites

I think this is what you want. I also combined the two arrays into one, so a foreach loop can be used:

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

foreach ($dranken as $d => $p) {
$optionlist = "<select name='{$d}_aantal'>";
for  ($i=0;$i<4;++$i) {
	$optionlist .= "<option value='$i'>$i</option>";
}
$optionlist .= "</select>";
echo "<tr><td>$d</td>\n<td>$p</td>\n";
echo "<td>$optionlist</td></tr>\n";
}
echo "</table>";
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/230053-option-list/#findComment-1184835
Share on other sites

Archived

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

×
×
  • 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.