Welcome Guest, Not a member yet? Register   Sign In
One controller to load the same form (empty or populated)
#1

1. I'd like to create one controller to add or edit article and load only one view (empty form or populated form)

The code I've attached works and it's close to what I want but it has two different views with the same form and two controllers for add and edit data.

2. What happens when user sends the form? Let's say we have field with name="username" value="some_value"
How can I retrieve some_value in view file? Is it a variable or an array?



Can you help please?

Attached Files
.php   Article.php (Size: 2.8 KB / Downloads: 39)
.php   Article_model.php (Size: 1.12 KB / Downloads: 29)
.php   add.php (Size: 555 bytes / Downloads: 28)
.php   edit.php (Size: 917 bytes / Downloads: 56)
.php   index.php (Size: 510 bytes / Downloads: 36)
Reply
#2

You need to add an edit switch to your form_validation stuff, this is only one way of doing it.

PHP Code:
// Register Method old code.

    /**
     * register ()
     * --------------------------------------------------------------------
     *
     * Register a user and redirect them to the success page
     *
     * @param bool   - whether or not the user should be logged in once account is created, used in admin panel
     * @param bool   - whether or not the user is simply being edited, used in admin panel
     * @param string - the user ID to be edited, used in the admin panel
     */
    
public function register($login true$edit false$id null)
    {
        if (
$edit === true)
        {
            
$this->CI->form_validation->set_rules('email''Email''trim|required|valid_email|callback_reg_email_check');
        }
        else
        {
            
$this->CI->form_validation->set_rules('username''Username''trim|required|min_length[4]|max_length[40]|callback_reg_username_check');
            
$this->CI->form_validation->set_rules('password''Password''trim|required|min_length[4]|max_length[12]|matches[conf_password]');
            
$this->CI->form_validation->set_rules('conf_password''Password confirmation''trim|required|min_length[4]|max_length[12]|matches[password]');
            
$this->CI->form_validation->set_rules('email''Email''trim|required|valid_email|callback_reg_email_check');
        }

        if (
$this->CI->form_validation->run() == false)
        {
            if (
$edit === true)
            {
                
$query  $this->CI->db->query("SELECT * FROM `$this->userTable` WHERE `id` = '$id'");

                
$result $query->result_array();

                
$this->view('register'$result[0]);
            }
            else
            {
                
$this->view('register');
            }
        }
        else
        {
            
$userName set_value('username');
            
$password $this->_salt(set_value('password'));
            
$email    set_value('email');

            if (
$edit === true)
            {
                
$this->CI->db->query("UPDATE `$this->userTable` SET `email` = '$email' WHERE `id` = '$id'");
                
$data2['msg'] = "The user has now been edited.";
            }
            else
            {
                
$this->CI->db->query("INSERT INTO `$this->userTable` (user_name, email, password) VALUES ('$userName', '$email', '$password')");
                
$data2['msg'] = "The user has now been created.";
            }

            if (
$login === true)
            {
                
$data2['msg'] = "The user has been created, you have now been logged in.";

                
$userdata $this->CI->db->query("SELECT * FROM `users` WHERE `user_name` = '$userName'");

                
$row $userdata->row_array();

                
$data = array(
                    
'userName' => $userName,
                    
'userId'   => $row['id'],
                    
'groupId'  => $row['group_id'],
                    
'email'    => $row['email'],
                    
'loggedIn' => true
                
);

                
$this->CI->session->set_userdata($data);

                if (
$this->config['auth_remember'] === true)
                {
                    
$this->_generate();
                }
            }

            
$this->view('reg_success'$data2);
        }
    }

// And in the View.

<div id="login">
    
    <?
php if(empty($username)) { ?>
    <h2>Register</h2>
    <?php } else { ?>
    <h2>Update</h2>
    <?php ?>
    
    <div class="box">
            <form method="post">
            <?php if(empty($username)) { ?>
            Username:<br />
            <input type="text" name="username" size="50" class="form" value="<?php echo set_value('username'); ?>" /><br /><?php echo form_error('username'); ?><br />
            Password:<br />
            <input type="password" name="password" size="50" class="form" value="<?php echo set_value('password'); ?>" /><?php echo form_error('password'); ?><br /><br />
            Password confirmation:<br />
            <input type="password" name="conf_password" size="50" class="form" value="<?php echo set_value('conf_password'); ?>" /><?php echo form_error('conf_password'); ?><br /><br />
            <?php ?>
            Email:<br />
            <?php if(empty($username)){ ?>
                <input type="text" name="email" size="50" class="form" value="<?php echo set_value('email'); ?>" /><?php echo form_error('email'); ?><br /><br />
            <?php }else{ ?>
            <input type="text" name="email" size="50" class="form" value="<?php echo set_value('email'$email); ?>" /><?php echo form_error('email'); ?><br /><br />
            
            <?php } if(empty($username)) { ?>
            <input type="submit" value="Register" name="register" />
            <?php } else { ?>
            <input type="submit" value="Update" name="register" />
            <?php ?>
            </form>
    </div>
</div> 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB