Welcome Guest, Not a member yet? Register   Sign In
Validation And Data from Database
#1

[eluser]BaRzO[/eluser]
Hi all,
There is an example for validation in user guide but if i want to include some
variables from the database i am having problem !!! how do you resolve this?
For example i want to add user groups from the database

Controller
Code:
<?php

class Form extends Controller {
    
function index()
{
    $this->load->helper(array('form', 'url'));
    
    $this->load->library('validation');
        
    $rules['username']    = "required";
    $rules['password']    = "required";
    $rules['passconf']    = "required";
    $rules['email']        = "required";
    $rules['groups']        = "required";
    
    $this->validation->set_rules($rules);
    
    $fields['username']    = 'Username';
    $fields['password']    = 'Password';
    $fields['passconf']    = 'Password Confirmation';
    $fields['email']    = 'Email Address';
    $fields['groups']    = 'User Groups';

    $this->validation->set_fields($fields);
        
    if ($this->validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
    }

}
?>

myform.php view file
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo $this->validation->error_string; ?>

<?php echo form_open('form'); ?>

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

<h5>Groups</h5>
&lt;?php echo $this->validation->groups_error; ?&gt;
<select name="groups">
<option value="&lt;?=$groups['value']?&gt;">&lt;?=$groups['name']?&gt;</option>
</select>

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

<h5>Username</h5>
&lt;?php echo $this->validation->username_error; ?&gt;
&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation->username;?&gt;" size="50" />

<h5>Password</h5>
&lt;?php echo $this->validation->password_error; ?&gt;
&lt;input type="text" name="password" value="&lt;?php echo $this-&gt;validation->password;?&gt;" size="50" />

<h5>Password Confirm</h5>
&lt;?php echo $this->validation->passconf_error; ?&gt;
&lt;input type="text" name="passconf" value="&lt;?php echo $this-&gt;validation->passconf;?&gt;" size="50" />

<h5>Email Address</h5>
&lt;?php echo $this->validation->email_error; ?&gt;
&lt;input type="text" name="email" value="&lt;?php echo $this-&gt;validation->email;?&gt;" size="50" />

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]Scriptor[/eluser]
Have you read about using models or fetching data from the database? Read the page on 'Models' in the User Guide first, followed by the Database pages. Also, you will have to pass the data to the view:

Code:
$view_data = array(
  'group' = //Code to fetch group goes here
);
$this->load->view('yourview', $view_data);

$group then becomes a variable in your view.
#3

[eluser]BaRzO[/eluser]
Thanks Scriptor, i am little confused thanks for your help Wink




Theme © iAndrew 2016 - Forum software by © MyBB