Welcome Guest, Not a member yet? Register   Sign In
  Best practise for $_POST and Ajax CRUD
Posted by: El Forum - 07-05-2008, 06:50 AM - Replies (5)

[eluser]CARP[/eluser]
Hi guys

1) I've a form, inside a view, and I'd like if my logic is going the best and quickest way possible

some_controller.php

Code:
if ($this->input->post('some_field')) {
    $this->load->model('model_person');
    $this->model_person->add($field1, $field2, $field3, ...);
}
else {
    $this->load->view('form_add_person');
}

What if my form has 25 fields? Could I send only the $_POST array through the model function's parameter? or is there a better way?

2) I want to reduce files, views, controllers. Did any1 try an easy way to do ajax with CI? I've been able to delete a record using jQuery ajax load function, and would like to know if there're other (easier) ways to try

Thanks a lot in advance,


  Modules in CI
Posted by: El Forum - 07-05-2008, 06:19 AM - Replies (1)

[eluser]kertz[/eluser]
CI does not support modules? Or is there any plugin that would help me to do it?


  Library with layered structure
Posted by: El Forum - 07-05-2008, 04:58 AM - No Replies

[eluser]kunitsuji From Japan[/eluser]
//MY_Loader extends CI_Loader

Code:
/*
     * <code>
     * $this->load->library('test/hoge/testscript');
     * $this->test_hoge_testscript->METHODNAME()
     * //filename = Testscript.php
     * //clasename = Test_hoge_testscript
     * </code>
     */
    public function library($library = '', $params = NULL)
    {
        if ($library == '')
        {
            return FALSE;
        }

        if (is_array($library))
        {
            foreach ($library as $class)
            {
                if (strpos($class, '/') !== FALSE)
                {
                    $this->_ci_load_class_my($class, $params);
                }
                else
                {
                    $this->_ci_load_class($class, $params);
                }
            }
        }
        else
        {
            if (strpos($library, '/') !== FALSE)
            {
                $this->_ci_load_class_my($library, $params);
            }
            else
            {
                $this->_ci_load_class($library, $params);
            }
        }

        $this->_ci_assign_to_models();
    }

    public function _ci_load_class_my($class, $params = NULL)
    {
        // Get the class name
        $class = str_replace(EXT, '', $class);

        //Directoryセパレータで分割
        $subdir = '';
        if (strpos($class, '/') !== FALSE)
        {
            $arrdir = explode('/', $class);
            //
            if (count($arrdir) >= 2)
            {
                for ($i=1; $i < count($arrdir); $i++) {
                    $subdir .= $arrdir[$i-1] . '/';
                }
            }
            $class = $arrdir[count($arrdir)-1];
        }

        // We'll test for both lowercase and capitalized versions of the file name
        foreach (array(ucfirst($class), strtolower($class)) as $class)
        {
            $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT;

            // Is this a class extension request?
            if (file_exists($subclass))
            {
                $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;

                if ( ! file_exists($baseclass))
                {
                    log_message('error', "Unable to load the requested class: ".$class);
                    show_error("Unable to load the requested class: ".$class);
                }

                // Safety:  Was the class already loaded by a previous call?
                if (in_array($subclass, $this->_ci_classes))
                {
                    $is_duplicate = TRUE;
                    log_message('debug', $class." class already loaded. Second attempt ignored.");
                    return;
                }

                include($baseclass);
                include($subclass);
                $this->_ci_classes[] = $subclass;

                return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
            }

            // Lets search for the requested library file and load it.
            $is_duplicate = FALSE;
            for ($i = 1; $i < 3; $i++)
            {
                $path = ($i % 2) ? APPPATH : BASEPATH;
                $filepath = $path.'libraries/'.$subdir.$class.EXT;

                // Does the file exist?  No?  Bummer...
                if ( ! file_exists($filepath))
                {
                    continue;
                }

                // Safety:  Was the class already loaded by a previous call?
                if (in_array($filepath, $this->_ci_classes))
                {
                    $is_duplicate = TRUE;
                    log_message('debug', $class." class already loaded. Second attempt ignored.");
                    return;
                }

                include($filepath);
                $this->_ci_classes[] = $filepath;
                if ($subdir)
                {
                    return $this->_ci_init_class_my($class, $subdir, str_replace('/', '_', $subdir), $params);
                }
                else
                {
                    return $this->_ci_init_class($class, '', $params);
                }
            }
        } // END FOREACH

        // If we got this far we were unable to find the requested class.
        // We do not issue errors if the load call failed due to a duplicate request
        if ($is_duplicate == FALSE)
        {
            log_message('error', "Unable to load the requested class: ".$class);
            show_error("Unable to load the requested class: ".$class);
        }
    }

    // --------------------------------------------------------------------

    private function _ci_init_class_my($class, $subdir, $prefix = '', $config = FALSE)
    {
        $class = strtolower($class);
        // Is there an associated config file for this class?
        if ($config === NULL)
        {
            if (file_exists(APPPATH.'config/'.$subdir.$class.EXT))
            {
                include(APPPATH.'config/'.$subdir.$class.EXT);
            }
        }

        if ($prefix == '')
        {
            $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
            // Set the variable name we will assign the class to
            $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
        }
        else
        {
            $name = $prefix.$class;
            // Set the variable name we will assign the class to
            $classvar = ( ! isset($this->_ci_varmap[$name])) ? $name : $this->_ci_varmap[$name];

        }


        // Instantiate the class
        $CI =& get_instance();
        if ($config !== NULL)
        {
            $CI->$classvar = new $name($config);
        }
        else
        {
            $CI->$classvar = new $name;
        }
    }


  is there a way to reset Validation class?
