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
https://forums.phpfreaks.com/topic/69900-dynamic-table/
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
https://forums.phpfreaks.com/topic/69900-dynamic-table/#findComment-351090
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.