CodeIgniter Forums

Full Version: How to call another controller?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Near[/eluser]
Hi, i'm a new user of code igniter. i was wondering if i can call another controller aside from the one declared at the routes.php? because when i try to call another controller the page cannot be found...

thanks..

sorry for bad english.. cheers!

El Forum

[eluser]John_Betong_002[/eluser]
It is not straight forward and far easier to inherit from a common library.

Try searching for "MY_Controller".
 
 

El Forum

[eluser]Near[/eluser]
is there any way to do this? this is the scenario.. when i click a button in a view naamed ocd_view it calls a function located at the default controller named ocd_controller. the controller loads another view. named highAndLow_view and when i click a button it calls another controller named hourlyheights_controller. but when i do that the page looks at the ocd_controller istead of looking at the hourlyheights_controller...

please help. thanks

sorry for bad english..thanks

El Forum

[eluser]Near[/eluser]
is there any easy way to do this? this is the scenario.. when i click a button in a view named ocd_view it calls a function located at the default controller named ocd_controller. the controller loads another view. named highAndLow_view and when i click a button it calls another controller named hourlyheights_controller. but when i do that the page looks at the ocd_controller istead of looking at the hourlyheights_controller...

please help. thanks

sorry for bad english..thanks

El Forum

[eluser]John_Betong_002[/eluser]
Try this:

Code:
// http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
  $this->load->helper('url');

  <?php
        // ocd_view
        echo anchor
        (
           'ocd_controller/func_FIRST/' .$params,
           'func_FIRST located ocd_controller'
        };
  ?>        
  
// the ocd_controller controller loads another view. named highAndLow_view
// and when i click a button
// it calls another controller named hourlyheights_controller.
  <?php
      // highAndLow_view
      echo anchor
      (
        'hourlyheights_controller/func_TWO/' .$params,
        'func_TWO located in hourlyheights_controller'
      };
  ?>


 
edit renamed incorrect link.
 

El Forum

[eluser]Near[/eluser]
thanks.. that takes care of the different controller problem .. my new problem is why i get stucked at the highAndLow view when im sending a parameter as a third segment of the uri?

the page loads but the uri looks like this...

example.com/tides/ocd_controller/highAndLow_view/hourlyMonth

instead of

example.com/tides/ocd_controller/hourlyMonth

El Forum

[eluser]John_Betong_002[/eluser]
With the information you provide I will try three guesses.


1. Are you using the anchor function?

2. Have you set $config['base_url']?

3. Have you checked the script against another link that is working?


I am not very good at guessing and reckon it will be far better if you supply the script.
 
 

El Forum

[eluser]Near[/eluser]
yes im using anchor tags and my $config['base_url'] are set to "http://localhost/Oceano";

heres my code
_________________________________

TIDES CONTROLLER

<?php

Class Tides_Controller extends CI_Controller {

function __construct()
{

parent::__construct();

$this->load->model('tides_model');
$this->load->helper('form');
$this->load->helper('html');

}

function Index()
{

$this->load->view('Index');

}


//get the list of distinc stations in the database
function getStation()

{

$data['query1'] = $this->tides_model->getStation();
$this->load->view('main',$data);

}

function hourlyQuery()
{

$this->load->view('hourlyHeights');

}

function hourlyMonth()
{

$month = $_POST['month'];
$year = $_POST['year'];
$datum = $_POST['datum'];
$hidden = $_POST['hidden'];
$key = $_POST['key'];

$date = $year."-".$month;

$data2['query2'] = $this->tides_model->getMonthly($date,$datum,$hidden);

$data2['key'] = $key;
$data2['datum'] = $datum;

$this->load->view('/viewers/hourly_viewer',$data2);

}


}

?>

__________________________________________

TIDES_MODEL

class Tides_model extends CI_Model {

function Events_model(){
parent::__construct();
}

function getStation(){

$query1 = $this->db->query('SELECT DISTINCT location FROM proc_tides');

return $query1;

}

function getMonthly($date,$datum,$hidden){

$query2 = $this->db->query("SELECT height FROM proc_tides where date_p like '$date%' and location='$hidden'");
return $query2;

}

function getHourlyYear($date,$location,$datum){
$query4 = $this->db->query("SELECT height FROM proc_tides where date_p like'$date%' and location ='$location'");
return $query4;

}

}
_____________________________________


INDEX (VIEW)

<?php echo anchor('tides_controller/getStation','Query Center')?>

<br />
<br />

&lt;?php echo anchor('tides_controller/call_database_main','Database'); ?&gt;

&lt;/body&gt;
&lt;/html&gt;
__________________________________

MAIN (VIEW)

&lt;html&gt;
&lt;form action="category_view" method="post"&gt;
<select name="station">

&lt;?php
foreach ($query1->result() as $row)
{
?&gt;

<option>&lt;?php echo $row->location; ?&gt;</option>

&lt;?php
}
?&gt;
</select>
&lt;input type="submit" name="submit" value="SUBMIT"&gt;
&lt;/form&gt;
&lt;/html&gt;
_______________________

CATEGORY_VIEW

&lt;?php
echo anchor('tides_controller/hourlyQuery/'.$station,'HOURLY HEIGHTS');
?&gt;

&lt;?php
echo anchor('highlow_controller/test/' .$station, 'HIGH AND LOW TIDES');
?&gt;

_____________________________

HOURLY_HEIGHTS (VIEW)

&lt;?php $station = $this->uri->segment(3)?&gt;
&lt;?php echo 'Enter your Query for'.' '.$station?&gt;

&lt;html&gt;
<h1>Monthly Query</h1>

&lt;form action="hourlyMonth" method="post"&gt;

<label for="year">Enter Year</label>
&lt;input type="text" name="year"&gt;

<label for="month">Enter month</label>
&lt;input type="text" name="month"&gt;&lt;br><br>

<label for="datum">Datum</label>
&lt;input type="text" name="datum" value="0"&gt;&lt;br>

&lt;input type="submit" value="Submit Query"&gt;
&lt;input type="hidden" name="hidden" value="&lt;?php echo $station;?&gt;"&gt;
&lt;input type="hidden" name="key" value="1"&gt;


&lt;/form&gt;


my problem is when i click the submit button at hourly_Heights the page is loaded in the same page?

El Forum

[eluser]John_Betong_002[/eluser]
Try this:

Code:
&lt;form action=“Tides_Controller/hourlyMonth” method=“post”&gt;
&nbsp;
Please also note that the script I submitted is wrapped in code tags and makes reading the code easier.
Try wrapping your script in code tags and notice the difference.
&nbsp;
&nbsp;

El Forum

[eluser]Near[/eluser]
ok sir.. thanks for your help. i will try this when i arrive our house. im only at computer shop..


i'm not an english speaking person. sorry for my poor english.
Pages: 1 2