CodeIgniter Forums
Array in view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Array in view (/showthread.php?tid=65055)



Array in view - ventt - 04-24-2016

[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.


RE: Array in view - arma7x - 04-24-2016

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


RE: Array in view - ventt - 04-24-2016

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.


RE: Array in view - mwhitney - 04-27-2016

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.


RE: Array in view - ventt - 04-28-2016

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