Jump to content

Dynamic Table


proctk

Recommended Posts

Hi the below is code that is supposed to add the number of rows that is selected form the first select box. So if the number twenty is selected then that number of of rows will display.  The code works but with one BIG problem.  it works backwards. If the number 1 is selected twenty rows are displayed and if the number 20 is selected one row is selected.

 

Any ideas how to fix this thank you

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

  <select name="numItems" id="numItems" onchange="this.form.submit()">
<?php
$items = $_POST['numItems'];
foreach(array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
"16","17","18","19","20") as $value)

{
  echo "<option value='$value'";
  if($value == $items)
  {
    echo " selected='selected'";
  }
  echo ">$value</option>\n";
}
?>
  </select>
   </form>
  <table class="table">
    <?php  
if(isset($_POST['numItems'])){
$numItems = $_POST['numItems'];

while ($numItems <= 20) { ?>
<tr>
      <td>
        <select name="wholeNum[]">
          <option value="00">00</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
          <option value="7">7</option>
          <option value="8">8</option>
          <option value="9">0</option>
          <option value="10">10</option>
        </select>
        <select name="fraction[]">
          <option value="00">00</option>
          <option value="1/16">1/16</option>
          <option value="1/8">1/8</option>
          <option value="1/4">1/4</option>
          <option value="1/2">1/2</option>
          <option value="3/4">3/4</option>
        </select>
        <select name="meassurement[]" id="measurement">
          <option value="">Choose</option>
          <option value="Teaspoon(s)">Teaspoon(s)</option>
          <option value="Tablespoon(s)">Tablespoon(s)</option>
          <option value="Cup(s)">Cup(s)</option>
          <option value="Pint(s)">Pint(s)</option>
          <option value="Quart(s)">Quart(s)</option>
          <option value="Pound(s)">Pound(s)</option>
          <option value="Pintch">Pintch</option>
          <option value="Ounce">Ounce</option>
        </select>
        <input name="ingredient" type="text" />
        <?php $numItems ++; ?>        </td>
      </tr>
        <?php }}?>
        <tr><td>Preperation</td></tr>
    <tr>
     <td><textarea name="preperation[]" cols="" rows="" class="message_to"></textarea></td>
      </tr>
    <tr>
      <td> </td>
      </tr>
  </table>

Link to comment
Share on other sites

[code]first off this don't make sense

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

 

  <select name="numItems" id="numItems" onchange="this.form.submit()">

<?php

$items = $_POST['numItems'];

foreach(array("","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",

"16","17","18","19","20") as $value)

 

{

  echo "<option value='$value'";

  if($value == $items)

  {

    echo " selected='selected'";

  }

  echo ">$value</option>\n";

}

?>

[/code]

 

secondly your issue is in the logic of your while loop

 

you have it in the form while A <= B  where B is a constant and A is your user's input.  So you have B=20 and if A = to 1 it will need to loop through enough times to get to 20 (I.e 21 times)  so what I suggest you do is say  while ($i <= $user_input)  where $i is initialized at a value of 1 and $user_input is the user input. 

 

Can you explain the purpose of the foreach loop in the above?  Pre selecting/creating a selection is way easier

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.