Welcome Guest, Not a member yet? Register   Sign In
Re use view for various purposes - Help
#21

[eluser]andrewtheandroid[/eluser]
Ahh yeh I think I know what you mean. Two options come to mind here.

1. Combine them back into the same one Smile
This way you add the form at the bottom then you can do check if any post values and validation then you still show the news details. Like what you were doing before. But now show the error somewhere (now you are using the string method? so you can now easilly manipulate your final view).

Code:
function readArticle()
{

// if any post validate as usual
// commit entry to database
// generate either success or error message
$data['error'] = ....;

// ----- code below is your original show article()
       // get from database/populate article and comments
// ----- show your error message
// ----- show message form

}

2. You can reduce the amount of redundancy to only 2 lines each.

Code:
// 1. you need to make one call to database
$unprocessed_data = $this->model..... etc
// 2. you need to make a call to parse the data using a helper/library/view
$parsed_string = $this->load->view('article_view',$unprocessed_data,true);
OR
$parsed_string = my_helper_parser($unprocessed_data);

So you will have the same code twice but it's not so bad since it's not that redundant.. seeing as if you change the way you parse the article it will carry through with the second.
#22

[eluser]wowdezign[/eluser]
You could do:

Code:
class News extends Controller{

  var $data;

  function News(){
    parent::Controller();
  }

  function readNews(){
    $this->data['results'] = $this->news_model->getResults(); // $this->data refers to the property up top.
  }

  function newsDetails(){
    $this->data['details'] = $this->news_model->getDetails(); // same deal here
  }

  function addComment(){
    // use the data by referring to the $data property on this controller object:
    $this->data['results']->id //(or $this->data['results']['id'] if the results are an array)
  }
}

Hope that makes sense. Once you store data on an object, the controller itself in this case, all the functions in that controller can share and access that data.
#23

[eluser]oliur[/eluser]
[quote author="andrewtheandroid" date="1257826865"]Ahh yeh I think I know what you mean. Two options come to mind here.

1.

Code:
function readArticle()
{

// if any post validate as usual
// commit entry to database
// generate either success or error message
$data['error'] = ....;

// ----- code below is your original show article()
       // get from database/populate article and comments
// ----- show your error message
// ----- show message form

}

2.

Code:
// 1. you need to make one call to database
$unprocessed_data = $this->model..... etc
// 2. you need to make a call to parse the data using a helper/library/view
$parsed_string = $this->load->view('article_view',$unprocessed_data,true);
OR
$parsed_string = my_helper_parser($unprocessed_data);

[/quote]


I've used no 1 and it just made things so simple. I dont even need two functions. One is sufficient.

No 2 is all cool too. It's that I need to make $data accessible from different functions within the same controller. That way, there would be just one database call and redundancy removed. Thanks for your kind help.
#24

[eluser]oliur[/eluser]
[quote author="wowdezign" date="1257850559"]You could do:

Code:
class News extends Controller{

  var $data;

  function News(){
    parent::Controller();
  }

  function readNews(){
    $this->data['results'] = $this->news_model->getResults(); // $this->data refers to the property up top.
  }

  function newsDetails(){
    $this->data['details'] = $this->news_model->getDetails(); // same deal here
  }

  function addComment(){
    // use the data by referring to the $data property on this controller object:
    $this->data['results']->id //(or $this->data['results']['id'] if the results are an array)
  }
}

Hope that makes sense. Once you store data on an object, the controller itself in this case, all the functions in that controller can share and access that data.[/quote]


Yes that's brilliant and it's very OO which I like. Also, what andrew suggested about using one function is worth checking. Thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB