0

I have created a form with 3 rows, each row have 5 columns, and each column contain 3 input box as below screen shot.

enter image description here

I would like to submit this form and generating a table. The structure of the table will determine by the form's data.

For example:

  1. If I submit the form as below:

enter image description here

It will create a table like this:

enter image description here

(two rows, first row have two columns and second row have one column)

I suppose I need this write a huge of if then else code for this situation, do someone have an idea on writing this logic? Prefer to write it simple/tiny.

Welcome to discuss it.

1 Answer 1

1

This is just the algorithm, but implementing it in php should be simple enough

  1. Loop over the entire post to see how many rows have been returned, and which row has the max columns.
  2. Create a table
  3. In first row, add the number of cells you require, such that the colspan for each cell is maxCols / thisRowCols
  4. do this for all rows

The only caveat I see in this logic is if the result of maxCols / thisRowCols is not an integer, but you will have to either live with it, or think up a workaround :P

EDIT

If you are echoing the from the php,

if(thisRowCols != maxCols) {
    echo("<td colspan=\"", maxCols / thisRowCols, "\">&nbsp;</td>");
} else {
    echo("<td>&nbsp;</td>");
}

If you are having the <td>... in the html code,

<td <?php if(thisRowCols != maxCols) { echo("colspan=\"", maxCols / thisRowCols, "\"");} ?> >&nbsp;</td>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, but how can I put the colspan in to the <td> which have different cell than other row?
Edited my answer, see if it answers your query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.