Welcome Guest, Not a member yet? Register   Sign In
parsing values
#1

[eluser]miloneuss[/eluser]
Hi,

i think my question would be very easy to you all, i am using CI 1.7.1, what i want to ask is how do i parsing value in href object to CI controller, i've tried and error, and still not found any way out.
my code :
view =
Code:
<div id="navcont">
    <ul id="nav">
        <li><a href="cmain" id="home">Home</a></li>
      <li><a href="cmain" id="logout">Logout</a></li>
        <li></li>
    </ul>
</div>

controller =
Code:
class Cmain extends Controller {
    function index()
    {
        $this->hitButton();
    }
    
    function hitButton()
    {
        $this->load->library('session');
        $this->load->helper(array('form','url'));
        
        $whatPost = array($this->input->post('btnLogout'), $this->input->post('btnTest'), $this->input->post('logout'));
        foreach($whatPost as $postValue)
        {
            switch ($postValue)
            {
                case 'Logout' :
                    {
                        $this->session->sess_destroy();
                        $data['title'] = 'You\'ve been logout';
                        $data['bodyMessage'] = 'You\'ve been successfully logout from code123 application';
                        $this->load->view('main/logout', $data);                    
                        break;
                    }
             ...

Where I am going wrong ?


miloneuss
#2

[eluser]Mushex Antaranian[/eluser]
Try at first construct your controller Smile

Code:
class Cmain extends Controller {
    function __construct()
    {
       parent::__construct();
    }
//  for PHP 4
//  function Cmain()
//  {
//    parent::Controller();
//  }
    function index()
    {
        $this->hitButton();
    }
    //your hitButton code here
}

If it doesn't help, send me ( jesirobendebua@gmail ) full code of your application, I'll try to find out what is wrong..
#3

[eluser]n0xie[/eluser]
[quote author="miloneuss" date="1241683318"]Hi,

what i want to ask is how do i parsing value in href object to CI controller
[/quote]

Isn't it much easier to do this?

Code:
/**
* View
*/
<a href="/cmain/home">Home</a>
<a href="/cmain/logout">Logout</a>

/**
* Controller cmain
*/
function home()
{
  // show homepageview
}

function logout()
{
  // logout user and show logout view
}
#4

[eluser]Mushex Antaranian[/eluser]
Ooh , sorry, I haven't notice what is in View (I think problem is in controller Smile )
@noxie
I'm doing in way you mentioned, but if @miloneuss want to do it via switch case to keep style of all souce code, try something like
Code:
class Cmain extends Controller {

  function Cmain()
  {
    parent::Controller();
  }
  function index()
  {
  // show homepage view
  }
  // try to not use CamelCase in CI applications
  function hit($value)
  {
    switch ($value)
    {
      case 'logout' :
      {
        // your code here
        break;
      }
      //..
  }
}


in view
Code:
<a href="/cmain/">Home</a>
<a href="/cmain/hit/logout">Logout</a>

Anyway, my advice is to do it in noxie's way )
#5

[eluser]miloneuss[/eluser]
@n0xie, thanks for reply me back. yes i can do that, but i also have to create many controller file, what i want to do is only 1 controller file for 1 view file. i'm try to put it in a condition way.

@mushex, is true i can use
Code:
<a href="/cmain/hit/logout">Logout</a>
and exploded hit as function and logout as varibale value ?
#6

[eluser]Mushex Antaranian[/eluser]
Yes, you can use it, I'm not lying Wink

You can read about it more in User Guide - Passing URI Segments to your Functions




Theme © iAndrew 2016 - Forum software by © MyBB