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