Posted by: El Forum - 07-05-2008, 12:37 AM - Replies (4)

[eluser]Chillahan[/eluser]
I have a multi-page registration form process. To go page to page I am doing:

Code:
if ($this->validation->run() == FALSE)
        {
            // load the view for the current page again, which will also show validation errors;
        }
        else
        {
            // load view for the next step;
        }

However, when I load the view for the next step, it will show a validation error, even though the user has not had a chance to enter data yet. If, instead, in the else case, I use the redirect() function, then of course it works. I assume the validation class is using some sort of cookie to store whether the page it is validating has already been viewed once or not (based on URL), and so when I use load to get the next page up it fails and shows an error right away (since URL looks the same).

Now, I realize that using redirect is pretty simple, but I still think using load is cleaner since it avoids doing a redirect. Originally I thought it would be more secure too, since I could make the other registration steps more private by prefixing hem with _, but then quickly realized that doesn't work since then a user can't view the second steps or beyond if they don't enter data correctly (since those steps' forms do submit to their own controllers).

Perhaps I am just being silly and the only way to do it is to redirect to the next form page upon successful completion of the current page? I am having to check cookie/session data anyway to ensure people can't skip to the middle of the registration process, so really this is becoming more of a theoretical question.


  Sub-obects break Template Parser data
Posted by: El Forum - 07-04-2008, 11:20 PM - No Replies

[eluser]Unknown[/eluser]
I'm not sure if this is a bug or new feature request, but I had a problem with putting an object into the Template data - the Parser library tries to convert it to a string and errors at line 63. It's fine with an array, but I wanted to put a DB object in there in amongst other 'flat' data, and it's annoying to have to convert it to an array, or hide it in some other way.

eg.

Code:
$oTVars->oTest = $this->testmodel->get_object($iTestID);
$oTVars->sName = 'some other flat content';
$this->parser->parse('page-home_test-view', $oTVars);
So I've changed line 57 of system/libraries/Parser.php thus:

Code:
if (is_array($val) || is_object($val))
and it now works as expected.

Is this something that might be considered for later releases - or does it introduce problems? Or is there another way round it?


  Objects of Models within models?
Posted by: El Forum - 07-04-2008, 10:05 PM - Replies (4)

[eluser]Carl_A[/eluser]
I have a structure some what like this:

user_model.php

Code:
Class User_model extends Model
  {
    //some properties and functions
  }

attributes_model.php
Code:
Class Attributes_model extends Model
  {
    //some properties and functions
  }



Now what I need is
Code:
Class Member_model extends Model
  {
     //some properties

     Object of User_model class
     Object Array of Attributes_model class

      //some more functions that may also call functions from those objects

  }


Before you all tell me to put it all into 1 model/class, I need to have them in separate models because they're all user dependent. Everything is variable.

Also, I dont want to simply call functions of User_model and Attributes_Model from within the member_model, i need to access it as a complete object (similar to C++)


How can I make objects of a model within a model?


  render views for json encoding
Posted by: El Forum - 07-04-2008, 08:41 PM - Replies (8)

[eluser]coolant[/eluser]
Is it possible to load views into variables and then render them via json?

i.e...

Code:
$data['name'] = 'John';

$json['header'] = $this->view( 'header', $data );
$json['page'] = $this->view( 'page' );
$json['footer'] = $this->view( 'footer', $data );

echo json_encode($json);


  Best Practice for dynamic header content
Posted by: El Forum - 07-04-2008, 06:48 PM - Replies (2)

