Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

Multiple methods, one main controller in Codeigniter

Hi Chris,
Thank you for your help on the last question!

I had this question after viewing Printing unique/distinct values PHP, MSSQL, Codeigniter.

In my main application Controller of 'post', I have the original method of 'show' (dropdown of faculty names)
casweb.memphis.edu/suapp-research/index.php/post/show

I also have another method of 'by_area'
casweb.memphis.edu/suapp-research/index.php/post/by_area (dropdown of research areas)

If I go to: 'http://casweb.memphis.edu/suapp-research/index.php/post/show', I get both methods, but the 'by_area' gives an error of:
Undefined Index for Research_Area and Research_Area_Prefix.

If I go to: http://casweb.memphis.edu/suapp-research/index.php/post/by_area (it works as it should)

How can I view them both through the main controller of 'post'? Or can I? I will have at least one more method for 'post' if possible.

Post.php Main Controller
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Post extends CI_Controller {
    
    public function show() {
                        
        
        $this->load->helper('form');
        $this->load->model('Post_faculty_model');  //load model
        $post_faculty = $this->Post_faculty_model->get_posts();


        $data = array();
        $data['rows'] = $post_faculty;
        $this->load->view('post_view', $data);
    }

    public function by_area() {
                        
        
        $this->load->helper('form');
        $this->load->model('Post_research_area_model');  //load model
        $post_area = $this->Post_research_area_model->get_areas();


        $data = array();
        $data['rows'] = $post_area;
        $this->load->view('post_area_view', $data);
    }
    
    
    
}

Open in new window

Debra
0
Debra Turner
Asked:
Debra Turner
  • 3
  • 3
1 Solution
 
Marco GasiFreelancerCommented:
It looks like Post_research_area_model->get_areas() returns all required data whereas Post_faculty_model->get_posts() returns only data for faculty posts. Can you post here the code of these 2 functions and the code of the 2 views involved?
1
 
Debra TurnerWeb Specialist IIAuthor Commented:
Thank you Marco,

Both are pulling the correct information and displaying it in the browser, but the final product I would like is to have them called from the one Post.php (which is the main controller).

Is that possible?
http://casweb.memphis.edu/suapp-research/index.php/post/show

Post_faculty_model.php
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Post_faculty_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_posts() {
        $this->load->database();

        $query = $this->db->query("SELECT First_Name, Last_Name, Faculty_UUID, Department_Name, Faculty_Email, Faculty_Title FROM suapp.FACULTY ORDER by Department_Name, Last_Name ASC");

       $rows = $query->result_array();
        return $rows;

    }

}

Open in new window


Post_research_area_model.php
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Post_research_area_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function get_areas() {
        $this->load->database();

        $query = $this->db->query("SELECT Research_Area_Prefix, Research_Area FROM suapp.RESEARCH_AREA");

       $rows = $query->result_array();
        return $rows;

    }

}

Open in new window


post_area_view.php
<html>
    <head>
        <title></title>
    </head
    <body>
        
<h2>Search By Research Area</h2>

        <?php
        /* @var $this type */

        print "<table>";

        echo form_open('Match_research_area_controller/match_area_now');
        echo form_hidden('select', 'Research_Area_Prefix');

        if (isset($select) && $select != "") {
            $select = $_POST['Research_Area_Match'];
        }
        ?>
        <select name="Research_Area_Match">
            <?php
            foreach ($rows as $row) {
                ?>
                <option value="<?php print $row['Research_Area_Prefix']; ?>"><?php print $row['Research_Area']; ?></option>

                <?php
// End while loop.
            }
            ?>
        </select>
        <input type="submit" name="submit" value="Research Area Match">
    </form>

    
    

Open in new window


post_view.php
<html>
    <head>
        <title></title>
    </head
    <body>
        <h2>Search by Faculty Member</h2>

        <?php
        /* @var $this type */

        print "<table>";

        echo form_open('Match_faculty_research_controller/match_it_now');
        echo form_hidden('select', 'Faculty_UUID');

        if (isset($select) && $select != "") {
            $select = $_POST['Faculty_Research_Match'];
        }
        ?>
        <select name="Faculty_Research_Match">
            <?php
            foreach ($rows as $row) {
                ?>
                <option value="<?php print $row['Faculty_UUID']; ?>"><?php print $row['First_Name']; ?>&nbsp;<?php print $row['Last_Name']; ?>,&nbsp;<?php print $row['Department_Name']; ?> </option>

                <?php
// End while loop.
            }
            ?>
        </select>
        <input type="submit" name="submit" value="Match Faculty Research">
    </form>
    
    
<hr />
<h2>Search By Research Area</h2>

        <?php
        /* @var $this type */

        print "<table>";

        echo form_open('Match_research_area_controller/match_area_now');
        echo form_hidden('select', 'Research_Area_Prefix');

        if (isset($select) && $select != "") {
            $select = $_POST['Research_Area_Match'];
        }
        ?>
        <select name="Research_Area_Match">
            <?php
            foreach ($rows as $row) {
                ?>
                <option value="<?php print $row['Research_Area_Prefix']; ?>"><?php print $row['Research_Area']; ?></option>

                <?php
// End while loop.
            }
            ?>
        </select>
        <input type="submit" name="submit" value="Research Area Match">
    </form>

    
    

Open in new window


Any help is greatly appreciated!
Debra
0
 
Marco GasiFreelancerCommented:
Ok. In both your functions you are using $data['rows'] to grab the data from model. You have to differntiate those data
    public function show() {
        $this->load->helper('form');

        $this->load->model('Post_faculty_model');  //load model
        $post_faculty = $this->Post_faculty_model->get_posts();

        $this->load->model('Post_research_area_model');  //load model
        $post_area = $this->Post_research_area_model->get_areas();

        $data['posts'] = $post_faculty;
        $data['areas'] = $post_area;
        $this->load->view('show_view', $data);
    }

Open in new window


Then you have to change post_view in order to manage posts and areas instead of rows:
<html>
    <head>
        <title></title>
    </head
    <body>
        <h2>Search by Faculty Member</h2>

        <?php
        /* @var $this type */

        print "<table>";

        echo form_open('Match_faculty_research_controller/match_it_now');
        echo form_hidden('select', 'Faculty_UUID');

        if (isset($select) && $select != "") {
            $select = $_POST['Faculty_Research_Match'];
        }
        ?>
        <select name="Faculty_Research_Match">
            <?php
            foreach ($posts as $row) {
                ?>
                <option value="<?php print $row['Faculty_UUID']; ?>"><?php print $row['First_Name']; ?>&nbsp;<?php print $row['Last_Name']; ?>,&nbsp;<?php print $row['Department_Name']; ?> </option>

                <?php
// End while loop.
            }
            ?>
        </select>
        <input type="submit" name="submit" value="Match Faculty Research">
    </form>
    
    
<hr />
<h2>Search By Research Area</h2>

        <?php
        /* @var $this type */

        print "<table>";

        echo form_open('Match_research_area_controller/match_area_now');
        echo form_hidden('select', 'Research_Area_Prefix');

        if (isset($select) && $select != "") {
            $select = $_POST['Research_Area_Match'];
        }
        ?>
        <select name="Research_Area_Match">
            <?php
            foreach ($areas as $row) {
                ?>
                <option value="<?php print $row['Research_Area_Prefix']; ?>"><?php print $row['Research_Area']; ?></option>

                <?php
// End while loop.
            }
            ?>
        </select>
        <input type="submit" name="submit" value="Research Area Match">
    </form>

Open in new window


Notice that you don't need to initialize $data array: this is done by CodeIgniter automatically :)
1
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Debra TurnerWeb Specialist IIAuthor Commented:
Thanks Marco,
Now I get it! And it's working. Thanks so much for your help :)

I have truly learned so much from this site.
debra
0
 
Marco GasiFreelancerCommented:
You're welcome!
1
 
Debra TurnerWeb Specialist IIAuthor Commented:
Sorry to be so late in assigning this as Best Choice and Closing Question. I appreciate all the help!
Debra
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now