[Solved]How to create dynamic name for text box PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VMthinker
Forum Newbie
Posts: 6
Joined: Sun Sep 12, 2010 8:50 pm

[Solved]How to create dynamic name for text box PHP?

Post by VMthinker »

Hi guys! I have a question regarding the creation of Auto Dynamic names for the text boxes within PHP. The scenario is like this:

* I have 2 tables in a MySWL database.
* The 2 tables (students/teachers) have different number of fields
* Teachers = 8 fields / Students = 5 fields
* A page for inserting new data into the tables is created.
* The text box appears but without any field names beside the text box.

So now there is a need to allow the "insert" page to auto generate the Field names found within the table to allow the naming of the various text box.

I think that by including the "mysql_fetch_field" function, it can be used to auto generate the field names needed for the PHP page.

Please kindly give some suggestions. Thanks!

Here are my codes:

Code: Select all

<?php

include "db_connect.php";

$name = mysql_query("SELECT * from users");

$property = mysql_fetch_field($result);

$result = mysql_query("SHOW COLUMNS FROM users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
    echo "$property: <input type=\"text\" name=\"{$row[0]}\" size=\"40\"
    maxlength=\"256\" /><br>";
    }
}
?>
Last edited by VMthinker on Wed Sep 15, 2010 4:01 am, edited 1 time in total.
VMthinker
Forum Newbie
Posts: 6
Joined: Sun Sep 12, 2010 8:50 pm

Re: How to create dynamic name for text box PHP?

Post by VMthinker »

Looks like no one could answer the question. Anyway here's the code for those having the same PHP problem.

Code: Select all

<?php

include "db_connect.php";

$name = mysql_query("SELECT * from checkusers");

$property = mysql_fetch_field($name);

$result = mysql_query("SHOW COLUMNS FROM checkusers");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
        for ($i = 0; $i < mysql_num_fields($name); $i++)
        {
          $meta=mysql_field_name($name, $i);
          echo "$meta: <input type=\"text\" name=\"{$row['health_id']}\" size=\"40\" maxlength=\"256\" /><br>";
        }
        //echo "$new: <input type=\"text\" name=\"{$row[0]}\" size=\"40\" maxlength=\"256\" /><br>";
?>
Post Reply