Input form in each cell of a table

Hi,
I have a table which displays values from a table.
But I would like to be able to directly edit the table.
Basically I want each cell of the table to be a form.
Then one submit button would post all the values to the DB.
I have attempted this but know that it is incorrect as the I have no way to distinguish the values in the form in each cell.
Here is my index.php excerpt

if (isset($_GET['courses']))
{
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
// the following try is probably not correct
  try
  {
    $sql = 'SELECT id, course FROM courses WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->bindValue(':course', $_POST['course']);
    $s->execute();
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching course.';
    include 'error.html.php';
    exit();
  }
  $row = $s->fetch();

  $pageTitle = 'Manage courses';
  $action = 'editcourse';
  $text = $row['course'];
  $id = $row['id'];
  $button = 'Update course';

  // Build the list of courses
  try
  {
    $result = $pdo->query('SELECT id, course FROM courses');
  }
  catch (PDOException $e)
  {
    $error = 'Error fetching list of courses.';
    include 'error.html.php';
    exit();
  }

  foreach ($result as $row)
  {
    $courses[] = array('id' => $row['id'], 'course' => $row['course']);
  }

  include 'managecourses.html.php';
  exit();
}

The form then is,

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php'; ?>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Manage Courses</title>
  </head>
  <body>
    <h1>Manage Courses</h1>
     <table>
     <tr>
    <th></th>
    <th>Courses</th>
    </tr>
      <?php foreach (array_reverse($courses) as $course): ?>
        <li>
          <form action="" method="post">
            <tr>
              <input type="hidden" name="id" value="<?php
                  echo $course['id']; ?>">
              <td><input type="submit" name="action" value="Delete"></td>
       		<td><input type="text" name="name" id="name" value="<?php htmlout($course['course']); ?>"></td>
            </tr>
          </form>
        </li>
      <?php endforeach; ?>
    </table>
    <p><a href="..">Return to physCMS home</a></p>
    <?php include '../logout.inc.html.php'; ?>
  </body>
</html>

I have not yet attempted to write the next part which sends the values in the table to the DB.
Any help would be greatly appreciated,
Thanks,
Shane

I know you probably don’t want to rewrite your code, but I’ll give you some food for thought. I’m in a process of writing a graphical calendar using tables, but I wanted something flexible that in case I decide to use <div> or some other HTML tag method in the distant future. So I’m thought I would put an anchor tag into each cell that I want to be able to add some data to into…

Here’s a few snippets of my script:

This portion figures out the current month:

    /* Since we know the last location of previous month's day location in the week, */
    /* we don't have to figure out the location for the current month, we just have  */
    /* to add it on to the existing array.                                           */
    for ($this->x = 1; $this->x <= self::daysInMonth($date); $this->x++) {

      /* Put the Calendar date in the 0000-00-00 format */
      self::$urldate = self::bookingDate(self::displayMonth($date) . ' ' . $this->x . ', ' . self::displayYear($date));
      /* Grab the day of the week (Example: Sunday) */
      self::$checkDayOfWeek = self::displayDay(self::$urldate);
      /* If the day is Saturday or Sunday don't give it a link */
      if (self::$checkDayOfWeek == 'Sunday' || self::$checkDayOfWeek == 'Saturday') {
        //$this->monthyCalendar[] =  '<td class="regText">' . $this->x . '</td>';
        $this->monthyCalendar[] = '<td><a class="calText" href="bookingCalendar.php?app=' . self::$urldate . '">' . $this->x . '</a></td>';
      } else {
        $this->monthyCalendar[] = '<td><a class="calText" href="bookingCalendar.php?app=' . self::$urldate . '">' . $this->x . '</a></td>';
      }
    } // End of Current Calendar Days:

This puts all the date into a two-dimensional array (I find working with two-dimensional versus one-dimensional arrays easier).

    /* Create a while loop in order to create the two-dimensional array */
    while ($this->num < sizeof($this->monthyCalendar)) {

      /* Values being put into the two-dimensional array */
      $this->displayCalendar[$this->b][$this->c] = $this->monthyCalendar[$this->num];

      $this->num += 1; // Increment this variable by one to avoid infinite loop.
      $this->c += 1; // Inside portion of the two dimension array

      /* Check to see if number can be divided by 7 (seven days in a week) */
      if (( $this->num % 7 ) == 0) {
        $this->b += 1; // Increment the outer portion of the array by one to get the rows of weeks.
        $this->c = 0; // Reset back to zero (Remember Arrays are zero based!)
      }
    }

Finally this generates the form in order to be outputted:

    $this->tabulateForm($date); // Figure out the month being displayed:

    $this->theForm = '<article class="myGreatCalendar">' . "\
";
    $this->theForm .= '<table class="tableStyle">' . "\
";
    $this->theForm .= '<thead>' . "\
";
    $this->theForm .= '<tr>' . "\
";

    /* Output the Month and Year */
    $this->theForm .= '<th colspan="7" class="tableHeading">' . self::displayMonth($date) . ' ' . self::displayYear($date) . '</th>' . "\
";
    $this->theForm .= '</tr>' . "\
";
    $this->theForm .= '<tr>' . "\
";

    /* This just uses an array that spits out the Days of the Week in the heading */
    for ($d = 0; $d <= 6; $d++) {
      $this->theForm .= '<th class="headingText dayHeading">' . self::$day[$d] . '</th>' . "\
";
    }

    $this->theForm .= '</tr>' . "\
";
    $this->theForm .= '</thead>' . "\
";
    $this->theForm .= '<tbody>' . "\
";

    /* This creates the calendar base using the two-dimensional array */
    foreach ($this->displayCalendar as $innerCalendar) {
      $this->theForm .= '<tr>' . "\
";
      foreach ($innerCalendar as $value) {
        $this->theForm .= $value . "\
";
      }
      $this->theForm .= '</tr>' . "\
";
    }

    $this->theForm .= '</tbody>' . "\
";
    $this->theForm .= '</table>' . "\
";
    $this->theForm .= '</article>' . "\
";

Sorry for the OOP, but the important thing that I wanted to point out is the first snippet of code, notice where I put the anchor tags. I put the <a> tag inside the <td></td> tags.

I know what your thinking, well the anchor tag is only going to be the size of the word (in this case a couple of numbers), but that can be easily fixed with some CSS magic. :wink:

  .calText {
    @include box-sizing(border-box);
    display: inline-block;
    overflow:auto;
    width: 100%;
    height: 100%;
    text-decoration: none;
    color: $color-200;
    background-color: rgba( $color-1100, 0.4 );
    font-weight: bold;
    &:hover {
      color: $color-100;
      background-color: rgba( $color-600, 0.8 );
    }
    padding: 3px 5px;
  }

You can keep track of each individual cell with a unique number (In my case that would be the Day of the Month for that particular Year). That way would could check the database table to see if any data needs to be added, edited or deleted. I personally haven’t gotten around to do this myself, but I will for this is a work in progress.I just thought I throw this out there for something for you to think about. In my opinion it beats dealing with sessions at this stage, where this can be confusing as it is. I know for I have been fighting this logic myself. :smiley:

Hi Strider64,
Thanks for your reply. OOP in PHP is a bit beyond me at the moment as you rightly guessed.
I see very generally what you are doing, unfortunately I don’t have enough knowledge to implement it.
But your code is interesting and thank you for posting.
I will go through it again and see if i can get a little more from it.
Thanks,
Shane