Jump to content

Reading Table Cells


Neosocrates

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.