Welcome Guest, Not a member yet? Register   Sign In
Converting an app to CI (conceptualizing the breakdown)
#1

[eluser]CheekyGeek[/eluser]
I'm a newbie to CI, but pretty excited by what I see in the screencast demos and working my way through the User Guide and Upton book.

A couple of questions: Anything glaring "out of date" or deprecated in 1.6.2 from what is in the Upton book? For example, I don't recall the Controller functions in the blog screencast including the
Code:
{parent:: Controller();}
(which Upton says is required, at a minimum, on page 34)

Also, I'd like to convert an existing app to CI, just so I can conceptualize where in the CI folder structure (and MVC concept) the different pieces go. The app uses .php and .phtml pages (where the .phtml mix logic and presentation - an obvious no-no in MVCland).

The app also uses a mixture of PHP and Javascript, for example it uses dynamic dropdown menus values and visible labels from a database table. It populates the dropdown menu on page load if a value has been set and modifies the value if the dropdown has been changed on submit.

It also uses PHP sessions to set a securitylevel parameter that is checked to show/hide (and limit access to) certain functionality. For example, anything under the "admin/" URI will only be loaded if the session securitylevel is set to admin.

The app also does php includes for files like shared/common_lib.php for functions, etc.

I get that any HTML goes in the Views folder, but not sure where the Javascript goes or how I divide up the existing code to fit into the CI folder hierarchy. Anybody have any guidelines to follow - or do you suggest just starting to rewrite the existing apps functionality from scratch the CI way?

TIA for any opinions you might wish to share.
#2

[eluser]Mirage[/eluser]
I think you want to consider rewriting a lot, because your current application doesn't have a proper separation of logic and presentation.

The .phtml would be in the views folder, that is correct.

Javascripts, images, etc. should go outside of the application in the typcial public locations, e.g. /js and /img. This his what my my public_html folder of a website looks like:
Code:
/application
/assets             <-- This is the root for user modifiable media files e.g. via a CMS
/css
/img                <-- This is for application media files that make up the application
/js
/system
.htaccess
favicon.ico
index.php
robots.txt
#3

[eluser]srajibtechno[/eluser]
Hello CI experts....

I am a new CI developer..starting to build a web based application.
I already created menu using css,php and my sql...but i can not implement it...with codeigniter..I already created model,controller and view files..for this application ..but i can not implement it perfectly ..can u help me...how can i do this??i want to use query1 and query2 in menu view file instead of result and result1(query1 and query2 come from menu_model file)....
I attached my code are shown below..

inbook_links.sql

CREATE DATABASE `inbook_db`;

CREATE TABLE `inbook_links` (
`menu_id` int(9) NOT NULL auto_increment,
`main_menu` varchar(150) collate latin1_general_ci NOT NULL,
`sub_menu` varchar(150) collate latin1_general_ci NOT NULL,
`menu_location` varchar(9) collate latin1_general_ci NOT NULL,
`menu_contents` varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`menu_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ;




menu_model.php (Model file)

class Menu_model extends Model
{
function Menu_model()
{
parent :: Model();
}

function menu_getall()
{
$this->load->database();
$query = $this->db->query("SELECT * FROM inbook_links group by main_menu");
$query2 = $this->db->query("SELECT sub_menu FROM inbook_links");
return $query->result();
}
}

menu.php (Controller File)

class Menu extends Controller
{
function Menu()
{
parent :: Controller();
}

function GetAll()
{
$this->load->model('menu_model');
$data=array();
$data['query']=$this->menu_model->menu_getall();
$data['query2']=$this->menu_model->menu_getall();
$this->load->view('menu_view',$data);
}

}

menu_view.php (view file.)

<div class="mC">

&lt;!-- Main Menu Start --&gt;
&lt;?php
$menuID = 1;
while($val=mysql_fetch_array($result))
{
?&gt;
&lt;div class="mH" onmouseover="toggleMenu('&lt;?php echo 'menu'.$menuID; ?&gt;')"&gt;

</div>

&lt;? $result = mysql_query("SELECT sub_menu FROM inbook_links where main_menu='$row[main_menu]'");

?&gt;
<div id="&lt;?php echo 'menu'.$menuID; ?&gt;" class="mL" style="display:none;">
&lt;?php
while($val2=mysql_fetch_array($result2)){ ?&gt;<a class="mO" href="#">&lt;?php echo $val2['sub_menu']; ?&gt;</a>&lt;? } ?&gt;

</div>


&lt;!-- Main Menu End --&gt;
&lt;?php
$menuID++;
}
?&gt;

</div>

cheers...;-)
Regards.....

SRR
#4

[eluser]Mirage[/eluser]
Well, from an MVC point of view you have a few things in the wrong place. While CI allows it, your view file has way too much business logic in it.

Try this:

1. From the controller call your Model to fetch the menu structure
2. In the Model, run all the queries you need to build up the menu and 'model' the complete result e.g. into a nested array or similar. Return that completed structure to the controller.
3. From the controller pass this menu structure to the view.
4. In the view unroll the data in a loop/nested loop.

The model is where it all happens. The controller is just a traffic cop and the view just a [dumb] presentation of your data.

Cheers!
#5

[eluser]srajibtechno[/eluser]
ok..thank you...here is my modified code...for menu_model.php and menu_view.php
but it is not working properly...please check this and give me solution as possible..




menu_model.php (model file)
&lt;?php

/** Model: Left Menu Model
* @author Syed Rajib Rahman , Date : 17-06-2008
* @copyright 2008
*/

class Menu_model extends Model
{
function Menu_model()
{
parent :: Model();

}

function menu_getall()
{
$this->load->database();
$this->db->select('main_menu');
$this->db->from('inbook_links');
$this->db->group_by("main_menu");
$GLOBALS['query'] = $this->db->get();
return $GLOBALS['query']->result();

}


function sub_menu_getall()
{

$this->load->database();

$sub_menu_array = array();

for ($i=0; $i<count($GLOBALS['query']); $i++)
{
for ($i=0; $i<count($GLOBALS['query']); $i++)
{
foreach ($GLOBALS['query'][$i] as $value)
{
array_push($sub_menu_array,$value);

}
}
}

for ($i=0;$i<count($sub_menu_array); $i++)
{
$this->db->select('sub_menu');
$this->db->from('inbook_links');
$this->db->where('main_menu',$sub_menu_array[$i]);

$query2 = $this->db->get();
return $query2->result();
}



//} //End Foreach
//} // End for

} //End sub_menu_getall()


} // End class


?&gt;

menu.php (controller file)

&lt;?php

/** Controller : Left Menu Controller
* @author Syed Rajib Rahman , Date : 17-06-2008
* @copyright 2008
*/

class Menu extends Controller
{
function Menu()
{
parent :: Controller();
}

function GetAll()
{
$this->load->model('menu_model');
$data=array();
$data['query']=$this->menu_model->menu_getall();
$data['query2']=$this->menu_model->sub_menu_getall();
// $data['query2']=$this->menu_model->menu_getall();
$this->load->view('menu_view_old',$data);
}

}

?&gt;


menu_view.php (view file)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

<div class="mC">

&lt;!-- Main Menu Start --&gt;
&lt;?php
$menuID = 1;

for ($i=0; $i&lt;count($query); $i++)
{
foreach ($query[$i] as $value) { ?&gt;

<div class="mH" onmouseover="toggleMenu('&lt;?php echo 'menu'.$menuID; ?&gt;')"&gt;


</div>

<div id="&lt;?php echo 'menu'.$menuID; ?&gt;" class="mL" style="display:none;">
&lt;?php

foreach($query2 as $row2)
{

?&gt;

&lt;?
} ?&gt;

</div>

&lt;?php
}
$menuID++;
}
?&gt;

</div>

&lt;/body&gt;

&lt;/html&gt;
#6

[eluser]nmweb[/eluser]
Please place your code between [ code ] [ / code ] This is unreadable
#7

[eluser]srajibtechno[/eluser]
ok..thank you...here is my modified code...for menu_model.php and menu_view.php
but it is not working properly...please check this and give me solution as possible..

menu_model.php (model file)

Code:
&lt;?php

/** Model: Left Menu Model
* @author Syed Rajib Rahman , Date : 17-06-2008
* @copyright 2008
*/

class Menu_model extends Model
{
function Menu_model()
{
parent :: Model();

}

function menu_getall()
{
$this->load->database();
$this->db->select('main_menu');
$this->db->from('inbook_links');
$this->db->group_by("main_menu");
$GLOBALS['query'] = $this->db->get();
return $GLOBALS['query']->result();

}


function sub_menu_getall()
{

$this->load->database();

$sub_menu_array = array();

for ($i=0; $i<count($GLOBALS['query']); $i++)
{
for ($i=0; $i<count($GLOBALS['query']); $i++)
{
foreach ($GLOBALS['query'][$i] as $value)
{
array_push($sub_menu_array,$value);

}
}
}

for ($i=0;$i<count($sub_menu_array); $i++)
{
$this->db->select('sub_menu');
$this->db->from('inbook_links');
$this->db->where('main_menu',$sub_menu_array[$i]);

$query2 = $this->db->get();
return $query2->result();
}



//} //End Foreach
//} // End for

} //End sub_menu_getall()

} // End class

?&gt;

menu.php (controller file)

Code:
&lt;?php

/** Controller : Left Menu Controller
* @author Syed Rajib Rahman , Date : 17-06-2008
* @copyright 2008
*/

class Menu extends Controller
{
function Menu()
{
parent :: Controller();
}

function GetAll()
{
$this->load->model('menu_model');
$data=array();
$data['query']=$this->menu_model->menu_getall();
$data['query2']=$this->menu_model->sub_menu_getall();
// $data['query2']=$this->menu_model->menu_getall();
$this->load->view('menu_view',$data);
}

}

?&gt;

menu_view.php (view file)

Code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1” /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

<div class="mC">

&lt;!-- Main Menu Start --&gt;
&lt;?php
$menuID = 1;

for ($i=0; $i&lt;count($query); $i++)
{
foreach ($query[$i] as $value) { ?&gt;

&lt;div class="mH" onmouseover="toggleMenu(’&lt;?php echo 'menu'.$menuID; ?&gt;’)"&gt;

</div&gt;

<div id="&lt;?php echo 'menu'.$menuID; ?&gt;” class="mL" style="display:none;">
&lt;?php

foreach($query2 as $row2)
{

?&gt;

&lt;?
} ?&gt;

</div>

&lt;?php
}
$menuID++;
}
?&gt;

</div>

&lt;/body&gt;

&lt;/html&gt;
#8

[eluser]Mirage[/eluser]
Hi -

Not certain if you're just doing this for testing purposes, but you wouldn't make a specific controller for building a menu.

You're essentially on the right path, though it seems like you have a bit of trouble with passing things around and OO practices in general.

Controller
Code:
class SomeController extends Controller {

    // Contsructor
    function SomeController() {
        parent::Controller();
        // we always need the menu, so we can load that model here
        $this->load->model('MenuModel','',true);
    }


    // show some page
    function index() {
        $menu=$this->MenuModel->buildMenu();
        $this->load->view('index_view',compact('menu');        
    }

}


Model
Code:
class MenuModel extends Model {

    // Contsructor
    function MenuModel() {
        parent::Model();
    }


    // build the entire menu as a nested array
    function buildMenu() {
        $menu=$this->getMainMenu();

        if(!is_array($menu)) {
            return array();
        }

        foreach($menu as $idx=>$name) {
            $menu[$idx]['submenu']=$this->getSubMenu($name);
        }

        return $menu;
    }


    // grab the top level menu
    function getMainMenu() {
        $this->db->select('main_menu as name');
        $this->db->group_by('main_menu');
        $query = $this->db->get('inbook_links');
        return ($query && $query->num_rows()>0 ? $query->result_array() : array());
    }

    // grab a submenu given a parent menu
    function getSubMenu($parent_menu) {
        $this->db->select('sub_menu as name');
        $this->db->where('main_menu', $parent_menu);
        $query=$this->db->get('inbook_links);
        return ($query && $query->num_rows()>0 ? $query->result_array() : array());
    }
}


View
Code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1” /&gt;
        &lt;title&gt;Untitled Document&lt;/title&gt;
    &lt;/head&gt;

    &lt;body&gt;
        <div class="mC">
        &lt;?php foreach($menu as $topId=>$topMenu): ?&gt;
        &lt;div class="mH" onmouseover="toggleMenu(’&lt;?php echo "menu{$topId}" ?&gt;’)"&gt;&lt;?php echo $topMenu['name']?&gt;</div>
            &lt;?php if(!empty($topMenu['submenu'])):?&gt;
            <div id="&lt;?php  echo "menu{$topId}" ?&gt;” class="mL" style="display:none;">
                &lt;?php foreach($topMenu['submenu'] as $subId=> $subMenu): ?&gt;
                <div class="mH">&lt;?php echo $subMenu['name']?&gt;</div>
                &lt;?php endforeach;?&gt;
            &lt;?php endif;?&gt;
        &lt;?php endforeach;?&gt;
        </div>
    &lt;/body&gt;      
&lt;/html&gt;


Code is not tested, but I hope this gets you on your way.

Good luck!
m




Theme © iAndrew 2016 - Forum software by © MyBB