Neosocrates Posted January 16, 2011 Share Posted January 16, 2011 I have created code that allows a table of 'X' rows and 'Y' columns to be made (the user enters the X and Y value) with each table cell being uniquely identified through name as (X, Y). Each cell is has a value of 5 and all cells needs to be filled in before the table is submitted, which is where the problem arises. How exactly would one be able to verify that every cell has been filled and then calculate the sum of all the values entered into the cells for all those which have the same X value? The code to generate the table is as follows: echo "<td align='left'><b>$X<b></td>"; For($Y = 1; $Y <= $num; $Y++) { ?> <td align="center"><input type="text" name="<?php echo "$X,$Y";?>" value="<?php echo 5; ?>" /> </td> <?php } Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/224606-reading-table-cells/ Share on other sites More sharing options...
denno020 Posted January 16, 2011 Share Posted January 16, 2011 To verify all of the fields have been entered, you can either use javascript, which will check all of the fields before the form is sent, or you can use PHP, which will require the form to be submitted, and then the fields are checked at the server, and a new page is rendered with appropriate error messages. To calculate the sum of the fields, would be pretty easy. Just submit the form to a php script (or the same page itself, just have an if statement at the top of the page to check if data has been POSTed), and then grab the contents of each table cell (which I'm assuming has text fields in there), and add them up. Grab them from $_POST['eac_fields_name'], and add them into a variable called $total. Echo $total or do whatever you want with the total. Denno Quote Link to comment https://forums.phpfreaks.com/topic/224606-reading-table-cells/#findComment-1160243 Share on other sites More sharing options...
Neosocrates Posted January 16, 2011 Author Share Posted January 16, 2011 Thanks, Denno. How exactly would I grab each cell given the X and Y value can always differ? If the values were fixed I could do it easily enough, but as they will always change that is where the problem occurs. I submit the form using (isset($_POST['submit']))" and then initiate the variable $error as an array which will later list all the errors. $error = array(); if (empty($_POST['$X,$Y'])) { $error[] = "Empty Cell at $X,$Y"; } else { $??? = ($dbc, trim($_POST['$X,$Y'])); } if (empty($error)) { $total = ???? Where the question marks are is where I am unsure what to do. In the first instance of checking it is empty, would that code work for checking every single cell? If so, what can I set the "$???" as so each cell can continue to be uniquely identified? This problem also stems into calculating the total. Once empty, how can I add up the total a row if, for example, the X value was one? Quote Link to comment https://forums.phpfreaks.com/topic/224606-reading-table-cells/#findComment-1160246 Share on other sites More sharing options...
denno020 Posted January 16, 2011 Share Posted January 16, 2011 You could set some hidden input boxes in the form as the x and y values, and then use these values to see how many iterations need to be done. If each of the text boxes are incrementally named, then you should be able to work out how many there are, and go through getting the values for them all: Example: X=3, Y=5 (15 text fields are printed) hidden_input_X = X ( = 3) hidden_input_Y = Y ( = 5) each text fields name will be: number1, number2, number3.......number15 Form will look like this: (each number represents a text field) 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 10 | 11 | 12 13 | 14 | 15 (Form is submitted) $X = $_POST['hidden_input_X'] $Y = $_POST['hidden_input_Y'] (use for loop to go through every row in each column, or each column in every row - there will be a for loop inside a for loop) int $tracker = 1; for(int i = 0 ; i < $X ; i++){ for(int j = 0 ; j < $Y ; j++){ $total += $_POST['number'.$tracker]; $tracker++; } } echo $total; The only problem that I have above is whether the variable $tracker can be used in the way I have used it :S... How does that sound? Denno Quote Link to comment https://forums.phpfreaks.com/topic/224606-reading-table-cells/#findComment-1160247 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.