[eluser]Chillahan[/eluser]
I am wondering how people currently handle dynamic headers. I've seen discussion of how to bundle header, page, and footer view loading into master template files, which I was thinking of doing, but decided against (since it only saves a couple of lines of code and then makes it actually harder to customize the header in each case. Anyway, that's not my question - my question is if anyone has what they feel to be the Holy Grail of handling dynamic header content (and still applying the concept of Don't Repeat Yourself).

To start, some common needs I see in customizing a "header":

- customizing navigation - either showing different navigation (for tabbed approaches) or at least highlighting the currently selected part of the navigation;
- customizing JavaScript loaded in header to support dynamic functionalities in the page
- custom data display - showing login box vs. "Welcome, Username" + Logout (this isn't so bad since it is independent of which page you're on)

The first two are major ones. Here are some approaches I've thought of:

1. instead of having one header (which contains HTML declaration tags, complete HEAD section, and BODY open tag), have two (or more) header files... one would load HTML doc tags, the next would cover the HEAD section, and the last would cover the BODY open tag and the navigation (or one view could handle the first two of these)
2. sending text to be injected into HEAD via a $data parameter, and dealing with customizing nav some other way that makes it independent (i.e., it could look in cookie for currently active site section so as to know which section to highlight/display)
3. using native PHP include files within the master header view file to load what's needed based on some sort of switch statement and cascading file include system (i.e., a default file for HEAD content, and then override files if needed for each page of the site, located in a subdirectory to the main HEAD file, and only loaded instead of default if located)
4. just have different header files for different site pages, and repeat yourself

This seems to be a common need - it almost seems like CI should address this at the platform level, having HEAD files be a separate entity for sure, and then having some sort of navigation helper (even if only cookie/session based) to help developers automate the showing/highlighting of correct navigation areas.

For now, out of the four approaches I've outlined, $2 would be the most proper, but also one of the uglier options, since HTML/JS code would be pushed to a view by the controller (or it could push it a name of a file to include instead - then it becomes more like #3, or the same really). #1 seems to be the simplest, and isn't so bad in terms of being elegant. Again, it's really a different take on #3 and #2, and keeps "control" in the controller where it belongs (but makes HTML/CSS/JS dev even more segregated).

Thoughts?


  Checking login sessions
Posted by: El Forum - 07-04-2008, 06:06 PM - Replies (2)

[eluser]Carl_A[/eluser]
Hi,
Im new to Codeigniter.
Im making a user login page, and i wanted to check for inactivity

Forexample, I create a new session using the session class when the user has properly logged in

Code:
class User extends Controller{
  
   function User()
    {
        parent::Controller();
        $this->load->helper('form');
        $this->load->helper('url');
        
    }
   function index()
   {
       if($this->session->userdata('logged_in')==TRUE)
       {
           $this->load->view('main_page');
       }
       else
           $this->load->view('login_page');
   }  
    
    function login()
    {
    $username = $this->input->post('Username');    
    $password  = $this->input->post('Pass');
    
    $this->db->where('Username',   $username);
    $this->db->where('Pass', $password);
    $query =$this->db->get('system_userdetails');
    
    if ($query->num_rows() > 0)
        {
           foreach ($query->result() as $row)
           {
              echo $row->ID;
              echo $row->EmailAddr;
              echo $this->session->userdata('session_id');
              $newdata = array(
                   'username'  => $row->Username,
                   'email'     => $row->EmailAddr,
                   'logged_in' => TRUE
               );
              
              $this->session->set_userdata($newdata);
           }
           redirect('User/index');
        }
    else
        {
           $this->load->view('login_page');
          
        }
      
      
    }  
  }

This works fine to login, but now i want to check if theres been no page load for say 10 mins, the session should get destroyed, and the user would have to re-login, but as long as the user stays active, the session continues..

How can i achieve that? I tried the Benchmark Class, however it seems to not work when the functions/controllers switch. (i.e I cant mark the starting point in my login function, and mark an ending point in another controller.. and check the time.)
When i echo'ed the value of time elapsed, if just gave me a blank page.

Please Help.

Thanks in advance


  An noob ask helpme plz
Posted by: El Forum - 07-04-2008, 03:47 PM - Replies (1)

[eluser]Unknown[/eluser]
hi im like known how i can put a font installed in fonts folder
i like this font in all pages how i call this font

Sorry for my english es very bad XD :cheese: :cheese:


Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Latest Threads
Codeigniter4 version 4.5....
by kenjis
5 hours ago
SQL server connection not...
by kenjis
5 hours ago
Problem with session hand...
by kenjis
5 hours ago
Is it possible to go back...
by kenjis
5 hours ago
Cannot access protected p...
by xsPurX
7 hours ago
Update to v4.5.1, same us...
by xsPurX
Today, 08:31 AM
How to use Codeigniter wi...
by kenjis
Today, 05:06 AM
CVE-2022-40834 SQL Inject...
by kenjis
Today, 03:44 AM
Retaining search variable...
by pchriley
Today, 03:15 AM
Disable debug output in v...
by groovebird
Today, 02:26 AM

Forum Statistics
» Members: 85,388
» Latest member: homefurniture
» Forum threads: 77,581
» Forum posts: 376,007

Full Statistics

Search Forums

(Advanced Search)


Theme © iAndrew 2016 - Forum software by © MyBB