Welcome Guest, Not a member yet? Register   Sign In
$field->max_length not working correctly?
#1

[eluser]djstu2010[/eluser]
Hi. I am trying to write some code to determine the maximum length of a field.

I am using $field->max_length as instructed by
http://ellislab.com/codeigniter/user-gui...ields.html

However, this seems to be reporting the length of the field value, i.e. the number of characters in the field value, rather than the maximum length of the field, as suggested by the manual.

I am using MySQL.

Does the feature work correctly?

The context of the code is that I am trying to write a helper function to display a generic data form, based on the control type of each field in a given table or query result.
#2

[eluser]LuckyFella73[/eluser]
This function relies on the php function
Code:
mysql_fetch_field()

which returns the value of the longest entry in that column
and not the defined max value defined when setting up the table.

So the feature is working correct - maybe the user guide could
be a bit more detailed in this point.
#3

[eluser]djstu2010[/eluser]
OK. Can anyone suggest how to modify the CodeIgniter code to produce the desired result?

Or another way of doing it (my function is in a helper file and based on a query result, not a table name, as it may be combining data from multiple tables).
#4

[eluser]djstu2010[/eluser]
Managed to fix it myself, by making these modifications in mysql_result.php

Code:
function field_data()
    {
        $retval = array();
        $index=0;
        while ($field = mysql_fetch_field($this->result_id))
        {    
            // code to fix field length problem
            $index=$index+1;
            $len=mysql_field_len($this->result_id,$index);
            
            $F            = new stdClass();
            $F->name         = $field->name;
            $F->type         = $field->type;
            $F->default        = $field->def;
            //$F->max_length    = $field->max_length;
            $F->max_length  = $len;
            $F->primary_key = $field->primary_key;
            
            $retval[] = $F;
        }
        return $retval;
    }
#5

[eluser]Julia Yan[/eluser]
Thanks djstu2010, I got *_^
I add one line into file at system\database\drivers\mysql\mysql_result.php

line 95 $F->len = mysql_field_len($this->result_id, $index++);

But Message: Undefined variable: index

Filename: mysql/mysql_result.php

Line Number: 95

-------------------------new question, how to get single value from field_data($table);
exam, get length of field = 'name' without foreach

$fields = $this->db->field_data($table);

echo $field->len['name'];//does not work?
#6

[eluser]Unknown[/eluser]
Maybe so:
$query = $this->db->query("SELECT `name` FROM $table");
$field = field_data($query);




Theme © iAndrew 2016 - Forum software by © MyBB