• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array in view

#1
[Image: se06az8.png]


As you can see, 3/4th of the code is my tips array, is there any way to get rid of it and make a "main" array for it, so I don't have to write the same code, also it looks ugly like this. 
Sorry for my bad professional english, don't know how to explain myself.
Reply

#2
$data->tip = array(.....);
if($this->form_validation->run()===FALSE) {
.........
}
else {
........
}
$this->load->view('viewname',$data);
Keep calm.
Reply

#3
Oh well, thank you ...  Rolleyes
I don't know why I didn't do it like this, but there must be another solution for it I think. Correct me if I am wrong though.
Reply

#4
There are a number of ways to do this, and most of it is down to personal preference. For example, you could create a $tips variable for your tips array, then add it to $data before passing it to a view:

PHP Code:
public function whatever()
{
 
   $data = new stdClass(); // initialize $data
 
   $tips $this->getTips();

 
   if ($this->form_validation->run() === false) {
 
       if ($this->User_model->isLogged()) {
 
           $user $this->User_model->getUser($_SESSION['id']);
 
       }

 
       $data->tips $tips;
 
       $data->name = isset($user) && isset($user->username) ? $user->username "Guest";

 
       $this->loadViews('base/index''sidebar'$data);
 
   } else {
 
       // ...
 
   }
}

private function 
getTips()
{
 
   return array(
 
       // ...
 
   );
}

private function 
loadViews($contentView$sidebarView null$data = array())
{
 
   $this->load->view('header'$data);
 
   $this->load->view($contentView$data);
 
   if ( ! empty($sidebarView)) {
 
       $this->load->view($sidebarView$data);
 
   }
 
   $this->load->view('footer'$data);


Some of this may also just be a matter of the overall code structure, but a picture of an incomplete section of the method isn't going to tell us whether you should just load the tips array before the form validation runs or load the views after the if block that starts at line 23 is closed. If you could include the text of the class (or even just the method), it would be a lot easier to work out a better structure than by working from a picture.
Reply

#5
Thank you, mwhitney, got lots of ideas from your code, I'll try to do something in the evening.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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