Solved

Multiple methods, one main controller in Codeigniter

Posted on 2016-11-16
6
19 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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
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 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Debra Turner
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
You're welcome!
1
 

Author Closing Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now