Welcome Guest, Not a member yet? Register   Sign In
Customising a helper (not taken into account) form_helper.php
#1

[eluser]Monarobase[/eluser]
Hello,

The script I'm working on stores in a database the values of an element and allows the user to change them using a drop down list.

I've placed all the values taken from the database in an object called $db to I access them with $db->name

I've got the <select> field in template in the views folder.

In order to select an item I currently have to use the following code :

Code:
&lt;?=set_select('name', 'Name1', (($db->name == 'Name1') ? TRUE : FALSE))?&gt;

And I want to either find a better way to do this or to reduce the code to :

Code:
&lt;?=set_select('critere_ppl', 'Name1', $db->name)?&gt;

So I copied the form_helper.php file from Codigniter's main helpers folder to my applications helpers folder and added some code to the set_select function to ask it to not only look for true but also to compare with the value (I added : OR $default == $value).

Original code :
Code:
if ( ! isset($_POST[$field]))
            {
                if (count($_POST) === 0 AND $default == TRUE)
                {
                    return ' selected="selected"';
                }
                return '';
            }

My code :
Code:
if ( ! isset($_POST[$field]))
            {
                if (count($_POST) === 0 AND ($default == TRUE OR $default == $value))
                {
                    return ' selected="selected"';
                }
                return '';
            }

But this does not seem to change anything, I can only get the selected="selected" if I use TRUE ...

Any ideas what I'm doing wrong ?
#2

[eluser]danmontgomery[/eluser]
A couple things...

First, you don't need to copy the entire form helper, you can just extend it by naming your file MY_form_helper.php, then when you load the form helper yours gets loaded first, and any functions you define are not re-defined by CI.

Second, I think you're using that function wrong. You only need to pass it a third parameter once, for the default value. Your code should look like:

Code:
<select name='name'>
  <option value='Name 1' &lt;?=set_select('name', 'Name 1', TRUE)?&gt;>One</option>
  <option value='Name 2' &lt;?=set_select('name', 'Name 2' )?&gt;>Two</option>
  <option value='Name 3' &lt;?=set_select('name', 'Name 3' )?&gt;>Three</option>
</select>

If you're talking about dynamically determining what the default value is, you might be better off using the form_dropdown() function and passing it the value as the 3rd parameter:

Code:
&lt;?=form_dropdown($field_name, $options, $selected_value)?&gt;
#3

[eluser]Monarobase[/eluser]
Thankyou,

I had forgotten about the MY_ extension, and yes the dropdown function should solve everything !
#4

[eluser]Monarobase[/eluser]
Hello again,

Same problem as before but with radio buttons !

I solved the select problem with the form helper (form_dropdown) but now am stuck with radio buttons.

nb. I'm working from an already existing database so I can't change my DB values.


In my database I've got "Yes" or "No" for each line, and would like to set the radio button with the database value and not TRUE or FALSE.

In my helpers folder I've created MY_form_helper.php

And have added the following :

Code:
if ( ! function_exists('set_radio'))
{
    function set_radio($field = '', $value = '', $default = FALSE)
    {
        $OBJ =& _get_validation_object();

        if ($OBJ === FALSE)
        {
            if ( ! isset($_POST[$field]))
            {
                if (count($_POST) === 0 AND ($default == TRUE OR $default == $value))
                {
                    return ' checked="checked"';
                }
                return '';
            }

            $field = $_POST[$field];
            
            if (is_array($field))
            {
                if ( ! in_array($value, $field))
                {
                    return '';
                }
            }
            else
            {
                if (($field == '' OR $value == '') OR ($field != $value))
                {
                    return '';
                }
            }

            return ' checked="checked"';
        }

        return $OBJ->set_radio($field, $value, $default);
    }
}


This is the bit I changed :

It was :
Code:
if (count($_POST) === 0 AND $default == TRUE)

I changed it to :
Code:
if (count($_POST) === 0 AND ($default == TRUE OR $default == $value))


Do you have an idea why this code doesn't work or even seem to change anything ?
#5

[eluser]Monarobase[/eluser]
I've got some news, I think I've solved the problem...

I noticed that the form_validation library also contained a function set_radio so I created a MY_Form_valadation.php in my application library folder.

I then put this in my file :

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation  {
    
    function set_radio($field = '', $value = '', $default = FALSE)
    {
        if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
        {
            if (($default === TRUE OR $default == $value) AND count($this->_field_data) === 0)
            {
                return ' checked="checked"';
            }
            return '';
        }
    
        $field = $this->_field_data[$field]['postdata'];
        
        if (is_array($field))
        {
            if ( ! in_array($value, $field))
            {
                return '';
            }
        }
        else
        {
            if (($field == '' OR $value == '') OR ($field != $value))
            {
                return '';
            }
        }
            
        return ' checked="checked"';
    }
    
}
// END Form Validation Class

/* End of file Form_validation.php */

And it seems to work ... !

Now my final question is, is there no better way to achieve the same result ?




Theme © iAndrew 2016 - Forum software by © MyBB