简体   繁体   中英

display same form multiple times in PHP

I have a table in database in which there is a column named roll_no, there are other four columns in the table. I am having a form to insert data into this table. I want to display this form x number of times, where x is number of distinct values of roll_no. That means if roll_no have 50 distinct values then the form will be displayed for 50 times on the same page. And in the form the roll_no field should be filled automatically with the value fetched from the database. The other 4 fields are start_date, end_date, total_lectures, attended_lectures. suppose start_date = 7th and end_date = 20 so there are total 13 lectures and out of which roll_no 1 attended 10 lectures and roll_no 2 attended 7 lectures and so on. The teachers fills the data like this in the form and then submit the form and data gets updated in the table. the combination of roll_no, start_date and end_date together serves as primary key.

If you have used phpmyadmin then you know that when we click on insert it shows two different forms to insert data. I also want to accomplish something like this but the number of forms will be about 60 maximum.

I can't for the life of me figure out what you're trying to do, but if I understand you correctly, you want to print something as often as there are distinct values in the database.

(Pardon me, but I still use the old-school MySQL functions so if you're using the newer ones you might need to change this somewhat.)

$distinct_query = mysql_query("
    SELECT DISTINCT row_no
    FROM table_name
");
for ($i = 0; $i < mysql_num_rows($distinct_query); $i++) {
    ?>
    All the stuff you want to print, where <?php print $i; ?> is the iteration of your form.<br />
    <?php
}

Please note that this code is untested.

You could simply add a unique number to each form element as you ouput each sub-form

1. <input type="text" name="roll_no_1" ... />
   <input type="text" name="start_date_1" ... />
   etc...
2. <input type="text" name="roll_no_2" ... />
   <input type="text" name="start_date_2" ... />
   etc...
...
50. <input type="text" name="roll_no_50" ... />
   <input type="text" name="start_date_50" ... />
    etc...

or, if you trust the users to not mangle the form, you can use PHP's array syntax for form fields:

1. <input type="text" name="roll_no[]" ... />
   <input type="text" name="start_date[]" ... />
2. <input type="text" name="roll_no[]" ... />
   <input type="text" name="start_date_[]" ... />
...
50. <input type="text" name="roll_no[]" ... />
    <input type="text" name="start_date[]" ... />

Either way works. The first has the benefits of guaranteeing that all the related fields have the same "subscript" identification within the name. The other one makes it simpler to simply do foreach ($_POST['roll_no'] as $idx => $val) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM