Welcome Guest, Not a member yet? Register   Sign In
first ci menu compiler... { MCV } is right to write as is ?
#1

[eluser]GioMBG[/eluser]
Hi ALL,
newbie ci
I just start to make my first SIMPLY application to compile the menu of my new web 2.0 control panel,
only to ask if is plausible use the MCV as is or if I have to make better my assemble.

controller:
Code:
class Site extends CI_controller{

function index() {
$this->load->model('data_model');
$data['rows'] = $this->data_model->getAll();
$this->load->view('home',$data);
}
}

module:
Code:
class Data_model extends CI_model {

function getAll() {
$rows = FALSE;
$this->db->select("idm, usa, level, name, href, sub, href_class, i_class, span_class, b_class");
$this->db->from('menu');

$q = $this->db->get();

  if ($q->num_rows() > 0) {
   foreach ($q->result() as $row){
   $data[] = $row;
   }
  return $data;
  }
}
}

view: (of course after I can obtain to write the array menu on an .html static file or recall all the script to render more dinamik the menu)...
Code:
$menu = FALSE;
$tab  = ' ';
$ul   = 0;

$menu = "<ul class=\"nav nav-list\">\n";

foreach ($rows as $r):

$level = 9;

  if ($level >= $r->level AND $r->usa ==1) {

  $menu.= "$tab<li>\n";

  $menu.= "$tab$tab<a >href\"";

   if ($r->href_class == TRUE) {
   $menu.= " class=\"$r->href_class\">\n";
   } else {
   $menu.= ">\n";
   }

   if ($r->i_class == TRUE) {
   $menu.= "$tab$tab<i class=\"$r->i_class\"></i>\n";
   }

   if ($r->span_class == TRUE) {
   $menu.= "$tab$tab<span class=\"menu-text\">$r->name</span>\n";
   } else {
   $menu.= $tab.$tab.$r->name."\n";
   }

   if ($r->b_class == TRUE) {
   $menu.= "$tab$tab<b class=\"$r->b_class\"></b>\n";
   }

  $menu.= "$tab$tab</a>\n";

   if ($r->sub == 1) {
   ++$ul;
   $menu.= "$tab<ul class=\"submenu\">\n";

   } elseif ($r->sub == 2) {

    $c=1;
    while($c<=$ul) {
    $menu.= "$tab</li>\n";
    $menu.= "$tab</ul>\n";
    ++$c;
    }

   $menu.= "$tab</li>\n";

   $ul = 0;
   } else {
   $menu.= "$tab</li>\n";
   }
  }

endforeach;

$menu.= "</ul>&lt;!-- /.nav-list --&gt;\n";

echo "&lt;textarea cols=\"100\" rows=\"35\" read&gt;$menu&lt;/textarea&gt;\n";

thanks in advance
GioMBG
#2

[eluser]GioMBG[/eluser]
thanks 4 the reply !
GioMBG
#3

[eluser]Tpojka[/eluser]
What is concrete question?
Got some error(s)?
#4

[eluser]GioMBG[/eluser]
I'm tring to do as is described in the ci netiquette regarding MCV,
but for to compile a menu I don't know another solution that writing code on view.
is right make a viev like this ?
GioMBG
#5

[eluser]Tpojka[/eluser]
You can use simple HTML in view and just insert PHP variables for some output. Those variables you shall store in $data array you have already.
Example:
Code:
// controller

$data['header_class'] = 'header';

Code:
//view
<!doctype HTML>
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
<div class="&lt;?php echo $header_class; ?&gt;">Some text here...</div>
&lt;/body&gt;
&lt;/html&gt;

That is pretty much similar way as I do.

Edit: of course for loops you have to use more PHP code.
But I am looking to use more PHP in controller and non HTML if possible in controller.
#6

[eluser]Massaki[/eluser]
I don't see problems on using control structures or loops inside the view. But if you realize that a code section could be transformed into a helper, then do it (and document it)
#7

[eluser]GioMBG[/eluser]
OK,
I understand the policy and the sense of MCV,
so is better write here php loops codes than html codes on controllers...
Thanks
GioMBG




Theme © iAndrew 2016 - Forum software by © MyBB