Solved

Multiple methods, one main controller in Codeigniter

Posted on 2016-11-16
6
49 Views
Last Modified: 2016-11-29
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
Comment
Question by:Debra Turner
  • 3
  • 3
6 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41891081
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
 

Author Comment

by:Debra Turner
ID: 41891524
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
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41891664
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:Debra Turner
ID: 41891773
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41891776
You're welcome!
1
 

Author Closing Comment

by:Debra Turner
ID: 41906224
Sorry to be so late in assigning this as Best Choice and Closing Question. I appreciate all the help!
Debra
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question