Welcome Guest, Not a member yet? Register   Sign In
Post your controller!
#13

[eluser]jegbagus[/eluser]
hey bro, your code look really mess...
better not to use a mvc framework if you won't separate the code in mvc way..

[quote author="James Brauman" date="1251631147"]Lets see what everyone elses coding standards look like, how they comment, and how they structure thier controllers. Perhaps we can learn something. Obviously, remove any sensitive information :-) Note: If your controller is absolutely massive, maybe post a different one.

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

class Code extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index($code_name)
    {
        $this->load->model('code_model');
        $this->load->library('tank_auth');
        $this->load->library('form_validation');
        
        // If the code url is not valid, show error message.
        $sql = 'SELECT * FROM codes WHERE url_name = ?';
        $bindings = array($code_name);
        $listing = $this->db->query($sql, $bindings)->row_array();
        if (empty($listing))
        {
            // Show error
            return;
        }
        
        // If we have feedback, lets post it.
        if ($this->tank_auth->is_logged_in() && $this->input->post('feedback'))
        {
            $this->form_validation->set_rules('feedback', 'Feedback', 'trim|required|xss_clean');
            if ($this->form_validation->run())
            {
                // Insert the data into the feedback table
                $sql = 'INSERT INTO feedback(code_id, user_id, feedback) VALUES (?, ?, ?)';
                $bindings = array ($listing['id'], $this->tank_auth->get_user_id(), set_value('feedback'));
                $this->db->query($sql, $bindings);
            }
        }
        
        // If we have a rating, lets rate it.
        if ($rating = $this->input->post('rating'))
        {
            if (is_numeric($rating) && $rating >= 1 && $rating <= 5)
            {
                // Get the number of votes that this IP address has on this code.
                $sql = 'SELECT * FROM ratings WHERE INET_NTOA(ip) = ? AND code_id = ?';
                $bindings = array ($_SERVER['REMOTE_ADDR'], $listing['id']);
                $query = $this->db->query($sql, $bindings);
                
                // We should only let them vote if they haven't already voted.
                if ($query->num_rows() == 0)
                {
                    $sql = 'INSERT INTO ratings (code_id, ip, rating) VALUES (?, ?, ?)';
                    $bindings = array ($listing['id'], $_SERVER['REMOTE_ADDR'], $rating);
                    $this->db->query($sql, $bindings);
                }
            }
        }
        
        // Add one to the number of views that this code has.
        $sql = "UPDATE codes SET views = views + 1 WHERE id = {$listing['id']}";
        $this->db->query($sql);
    
        // Retrieve language, category, tag and user arrays and add them to the listing.
        $sql = "SELECT * FROM languages WHERE id = {$listing['language_id']}";
        $listing['language'] = $this->db->query($sql)->row_array();
        unset($listing['language_id']);
        
        $sql = "SELECT * FROM categories WHERE id = {$listing['category_id']}";
        $listing['category'] = $this->db->query($sql)->row_array();
        unset($listing['language_id']);        

        $sql = "SELECT username FROM users WHERE id = {$listing['user_id']}";
        $listing['user'] = $this->db->query($sql)->row_array();
        unset($listing['user_id']);
        
        // Retrieve rating average and rating number and add it to listing
        $sql = "SELECT COUNT(rating) as rating_count, SUM(rating) / COUNT(rating) as average FROM ratings WHERE code_id = {$listing['id']}";
        $listing['rating'] = $this->db->query($sql)->row_array();
        
        // Retrieve the tags that this listing has
        $listing['tags'] = $this->code_model->get_tags($listing['id']);
        
        //echo '<pre>', print_r($listing, true), '</pre>';
        //return;
        
        // Render the partial view.
        $partial_data = array ('listing' => $listing);
        
        // If the user is logged in, lets add it to the partial data
        if ($this->tank_auth->is_logged_in())
        {
            $sql = 'SELECT * FROM users WHERE id = ?';
            $bindings = array($this->tank_auth->get_user_id());
            $partial_data['user'] = $this->db->query($sql, $bindings)->row_array();
        }
        
        // Get a list of feedback and add it to the partial data
        $sql = "SELECT feedback.*, users.username
                FROM feedback
                INNER JOIN users
                ON feedback.user_id = users.id
                WHERE feedback.code_id = {$listing['id']}
                ORDER BY feedback.time_posted ASC";
        $partial_data['feedback'] = $this->db->query($sql)->result_array();
        
        $partial_rendered = $this->load->view('pages/partial/code/index', $partial_data, TRUE);
        
        // Render the layout
        $layout_data = array(
            'content' => $partial_rendered
        );
        $this->load->view('pages/layout', $layout_data);
    }
    
    function _remap($code_name)
    {
        $this->index($code_name);
    }
}
[/quote]


Messages In This Thread
Post your controller! - by El Forum - 08-30-2009, 12:19 AM
Post your controller! - by El Forum - 08-30-2009, 01:36 AM
Post your controller! - by El Forum - 08-30-2009, 07:19 AM
Post your controller! - by El Forum - 08-30-2009, 12:31 PM
Post your controller! - by El Forum - 08-30-2009, 12:44 PM
Post your controller! - by El Forum - 08-30-2009, 08:59 PM
Post your controller! - by El Forum - 08-30-2009, 10:07 PM
Post your controller! - by El Forum - 08-31-2009, 02:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:13 AM
Post your controller! - by El Forum - 08-31-2009, 03:19 AM
Post your controller! - by El Forum - 08-31-2009, 04:43 AM
Post your controller! - by El Forum - 08-31-2009, 04:49 AM
Post your controller! - by El Forum - 09-01-2009, 01:05 AM
Post your controller! - by El Forum - 09-01-2009, 09:30 PM
Post your controller! - by El Forum - 09-02-2009, 02:42 AM
Post your controller! - by El Forum - 09-02-2009, 05:34 AM
Post your controller! - by El Forum - 09-02-2009, 09:02 AM
Post your controller! - by El Forum - 09-02-2009, 09:43 AM
Post your controller! - by El Forum - 09-02-2009, 10:13 AM
Post your controller! - by El Forum - 09-02-2009, 01:13 PM
Post your controller! - by El Forum - 09-03-2009, 06:40 AM
Post your controller! - by El Forum - 11-03-2009, 10:20 PM
Post your controller! - by El Forum - 11-03-2009, 11:02 PM
Post your controller! - by El Forum - 11-03-2009, 11:33 PM
Post your controller! - by El Forum - 11-04-2009, 12:09 AM
Post your controller! - by El Forum - 11-04-2009, 03:36 AM
Post your controller! - by El Forum - 11-04-2009, 07:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB