0

I am opening a file (formatted in rows and columns) with PHP, looping through the rows on the file and echoing out rows into a table that meet a certain criteria. On each row that I echo out I am wrapping it in form tags. Ultimately, if I echo out 20 rows I will echo out 20 forms, each of them having a button. I am also echoing out a column with a comments box. I want the end user to be able to go to this page, enter comments on each row, and then hit the submit button to store the results of the row into a database. I have 2 problems that I do not know how to overcome.

1.) When echoing out the results to the table I do not create a static variable to reference when I go to create my SQL insert statement. How do I label each piece of data so I can call it later?

2.) There are going to be multiple forms, and the user will only be using one at a time. How do I ensure that when the user clicks submit, it only submits the data fields from the same row?

Hopefully this makes sense. If not Im happy to add as much clarification as needed. Im definitely a newbie when it comes to PHP, so I'm certain this is a poor design but Im on a tight timeline and I just need a working product for now. Next week I can go back and perhaps implement a better solution I'm sure one you kind people will suggest:)

<?php 

if (($handle = fopen("name of file to open here", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
    if ($data[4] == $login){
        $num = count($data);
                echo "<form action='analyzer.php' method='post'>";
                echo "<tr>";
                echo "<td>"; 
                echo $data[1];
                echo "</td>";
                echo "<td>";
                echo $data[2];
                echo "</td>";
                echo "<td>";
                echo $data[3];
                echo "</td>";
                echo "<td>";
                echo $data[4];
                echo "</td>";
                echo "<td>";
                echo $data[5];
                echo "</td>";
                echo "<td>";
                echo $data[6];
                echo "</td>"; 
                echo "<td>"; 
                echo $data[7];
                echo "</td>";
                echo "<td>";
                echo $data[8];
                echo "</td>"; 
                echo "<td>";    
                echo "<input type='text' name='comments' />";
                echo "</td>"; 
                echo "<td>"; 
                echo "<input class='mybutton' type='submit'         name='#' value='Submit' />";    
                echo "</td>"; 
                echo "</tr>";
                echo "</form>";


}
fclose($handle);
}
}

?>

2 Answers 2

1

1.) When echoing out the results to the table I do not create a static variable to reference when I go to create my SQL insert statement. How do I label each piece of data so I can call it later?

You need some way of uniquely identifying the piece of data. You can do this using hidden input variables.

2.) There are going to be multiple forms, and the user will only be using one at a time. How do I ensure that when the user clicks submit, it only submits the data fields from the same row?

You can name the forms or assign them a sequence in an array. For example:

<input type="submit" name="form[5]" />

You can also simplify your code by using a loop like this:

foreach( $data as $key=>$val ) {
    echo "<td>$val</td>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, cant believe I didnt think about that! That will work perfectly. Also, thanks for the side tip!
0

I know there is already an accepted answer here, but I feel like suggesting another way of doing this. Having a bunch of forms on one page each with their own submit button and requiring a page load every time is kind of cumbersome from a UI perspective.

My suggestion is that you start the long, painful, and rewarding process of learning Ajax. I have been learning jQuery AJAX and I use it to do similar things.

Example:

I have a table with 30 rows, each of which represent a process. Each row has a checkbox. I have a submit button down at the bottom, where a user can submit all the rows with checked boxes.

But, I also have buttons in each row (right next to the checkboxes actually). The buttons are simply labeled "now", and when you click them, that row is processed in the background without a page reload.

So I have allowed users to submit many rows with the normal form submit button if they want, OR to submit any individual row and not have to wait for the page to reload.

I can perhaps add some code here if anyone is ever interested.

Comments

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.