?
Solved

Multiple methods, one main controller in Codeigniter

Posted on 2016-11-16
6
Medium Priority
?
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: 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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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