Welcome Guest, Not a member yet? Register   Sign In
Code logic in codeigniter
#1

(This post was last modified: 08-25-2015, 01:25 PM by sebastianvirlan.)

Can someone show me a optim and correct version of my code? I have a lot of if else switch, and I think my code is not organised

So I have a foreach in controller - method
I have get_user_details function in My Controller
The 3rd image is the view where I check if user is anonymous
The 4th image is same view another check if user is anonymous.

As you see I have a lot of if else and I don`t like my code.

In this line: $user = $this->users->select_details_by_id($user_id);
$user get all fields from database with details like:
$user->first_name would be Sebastian,
$user->last_name would be Vîrlan .

In controller method I have $un_questions->asker_id from database so if is 0 the user is anonymous.

Account.php

PHP Code:
public function questions() {

 
$this->login_required();

 
        $header['title' 'Unanswered Questions';
 
        $data['page'  DIR __FUNCTION__;
 
//page questions: user_id, status (1 = unanswered), limit, offset
 
$unanswered_questions $this->questions->select_by_profile($this->the_user->id1030);

 
       foreach ($unanswered_questions as $un_questions) {

 
        //get asker_id and asked_id details
 
        $un_questions->asker $this->get_user_details($un_questions->asker_id);

 
       }
 
       //number of questions
 
       $data['no_questions'   $this->questions->select_by_profile_and_status_count($this->the_user->id1);
 
               
 $data
['questions_unanswered_block'] = $this->questions_unanswered_block($unanswered_questionsNULL);

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


My Controller.php

PHP Code:
    public function get_user_details($user_id)
    {
        $user = new stdClass();
        
        
switch ($user_id) {
            case 0:
                $user->name $user->username $user->link $this->lang->line('anonymous');
                $user->city "Unknown";
                $user->profile_picture 'assets/images/default50.jpg';
                break;
            
            
default:
                $user $this->users->select_details_by_id($user_id);
                $user->name $user->first_name.' '.$user->last_name;
                $user->link '<a href="'.site_url('account/profile/' $user->username).'">'.$user->first_name.' '.$user->last_name.'</a>';
                $user->profile_picture 'uploads/users/'.md5($user->username.$user->id).'/'.$this->images->select_by_ids_image_and_type($user_id1);
                break;
        }

        return $user;

    


my_view.php

PHP Code:
<div class="profile-blog">

 <
img class="rounded-x" src="<?php echo $q->asker->profile_picture ?>" alt="@<?php echo $q->asker->name; ?>">
    <div class="name-location">
 <
ul class="list-inline pull-right">
 <
li><small class="time-ago"><?php echo $this->time->ago($q->question_date); ?></small></li>
 <li><i class="question-delete fa fa-close"></i></li>
        
 
    </ul>
    <strong><?php echo $q->asker->link?></strong>
        <span>
            <i class="fa fa-map-marker"></i>
            <a href="#"><?php echo $q->asker->city?></a>
        </span>                
 </div>
 <div class="clearfix margin-bottom-20"></div>    
 <p><?php echo $q->question_text?></p>
 <?php if(isset($q->question_image) &&  $q->question_image): ?>
    <div class="row">
        <div class="col-sm-2 col-xs-4">
            <div class="thumbnail">
                <a class="fancybox-button zoomer" data-rel="fancybox-button" title="<?php echo $q->question_text?>" href="<?php echo base_url("{$q->image_path}{$q->question_image}"); ?>">
                    <span class="overlay-zoom">
                        <img class="img-responsive question-pic" src="<?php echo base_url("{$q->image_path}{$q->question_image}"); ?>" />
                        <span class="zoom-icon"></span>
                    </span>
                </a>
            </div>
        </div>
 </div>
 <?php endif; ?>
 <div class="panel panel-profile">
 </div>
</div> 
Reply


Messages In This Thread
Code logic in codeigniter - by sebastianvirlan - 08-24-2015, 01:54 PM
RE: Code logic in codeigniter - by mwhitney - 08-25-2015, 08:55 AM
RE: Code logic in codeigniter - by cartalot - 08-25-2015, 11:38 AM
RE: Code logic in codeigniter - by mwhitney - 08-25-2015, 01:41 PM
RE: Code logic in codeigniter - by cartalot - 08-25-2015, 03:34 PM
RE: Code logic in codeigniter - by mwhitney - 08-26-2015, 08:22 AM
RE: Code logic in codeigniter - by cartalot - 08-25-2015, 08:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB