• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use attributes in controller or model?

#1
[eluser]djuric[/eluser]
I have function in Model like this:

Code:
public function cats($post_id = null) {  

  $qveri = $this->db->query("SELECT * FROM posts WHERE id = $post_id");
  return $qveri->result_row();
}

This is in controller:

Code:
$data['cats'] = $this->clean_blue_model->cats($post_id);


My question would be CAN I pass attributes to functions inside controller, like I did above, I'm getting error like variable $post_id not defined

What's the right way to pass variable to function like this?


#2
[eluser]Iciclefeet[/eluser]
Just looks like you got to defined $post_id that's all
Code:
$post_id=$this->input->post('id');
$data['cats'] = $this->clean_blue_model->cats($post_id);
Something like that.

If using Active Record Class
Can change your query to
Code:
public function cats($post_id = null) {  
  $qveri=$this->get_where('posts',array('id'=$post_id));
  if(!$qveri->num_rows())//No rows return
    return null;
  else
    return  $qveri->result_row();
}
Its the same thing but will add in the backticks

#3
[eluser]boltsabre[/eluser]
Quote:public function cats($post_id = null)
This is dangerous, your ID should NEVER be null... if by some chance it is then your DB query will fail and throw a SQL error - not good!

Best to handle it in the controller, and make sure it is set before calling your model function. If it's a post value, in your validation calls, just make sure 'required' is set, and bam, you form will only ever pass validation if it is set, and you'll only ever call your model after validation has passe. Problem solved!

(mind you, your validation checks would also want to check that it's a number, and above 0!)

#4
[eluser]InsiteFX[/eluser]
Code:
$post_id=$this->input->post('id', TRUE);

To XSS Clean your data before inserting or adding it to your database!

#5
[eluser]djuric[/eluser]
Peoplez,u no understand me Sad


I have completely different problem. Let me try to explain this way:

Model this is for showing blog index, looping all posts on home page:

Code:
// this function below is for pulling users information and his ID

public function Select_post_information_from_database() {
      $q = $this->db->query("SELECT user_id, username, email FROM users");
      return $q->result_array();

}

// this method below is my problem. I want to pull all categories for each post and show it in index page below each post. The thing I want to do below will not work of course, it's just one of the ways I tried

// How to refer to each posts ID at a time, and how to call this function from view

public function All_categories_for_one_post() {
      $q = $this->db->query("SELECT categories.cat_name FROM categories LEFT JOIN cat_rel ON categories.cat_id = cat_rel.cat_id WHERE cat_rel.post_id = ?? ")
      return $q->result_array();
}


Please help me with this, I will be very grateful. I don't want to use one big query for both, I want to separate them this way. How to do it?


#6
[eluser]Iciclefeet[/eluser]
I'm not sure what your going for but Ill take another crack at it.. Hope this helps
Code:
//Pull all categories for each post
//or for one post
public function Categories_for_post($post_id=null) {
      $this->db->select("GROUP_CONCAT(`categories`.`cat_name`) AS cat_name_list,cat_rel.post_id",FALSE)//Get all cat_name per post_id
                     ->from('categories')
                     ->join('cat_rel','categories.cat_id = cat_rel.cat_id','LEFT');
    if(!empty($post_id)//If Post id is set
       $this->where(array('cat_rel.post_id'=>$post_id);

              $q=$this->group_by("cat_rel.post_id")
                     ->get();

  if(!$q->num_rows())//No rows
    return NULL;

  return $q->result_array();      
}
Thats all i could think of, something in there might be helpful

#7
[eluser]Reneesh T K[/eluser]
Code:
$q = $this->db->query("SELECT cat_name FROM categories where categories.cat_id in (select cat_rel.cat_id from cat_rel where post_id=$id)")


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.