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

I have function in Model like this:

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:

$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?

Just looks like you got to defined $post_id that's all
$data['cats'] = $this->clean_blue_model->cats($post_id);
Something like that.

If using Active Record Class
Can change your query to
public function cats($post_id = null) {  
  if(!$qveri->num_rows())//No rows return
    return null;
    return  $qveri->result_row();
Its the same thing but will add in the backticks

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!)

$post_id=$this->input->post('id', TRUE);

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

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:

// 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?

I'm not sure what your going for but Ill take another crack at it.. Hope this helps
//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
                     ->join('cat_rel','categories.cat_id = cat_rel.cat_id','LEFT');
    if(!empty($post_id)//If Post id is set


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

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

[eluser]Reneesh T K[/eluser]
$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.