Results 1 to 6 of 6
  1. #1
    Join Date
    May 2002
    Location
    Deltona, FL
    Posts
    9

    Errors in Dynamic Field Input Function

    Trying to build a *somewhat* dynamic interface to post/update information into mysql dbs. Problem is, my function doesn't actually output anything, but I am too new to programming and php in general to know why... Anyone out there see what's wrong, or wants to take a crack at it?

    Thanks


    <HTML>
    <BODY>
    <?
    $link = mysql_connect("localhost", "******", "*******") or die("Could not connect");
    mysql_select_db("devweb");
    $databasename = 'devweb';
    $tablename = 'content';
    $result = mysql_query("SELECT * FROM content");
    $fields = mysql_num_fields($result);
    $rows = mysql_num_rows($result);
    $i = 0;
    $table = mysql_field_table($result, $i);


    $fields = mysql_list_fields($databasename, $tablename, $link);
    $type = mysql_field_type($result, $i);

    $columns = mysql_num_fields($fields);
    echo "Table: ".$table." table has ".$fields." fields and ".$rows." records <br />";
    echo "Below are the details...<br />";
    print "<table width=100% border=0 align=left>" . "\n";
    print "<tr>" . "\n";
    print "<td width=30%>" . "\n";
    print "<h3><b><u>Update Fields</u></b></h3>" . "\n";
    print "</td>" . "\n";
    print "</tr>" . "\n";
    print "<tr>" . "\n";
    print "<td><u>" . "\n";
    print "Field Name" . "\n";
    print "</u></td>" . "\n";
    print "<td><u>" . "\n";
    print "Information" . "\n";
    print "</u></td>" . "\n";
    print "</tr>" . "\n";
    for ($i = 0; $i < $columns; $i++)
    {
    $fieldname = mysql_field_name($fields, $i);
    $fieldtype = mysql_field_type($result, $i);
    $fieldlength = mysql_field_len($result, $i);
    print "<tr>" . "\n";
    print "<td width=30%>" . "\n";
    echo $fieldname . "\n";
    print "<td>" . "\n";
    echo $fieldtype . "\n";
    $input = GetInputType($fieldname, $fieldtype, $fieldlength);
    echo $input;
    print "</tr>" . "\n";
    print "</td>" . "\n";
    }
    print "</table>" . "\n";
    ?>
    </body>
    </HTML>
    <?
    function GetInputType ($fieldname, $fieldtype, $fieldlength)
    {
    switch ($i) {
    case 'str':
    $return = "<INPUT TYPE=TEXT NAME=" . $fieldname . " SIZE=" . $fieldlength . " MAXLENGTH=" . $fieldlength . ">";
    break;
    case 'int':
    $return = "<INPUT TYPE=TEXT NAME=" . $fieldname . " SIZE=10 MAXLENGTH=50>";
    break;
    case 'blob':
    $return = "<textarea name=" . $fieldname . " rows=8 cols=100></textarea>\n";
    break;
    }
    return $return;
    }
    ?>

  2. #2
    Join Date
    May 2002
    Posts
    7
    Doesn't look like you're calling your function anywhere, just defining it.

    To output the return value from the function:

    $outPut = functionName();
    echo($outPut);

  3. #3
    Join Date
    May 2002
    Posts
    7
    Ooops, yes you are, sorry

  4. #4
    Join Date
    May 2002
    Location
    Deltona, FL
    Posts
    9

    Problem Corrected (oops)

    Found the problem.

    In the GetFieldType() function, the switch statement

    switch($i)

    is using $i instead of $fieldtype. A wonderfully simple, yet agonizing copy & paste error.

    oops
    Patrick Hartnett
    http://www.e-dbapps.com
    patrick@e-dbapps.com

  5. #5
    Join Date
    May 2002
    Posts
    13
    To make your code more portable, you should define your function at the beginning of the code, too, before they are called.

    Plus it's just good programming practice..

    ---John Holmes...

  6. #6
    Join Date
    May 2002
    Location
    Deltona, FL
    Posts
    9

    Thanks for the tip

    Will definitely take that tip. Thought about that last night after pouring over some other code, and realized how fun it can be to discern between the functions and the code of the page.

    Is there a site anywhere laying out some php-programming guidelines/formatting rules that the community as a whole should or does follow? Didn't really see too much on php.net as far as code formatting or documentation preferences go.
    Patrick Hartnett
    http://www.e-dbapps.com
    patrick@e-dbapps.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •