Welcome Guest, Not a member yet? Register   Sign In
Passing a view as a variable.
#11

[eluser]ellipsis[/eluser]
[quote author="jedd" date="1245372022"]Close your eyes and learn how to [url="http://uk3.php.net/ternary"]use the ternary force[/url]. You'll never look back. But you will start looking sideways more often.[/quote]
Tried doing it your way, but I ended up taking a nap... So, the second time around I decided to keep my eyes open and read that wonderful page. Honestly, I feel like I just struck gold. :lol:
Now to go read some more about models and create that get_shit_from_db() function you so eagerly wanted to see.
#12

[eluser]Dam1an[/eluser]
Firstly, +1 for the ternary operator Smile
When I first came accross it I was like wtf! But it's so awesome now 9although don't try to replace every if/else with it, cause I've seen people try that, it just doesn't work)

Secondly, I'll step up and defend vars(), it can be very useful if like me, you prefer to use 2 level views (or more). Before I found out about it, I was nesting data arrays just so I would have them at the right level, but using vars() all my data is available to all views. Although I prefer to pass the data as a parameter to the view() function if it's just a single depth view

And ellipses, I beat you to writing the get_shit_from_db function Tongue
#13

[eluser]ellipsis[/eluser]
[quote author="Dam1an" date="1245374794"]Firstly, +1 for the ternary operator Smile
When I first came accross it I was like wtf! But it's so awesome now 9although don't try to replace every if/else with it, cause I've seen people try that, it just doesn't work)

Secondly, I'll step up and defend vars(), it can be very useful if like me, you prefer to use 2 level views (or more). Before I found out about it, I was nesting data arrays just so I would have them at the right level, but using vars() all my data is available to all views. Although I prefer to pass the data as a parameter to the view() function if it's just a single depth view

And ellipses, I beat you to writing the get_shit_from_db function Tongue[/quote]
Yes, that was also my understanding of what vars() is supposed to do, but what I did NOT understand - and maybe you can clear this up - is why when adding $fields to vars(), the variables did not get passed to the registration view, whereas if I provided $fields as a second argument to view('registration'), the variables were accessible just fine.
Also, while working on the page just now I realized that my username_check() and email_check() functions were botched. Apprently they return "username/email taken" for whatever I input. Now, that probably has something to do with the fact that I forgot the second part of the condition in the if statement, but even after modifying it it still doesn't work. Any thoughts?

Code:
// MODIFIED VERSION - see first post for the original version

function username_check($str)
    {
                
        if ($this->db->where('username', $str) != NULL)
        {
            $this->form_validation->set_message('username_check', 'The username is already taken');
            return FALSE;
        }
        elseif ($str == 'administrator' || $str == 'Greyhound' || $str == 'mihai')
        {
            $this->form_validation->set_message('username_check', 'That username is reserved');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
#14

[eluser]Dam1an[/eluser]
In the code in you're first post, the view you're refering to is called before the call to load vars, hence those vars not yet being available

Also, I just noticed, when you do load the vars, you pass in 2 arrays, thats not going to work as when you pass it to args, it expects 2 strings (key and value) (see line 339 in oader.php to see how/why it works)
If you want to pass in 2 arrays, you need to merge them using array_merge

As for the second problem, try something like
Code:
// Disclaimer: Not tested :P
function username_check($username) {
    // First we check if it's already taken
    $this->db->where('username', $username);
    if($this->db->count_all_results() > 0) return false;
    
    // Now check for reserved usernames
    $reserved = array('administrator', 'Greyhound', 'mihai');
    // if the username isn't in here, sucess, else fail
    // Need to negate the result to get what we expect
    return !in_array($username, $reserved);
}

Yours wasn't working, because the where clause returns an object, which is not null, so it passes the first condition

Hopefully my suggestion works and makes sense, if not just say so, and I'll get back to you Smile
#15

[eluser]ellipsis[/eluser]
I really thought I tried putting the vars($fields) before the if statement... oh well, it's almost 1 a.m. over here so I'll just blame it on that :lol:

As for the second part, I was just reading the db page in the user guide and tried that thing you suggested, but I get a MySQL error, unfortunately.
Quote:A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `username` = 'yuiyf'' at line 2

SELECT COUNT(*) AS `numrows` WHERE `username` = 'yuiyf'
#16

[eluser]jedd[/eluser]
I use raw SQL calls, so I'm not much good here.

Two things, though.

Turn on the [url="http://ellislab.com/codeigniter/user-guide/general/profiling.html"]profiler[/url] to see what your exact calls to the database are. It's often very enlightening in these situations.

And swap the order of your tests - an array / string search is much faster than doing a database lookup, so you should do it first.

ADDENDA - Third thing - I'd suggest you report a generic 'this account name is already in use', as a different error message ('this account is reserved') will hint to the user that the account is special in some way, and consequently a more attractive target. The user at this point (ie. when they are not known to you at all) does not need to know that there are special accounts, let alone what they are.
#17

[eluser]tonanbarbarian[/eluser]
try the following, you are not specifying which table to get the data from
Code:
$this->db->where('username', $username);
$this->db->from('users');
if($this->db->count_all_results() > 0) return false;
or i seem to remember you can specify the table in the call to count_all_results
Code:
$this->db->where('username', $username);
if($this->db->count_all_results('users') > 0) return false;
note this assumes the table is users, change that for whatever the real table name is
#18

[eluser]ellipsis[/eluser]
@tonanbarbarian - That solved it. I was really wondering how it was supposed to magically know what table to extract the info from and have been trying manual SQL queries, as per jedd's suggestion. Thanks for that and thank you jedd for the profiler hint; it's providing some pretty interesting info.

Here's the final code and it works flawlessly

Code:
//checking the username against a predefined set of rules    
    function username_check($str)
    {    
        $this->db->where('username', $str);
        if($this->db->count_all_results('users') > 0)
        {
            $this->form_validation->set_message('username_check', 'The username is already taken');
            return FALSE;
        }
        elseif ($str == 'administrator' || $str == 'Greyhound' || $str == 'mihai')
        {
            $this->form_validation->set_message('username_check', 'That username is reserved');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
        
    }

    //checking the e-mail address against a predefined set of rules
    function email_check($str)
    {
        $this->db->where('email', $str);
        if ($this->db->count_all_results('users') > 0 || $str == '[email protected]')
        {
            $this->form_validation->set_message('email_check', 'That e-mail address is already in use');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
#19

[eluser]ellipsis[/eluser]
Another day, another problem. I'm getting close to finalizing my registration form and database insertion, but I'm not quite there yet. Here's what I've come up with so far and aside from _encrypt() and add_to_db(), everything seems to be just fine. Any ideas what I'm doing wrong here?

controllers/registration.php
Code:
<?php

class Registration extends Controller {

    function Registration()
    {
        parent::Controller();    
        $this->load->model('Reg_model', "", TRUE);
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('encrypt');
        $this->output->enable_profiler(TRUE);
        function _encrypt($password)    
        {
            $toenc='bigstring'.$password.'anotherbigstring';
            $fields['shapassalt']=$this->encrypt->sha1($toenc);
        }        
    }
    
    //checking the username against a predefined set of rules    
    function username_check($str)
    {    
        $this->db->where('username', $str);
        if($this->db->count_all_results('users') > 0)
        {
            $this->form_validation->set_message('username_check', 'That username is already taken');
            return FALSE;
        }
        elseif ($str == 'administrator')
        {
            $this->form_validation->set_message('username_check', 'That username is reserved');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
        
    }    


    //checking the e-mail address against a predefined set of rules
    function email_check($str)
    {
        $this->db->where('email', $str);
        if ($this->db->count_all_results('users') > 0 || $str == '[email protected]')
        {
            $this->form_validation->set_message('email_check', 'That e-mail address is already in use');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }    


    function index()
    {


        //setting the page title
        $data=array('page_title'=>"Registration");
        
        //re-populating the forms after a registration error has occurred
        $fieldnames = array ("ln", "site", "city", "zipcode");
        foreach ($fieldnames as $fn)
        {
        $fields[$fn] = (isset ($_POST[$fn])) ?  $_POST[$fn] : NULL;
        }
                
        //setting the form rules
        $this->form_validation->set_rules('fn', 'First Name', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[20]|callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[50]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|matches[password]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
        $this->form_validation->set_rules('country', 'Country', 'required');
        
        //getting users from db
        $fields['query']=$this->db->get('users');

        $this->load->vars($fields);
        
        //running form validation and loading the appropriate page    
        if ($this->form_validation->run() == FALSE)
        {
            $data['page_content']=$this->load->view('register', "", true);
        }
        else
        {
            $this->form_validation->set_rules('fn', 'First Name', 'trim|xss_clean');
            $this->form_validation->set_rules('ln', 'Last Name', 'trim|xss_clean');
            $this->form_validation->set_rules('username', 'Username', 'trim|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|xss_clean');
            $this->form_validation->set_rules('email', 'Email', 'trim|xss_clean');
            $this->form_validation->set_rules('site', 'Website', 'trim|xss_clean');
            $this->form_validation->set_rules('country', 'Country', 'trim|xss_clean');
            $this->form_validation->set_rules('city', 'City', 'trim|xss_clean');
            $this->form_validation->set_rules('zipcode', 'Zipcode', 'trim|xss_clean');
            $this->form_validation->run();
            _encrypt($password);    
            $formdata=array('username'=>$username, 'password'=>$shapassalt, 'email'=>$email, 'first_name'=>$fn, 'last_name'=>$ln, 'website'=>$site, 'country'=>$country, 'city'=>$city, 'zipcode'=>$zipcode);
            $this->Reg_model->add_to_db($formdata);
            
            $data['page_content']=$this->load->view('regsuccess', "", true);
        }
        
        //loading the views
        $this->load->vars($data);
        $this->load->view('header');
        $this->load->view('content');
        $this->load->view('footer');
    }
        
}
/* End of file registration.php */
/* Location: ./frontend/controllers/registration.php */
?>

model/reg_model.php

Code:
<?php

class Reg_model extends Model
{
    function Reg_model()
    {
        parent::Model();
    }
    
    function add_to_db($formdata)
    {
        $this->db->insert('users', $formdata);
    }
    
}

?>

Edit: I've moved _encrypt() inside the constructor, but then I get the following errors:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: password

Filename: controllers/registration.php

Line Number: 124

Fatal error: Using $this when not in object context in C:\xampp\htdocs\site\frontend\controllers\registration.php on line 18
If I leave it outside the constructor and index(), I get
Quote:Fatal error: Call to undefined function _encrypt() in C:\xampp\htdocs\site\frontend\controllers\registration.php on line 111
#20

[eluser]Dam1an[/eluser]
First of all, the encrypt function
1) You're declaring it as a function within the constructor :-S
2) When calling a function, use $this->_encrypt instead of just _encrypt
3) It's setting the value in a local array called $fields, either make it return the encrypted value, or use a class variable for the array

As for the model, do you get any sort of errors with that? Or it just doesn't do anything?
I just noticed, you don't ever seem to actually set the variabes you use in the formdata array?




Theme © iAndrew 2016 - Forum software by © MyBB