Raj
Raj

Reputation: 307

PHP - Codeigniter professional structure

I am graduating in next month. I am applying for entry level php developer jobs. Many companies are asking to send sample code.

I am sending sample controller, view and model files and some screenshots of the outputs, but I am not getting through.

Please help me. Where Am I doing wrong? What am I supposed to send them? Is there any professional way of writing/structuring code?

My sample code files are:

Controller

<?php

class NewsRelease extends Controller
{
    function NewsRelease()
    {
        parent::Controller();
        $this->load->helper('url');
        // $this->load->helper('form');
        $this->load->model('news_model');
        $this->load->library('session');
    }

    /*
    This is loads the home page called 'home_view'. Before loading this,
    It checks wheter the admin is logged in or not and clears the admin
    session. Because, When admin logged out, he will be shown this page.
    */

    function index()
    {
        $checksession=$this->session->userdata('name');
        if(isset($checksession))
        {
            $this->session->unset_userdata('name');
            $this->session->unset_userdata('password');
        }
        $this->load->view('home_view');
    }

    /*
    On loading the home page, to display all the feature news, the following
    function is needed.
    */

    function datanews()
    {
        //$data['hi']="Hello World";
        $query=$this->news_model->getactivenews();
        foreach($query->result_array() as $row1)
        {
            echo"<p><h4><b><a href='#' id='".$row1['ID']."'>".$row1['Title']."</a>
            </h4></b>".substr($row1['Body'],0,100)."<b>...<a href='#' 
            id='".$row1['ID']."'> read more></b></a></p></br>";
        }    
    }

    /*
    All the archive news can be shown by this function.   
    */

    function archiveNews()
    {
        $year=trim($this->input->post('year'));
        $query=$this->news_model->getArchiveNews($year);
        foreach($query->result_array() as $row1)
        {
            echo"<p><h4><b><a href='#' id='".$row1['ID']."'>".$row1['Title']."</a></h4>
            </b>".substr($row1['Body'],0,100)."<b>...<a href='#' id='".$row1['ID']."'> 
            read more></b></a></p></br>";
        }
    }

    /*
    On clicking the Admin link on the home page, he will be navigated
    to the admin login page.
    */

    function adminlogin()
    {
        $this->load->view('adminlogin_view');
    }

    /*
    The admin login authentication can be handled by thie function.
    And the session stores his ID and Password. 
    */

    function validate()
    {
        $name=trim($this->input->post('name'));
        $password=trim($this->input->post('pwd'));

        $sess_data=array("name" => $name,
            "password" => $password);
        $this->session->set_userdata($sess_data);

        if($name=="raj"&&$password=="raj")
        {
            echo "1";
        }
        else
            echo "0";
    }

    /*
    After successful authentication, Admin will be shown his home page
    where he can add, modify and delete the news.
    */

    function adminhome()
    {
        if($this->session->userdata('name') && $this->session->userdata('password'))
            $this->load->view('adminhome_view');
    }

    /* and some more functions go here. */

?>

View

<?php $this->load->view('header'); ?>
<!-- scripthome.js has all the javascript and jquery code related to the functions which do the above mentioned process-->

<script type="text/javascript" src="<?php echo base_url();?>js/scripthome.js"></script>
<div id="content">

    <h3 class="FeatureNews"><a href="#" id="feature"> Feature News </a></h3><h3 class="admin"><?php echo anchor('newsrelease/adminlogin','Admin')?></h3>


    <div id="newsdetails">
        <!-- FEATURE NEWS DETAILS-->
    </div>
    <!--
    The archive page should display a list of all active news items in descending order (newest to oldest
    based on release date). Similar to the home page, archived news item features a title, a portion of
    the story and allow the users the ability to either click a title or a "read more" link to view the entire
    story
    -->

    <div id="newsarchivedetails">
        <!-- ARCHIVE NEWS-->
    </div>

    <div id="newsarchive">
        <!-- ARCHIVE NEWS-->
    </div>
    <div id="newshome">
        <!-- FEATURE NEWS-->

    </div>
    <div id="archivediv">
        <h3 class="archive">News Archive by</h3><h3><a href="#" id="2010"> 2010 </a> | <a href="#" id="2009"> 2009 </a> | <a href="#" id="2008"> 2008 </a></h3> <a href="#" id="2007">2007</a>
    </div>
    <!-- CONTENT CLOSE -->

</div>

<!-- WRAPPER CLOSE -->
<?php $this->load->view('footer');?>

Model

<?php

class News_model extends Model
{
    function News_model()
    {
        parent::Model();    
    }

    /*
    It gets all the featured news from the table News.
    */

    function getactivenews()
    {
        $this->db->where('Status','1');
        $this->db->where('Type','1');
        $this->db->order_by('ID','desc');
        return $this->db->get('News'); 
    }

    /*
    It gets all the news whose type is '0'(archived)
    */

    function getArchiveNews($year)
    {
        $this->db->where('year(Date)',$year);
        $this->db->where('Status','1');
        $this->db->where('Type','0');
        $this->db->order_by('ID','desc');
        return $this->db->get('News');
    }

}

?>

Upvotes: 3

Views: 507

Answers (2)

Wouter Dorgelo
Wouter Dorgelo

Reputation: 11978

  1. You could start with removing HTML tags from your Controller class (MVC)
  2. Create propper documentation (@return, @param, etc.)
  3. Most companies want to see code + the working version of that code on a live server
  4. If it's not an CodeIgniter specific job, show also non-CodeIgniter code
  5. Show em you can read/write UML diagrams
  6. Add some variety (XML, SOAP, OOP, different dbtypes, file upload, sessions, security, etc.)
  7. Point out you build your code by atleast Test Driven Development or Behaviour Driven Development

And ofc.. good luck!!

Upvotes: 7

Jorge Guberte
Jorge Guberte

Reputation: 11054

Well, i think you should write your own structures. It's fairly easy to send in some pre-built code, and i'm sure the potential employer will be much happier if he sees something unique. Of course, if you're applying for a CI position you'll have to send something built with CI.

Upvotes: 1

Related Questions