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
Solved

Multiple methods, one main controller in Codeigniter

Posted on 2016-11-16
6
54 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to count occurrences of each item in an array.

792 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