Jump to content


Photo

populate html table with php


  • Please log in to reply
2 replies to this topic

#1 demanon

demanon
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 July 2006 - 05:03 AM

hello all,

i'm just beginning to learn php and needed some help with a project i've been assigned.  basically, i need a way for non-technical people to be able to create/populate multiple html tables (2 columns, maybe 4-5 rows) and html is WAAAY over their heads.  I thought to begin with I'd just create the table template with dummy text and let them update the code, but they all just looked at me with blank expressions.  What I'm looking to do is setup a simple php form that will have a few text boxes and once they fill those in and click 'submit' it will display the entire pre-formatted html table code with their entries included in the appropriate place.  that way, they'll just be able to copy the entire html table code and paste it where necessary.  any help is much appreciated.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 July 2006 - 10:55 AM

SOmething like this:
<?php

if(isset($_POST['genCode']))
{
    // echo '<pre>' . print_r($_POST['tool'], true) . '</pre><br /><br />';

    echo '<h1>Generating HTML...</h1>';

    $html = <<<HTML

<table width="90%" border="1" cellpadding="7">
  <tr bgcolor="#66ccff">
    <td><big>"Problem/Error":</big> {$_POST['problem']}</td>
  </tr>

  <tr bgcolor="#ccffff">
    <td><big>"Cause":</big> {$_POST['cause']}</td>
  </tr>
  <tr>
    <td><big>"Solution":</big> {$_POST['solution']}</td>
  </tr>

  <tr bgcolor="#dddeee">
    <td><big>"Tools":</big>

      <table width="100%" border="1" cellpadding="7">

HTML;

    $i = 0;

    foreach($_POST['tool']['name'] as $tools => $tool)
    {
        $html .= <<<HTML
        <tr bgcolor="#dddeee">
          <td width="40%">"{$tool}"</td>
          <td>{$_POST['tool']['desc'][$i]}</td>
        </tr>

HTML;
        $i++;
    }

    $html .= <<<HTML

      </table>
    </td>
  </tr>
</table>
HTML;

    echo '<textarea cols="100" rows="20">' . $html . '</textarea>';
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <table cellpadding="7" cellspacing="0" border="1" width="90%">
      <tr>
          <td><big>"Problem/Error":</big> <input name="problem" type="text" /></td>
      </tr>
      <tr>
          <td><big>"Cause":</big> <input name="cause" type="text" /></td>
      </tr>
      <tr>
          <td><big>"Solution":</big> <input name="solution" type="text" /></td>
      </tr>
      <tr>
          <td colspan="2">
            <big>"Tools":</big>
            <table align="center" width="98%" cellpadding="7" cellspacing="0" border="1" style="margin: 1%;">
                <tr>
                    <td>"Tool Name"</td>
                    <td>"Tool Description"</td>
                </tr>
                <?php
                for($i=0; $i < 5; $i++)
                {
                ?>
                <tr>
                    <td width="40%"><input name="tool[name][]" type="text" size="50" /></td>
                    <td><input name="tool[desc][]" type="text" size="80" /></td>
                </tr>
                <?php
                }
                ?>
            </table>
          </td>
      </tr>
      <tr><td colspan="2"><input type="submit" name="genCode" value="Generate HTML" /></td></tr>
  </table>
</form>
<?php
}
?>


#3 demanon

demanon
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 July 2006 - 05:22 PM

awesome!  thank you so much.  now that i see how it's done it will be easier for me to understand how it works.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users