Jump to content

dynamically echo HTML table rows


pakenney38

Recommended Posts

Does anyone know how to echo HTML table rows based on a loop?
For example, I have this code that doesn't work. Assume that $addrow is set via a submit button.
With this said, rowcount should be equal to 5 on the intial page load. After the script runs, it should be equal to 10.

[code]if (isset($addrow))
{for ($x = ($rowcount + 1); $x <= ($rowcount + 5); $x++)
{echo
"
<tr>
    <td><label>
      <div align='center'>
        <input name='date$x' type='text' id='date$x' size='10' />
      </div>
    </label></td>
    <td><div align='center'>
      <label>
      <input name='fltno$x' type='text' id='fltno$x' size='5' />
      </label>
    </div></td>
    <td><div align='center'>
      <input name='actblk$x' type='text' id='actblk$x' size='5' />
    </div></td>
    <td><div align='center'>
      <input name='schblk$x' type='text' id='schblk$x' size='5' />
    </div></td>
    <td><div align='center'>
      <input name='dhcr$x' type='text' id='dhcr$x' size='8' />
    </div></td>
    <td><div align='center'>
      <input name='paycr$x' type='text' id='paycr$x' size='8' />
    </div></td>
    <td><div align='center'>
      <input name='nontax$x' type='text' id='nontax$x' size='8' />
    </div></td>
    <td><div align='center'>
      <input name='tax$x' type='text' id='tax$x' size='8' />
    </div></td>
    <td><div align='center'>
      <label>
      <input name='comments$x' type='text' id='comments$x' size='52' />
      </label>
    </div></td>
  </tr>
";
}
$rowcount = $x;
echo "<input name='rowcount' type='hidden' id='rowcount' />";}
else
{
$rowcount = 5;
echo "<input name='rowcount' type='hidden' id='rowcount' />";
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/27599-dynamically-echo-html-table-rows/
Share on other sites

Note exactly sure what you're after but see if the following helps.
[code]<?php

$rowcount = 5; //default number of rows

if (isset($addrow))
{
$rowcount = $rowcount + 5;  //add 5 rows if $addrow is set
}

for ($x = 1; $x <= $rowcount; $x++){
echo " <tr>
    <td><label>
      <div align='center'>
        <input name='date$x' type='text' id='date$x' size='10' />
      </div>
    </label></td>
    <td><div align='center'>
      <label>
      <input name='fltno$x' type='text' id='fltno$x' size='5' />
      </label>
    </div></td>
    <td><div align='center'>
      <input name='actblk$x' type='text' id='actblk$x' size='5' />
    </div></td>
    <td><div align='center'>
      <input name='schblk$x' type='text' id='schblk$x' size='5' />
    </div></td>
    <td><div align='center'>
      <input name='dhcr$x' type='text' id='dhcr$x' size='8' />
    </div></td>
    <td><div align='center'>
      <input name='paycr$x' type='text' id='paycr$x' size='8' />
    </div></td>
    <td><div align='center'>
      <input name='nontax$x' type='text' id='nontax$x' size='8' />
    </div></td>
    <td><div align='center'>
      <input name='tax$x' type='text' id='tax$x' size='8' />
    </div></td>
    <td><div align='center'>
      <label>
      <input name='comments$x' type='text' id='comments$x' size='52' />
      </label>
    </div></td>
  </tr>
";
}

//Not sure what you are trying to do which the hidden inputs
echo "<input name='rowcount' type='hidden' id='rowcount' />";

?>[/code]

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.