Welcome Guest, Not a member yet? Register   Sign In
Having trobles with data from DB
#1

I currently have this code in my controller:

PHP Code:
   public function profile($username){
 
       
        
// Check Login
 
       if(!$this->session->userdata('user_id')){
            
// Redirect to page
            
redirect('users/login');
 
       }

 
       // Get user info
 
       $data['item'] = $this->User_model->get_username($username);
 
       $user $this->User_model->get_username($username);
 
       
        
// Meta
 
       $data['title' $this->settings->title.' | '.ucfirst($user->username);
 
       $data['description' ucfirst($user->biography);

 
       // Already friends?
 
       $data['relationship'] = $this->User_model->the_relationship($user->id);

 
       // If empty show a 404 error
        
if(empty($data['item'])){
            
show_404();    
        }
                    
 
       // Load template
 
       $this->template->load('public''default''users/profile' $data);
        
 
   

and this in my model:

PHP Code:
   // Relationship for single
 
   public function the_relationship($id){    
    
$this->db->select('*');
 
       $this->db->from($this->relationship);
 
       $this->db->where('user_id'$this->session->userdata('user_id'));
 
       $this->db->where('friend_id'$id);
 
       $this->db->where('type'$this->type);

        
$query $this->db->get();

        if(
$query->num_rows() >= 1){
            return 
$query->row();
        } else {
            return 
false;
        }
    } 

and this is my view:

PHP Code:
       <?php if($item->username == $this->session->username) : ?>
        <!-- If friendship accepted - You can cancel the friendship request to this user -->
        <?php elseif($relationship->status == 'accepted') : ?>
            <?= form_open('users/unfollow/'.$item->id.'/'.$item->username) ; ?>
                <?php $data = array(
 
                   'name'  => 'friend_id',
 
                   'id'    => 'friend_id',
 
                   'value' => $item->id,
 
               ); ?>
                <?= form_hidden($data); ?>
            <?= form_submit('mysubmit''Unfollow', array('class' => 'btn btn-warning btn-xs')); ?>
            <?= form_close(); ?>
        <!-- If friendship pending - You can cancel the friendship request to this user -->
        <?php elseif($relationship->status == 'pending') : ?>
            <?= form_open('users/unfollow/'.$item->id.'/'.$item->username) ; ?>
                    <?php $data = array(
 
                       'name'  => 'friend_id',
 
                       'id'    => 'friend_id',
 
                       'value' => $item->id,
 
                   ); ?>
                    <?= form_hidden($data); ?>
                <?= form_submit('mysubmit''Pending - Cancel Friend Request', array('class' => 'btn btn-warning btn-xs')); ?>
            <?= form_close(); ?>
        <!-- If no friendship exists whatsoever - You can send a friendship request to this user -->
        <?php else : ?>
            <?= form_open('users/follow/'.$item->username) ; ?>
                <?php $data = array(
 
                   'name'  => 'friend_id',
 
                   'id'    => 'friend_id',
 
                   'value' => set_value('friend_id'),
 
               ); ?>
                <?= form_hidden($data); ?>
            <?= form_submit('mysubmit''Follow', array('class' => 'btn btn-warning btn-xs')); ?>
            <?= form_close(); ?>
        <?php endif; ?>


The problem is when there is not existing relationship(friendship). Let's say when I visit a user which is already my friend, it shows me the proper button "the unfollow"(friendship; status: accepted) and it shows the proper button as well when the friendship is in status: pending.

Now when I visit a user which is not my friend(when there's not friendship), it should display the last button which is the follow button but the current output is an error of "error handler"

Can somebody tell me where is my error?
I do Front-End development most of the time 
Reply
#2

@kirasiris,

What value is relationship when the error is thrown? Also, any reason why you didn't use switch condition instead of if_then_else condition?
Reply
#3

It is not clear exactly when is the error thrown. Is it as the not-friend profile displays or after you try to "follow" them?

What error message is shown?
Reply
#4

Do you know that, in the view, the first if() block has no code to execute?

Also, you do not need to use PHP opening and closing tags on every statement. Unless you want to leave the PHP processor to output HTML directly you only need the PHP opening tag.

If all the superfluous PHP tags are removed you get this, much easier to read code.

PHP Code:
<?php
if($item->username == $this->session->username) :
//  If friendship accepted - You can cancel the friendship request to this user -->
elseif($relationship->status == 'accepted') :
 
   form_open('users/unfollow/'.$item->id.'/'.$item->username);
 
   $data = array(
 
       'name' => 'friend_id',
 
       'id' => 'friend_id',
 
       'value' => $item->id,
 
   );

 
   form_hidden($data);
 
   form_submit('mysubmit''Unfollow', array('class' => 'btn btn-warning btn-xs'));
 
   form_close();
//  If friendship pending - You can cancel the friendship request to this user -->
elseif($relationship->status == 'pending') :
 
   form_open('users/unfollow/'.$item->id.'/'.$item->username);

 
   $data = array(
 
       'name' => 'friend_id',
 
       'id' => 'friend_id',
 
       'value' => $item->id,
 
   );

 
   form_hidden($data);
 
   form_submit('mysubmit''Pending - Cancel Friend Request', array('class' => 'btn btn-warning btn-xs'));
 
   form_close();
//  If no friendship exists whatsoever - You can send a friendship request to this user -->
else :
 
   form_open('users/follow/'.$item->username);

 
   $data = array(
 
       'name' => 'friend_id',
 
       'id' => 'friend_id',
 
       'value' => set_value('friend_id'),
 
   );

 
   form_hidden($data);
 
   form_submit('mysubmit''Follow', array('class' => 'btn btn-warning btn-xs'));
 
   form_close(); 
endif; 

Hopefully, it is now clear the first if() is followed by elseif() without code (except for the comment) in between the two.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB