CodeIgniter Forums

Full Version: How can I use the function in view of using segments?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
code here
//------------
switch($this->uri->segment(2))
{
case 'login' : return login(); break;
case 'contact' : return contact(); break;
case 'products' : return products(); break;
case 'lang_change' : return lang_change(); break;
}
function login() {
echo "initializing login procedure";
}
function contact() {
echo "initializing cntact form";
}
.............

El Forum

[eluser]TWP Marketing[/eluser]
[quote author="Guner ARIK" date="1304048569"]code here
//------------
switch($this->uri->segment(2))
{
case 'login' : return login(); break;
case 'contact' : return contact(); break;
case 'products' : return products(); break;
case 'lang_change' : return lang_change(); break;
}
function login() {
echo "initializing login procedure";
}
function contact() {
echo "initializing cntact form";
}
.............[/quote]

This should be in your CONTROLLER. for instance:
Code:
...
$segment = $this->uri->segment(2);
switch($segment)
  {
    case 'login':       $str = "initializing login procedure"; break;
    case 'contact':     $str = "initializing contact form"; break;
    case 'products':    $str = "initializing products form"; break;
    case 'lang_change': $str = "initializing language change"; break;
    default:            $str = "Error, Invalid segment: '".$segment."' passed."; break
}
// now pass the var $str to the view for display
...

El Forum

[eluser]Unknown[/eluser]
Thank you for your help, but there is not a value that you receive by $str into the code of a page will be so I'm having trouble. I want to do something to keep each page in a function that function under the direct with switch

El Forum

[eluser]TWP Marketing[/eluser]
I think that I know what you are saying, however it is not "proper" MCV design to have your php code manipulate data in-the-view-file-itself. Yes, it will work, but there are better (simpler and easier to maintain) ways to do this. Views "should" have only display properties, the Controller and Model(s) are where you create the content (using php to massage your data) which is then passed to the view files.

Please re-think the layout of your application, if it's not already a done deal. The User Guide gives a good, basic example of how MCV works.

Please tell me where you have placed the code in your first message, ie; in Controller, Model or View file?

El Forum

[eluser]toopay[/eluser]
As TWP Marketing mention, you should process that in your controller rather than in view. After switch and set the variable, you can send it to your view. It will generates same result with what you have been try!
Code:
// In Controller
$data['init_msg'] = $str;
$this->load->view('some_view',$data);
// Then you can echo it on your 'some_view' with...
<?php echo $init_msg ?>