0

So I am trying to pass data from my controller to my model which will than query the database and return the results back to the controller, then the controller will send them to the view so that they can be displayed however I keep getting a blank page.

This is what my Controller code looks like:

 class WelcomePageController extends CI_Controller {

 public function __construct()
   {
        parent::__construct();
        $this->load->model('guestsearchmodel');
        $this->load->helper('url');

   }

  public function index()
   {

    $this->load->view('WelcomePageView');

   }
 public function SearchQuestion()
 {
       $SearchTerm = $this->input->post('GSearch');

       /* Checks to see if a field has been left blank if so then it 
        * will show and error
        */
       if ($SearchTerm == null||$SearchTerm == '')
       {

           $this->load->view('WelcomePageView');

           /* This is a little script that displays an alert 
            * box on screen when a field has been left blank. 
            */
           echo '<script type="text/javascript">
           window.onload = function () { 
               alert("No Search Terms Have Been Entered Please Try Again."); 
           }
           </script>'; 
           return false;
       }

       /* This will call the AddNewUser function from the Model*/
       $Term = $this->guestsearchmodel->GuestSearch($SearchTerm);
       $data['term'] = $Term;

       $this->load->view('GuestSearchResultsView',$data);
 }

And this is the code from my model: class guestsearchmodel extends CI_Model {

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

function GuestSearch($Term)
{

    $results = "SELECT * FROM Questions WHERE Question Title LIKE '%".$this->db->escape_like_str($Term)."%'";
    $data = $results->row_array();
    $results->free_result();

    return $data;
}

And for reference this is code from my view but none of it displays, it's just a blank screen:

<html>
<head>
<title> My Website </title>
<link rel="stylesheet" href="<?php echo base_url();?>Assets/CSS/SampleCSS.css" type="text/css" />   
</head>

<body>

 <header>
 <ul>
 <li><a href="#Login">Login</a></li>
 <li><a href="<?php echo base_url();?>index.php/RegisterPageController">Register</a></li>
 <li><a href="<?php echo base_url();?>index.php/WelcomePageController">Home</a></li>
 </ul>
 </header>


 <!--This is my Welcome Page View which was loaded by the WelcomePage Controller -->
 <h1> Welcome to my Advanced Web Coursework! </h1>
 <hr>

 <p>The Contents of my Guest Search Result are yet to be decided.</p>
 <!--<?php echo $term['Question Title']?>-->
 <footer>
 Details about my Website
 </footer>

P.S: Sorry for any other mistakes or inefficient code, as this is just very early stages of development. Also I am not the best of programmers unfortunately.

EDIT: After Some testing I have discovered that when I add This Code:

$results = "SELECT * FROM Questions WHERE 'Question Title' LIKE '%".$this->db->escape_like_str($Term)."%'";
$data = $results->row_array();
$results->free_result();

return $data;

The page just loads as a blank page, therefore something in this part of the code must be breaking it.

11
  • You'll need to check the network traffic for errors. Are you using error reporting in CodeIgniter? What browser are you developing in? Commented Dec 10, 2014 at 19:16
  • I was checking for errors but it didn't come up with any, unless I did it incorrectly and I am using Firefox. Commented Dec 10, 2014 at 19:25
  • Urgh Firefox - On Firefox press 'CTRL + SHIFT + i' to bring up the console. Look under Network when your page is loaded, the errors may be appearing there. Commented Dec 10, 2014 at 19:29
  • oh wow, your right I got an 500 Internal Server Error after it asked me to refresh the page for detailed activity. Commented Dec 10, 2014 at 19:35
  • Sounds about right - does it give any PHP errors within the 500 error? Commented Dec 10, 2014 at 19:37

2 Answers 2

0

You don't appear to be actually running the query.

Try something like the following

$sql = "SELECT * FROM Questions WHERE 'Question Title' LIKE '%".$this->db->escape_like_str($Term)."%'";

$results = $this->db->query($sql);
$data = $results->row_array();
$results->free_result();

return $data;
Sign up to request clarification or add additional context in comments.

2 Comments

However the Query is returning an empty array which makes me wonder, why this statement is not working. it should be returning all matches however it returns nothing.
You might try running echo $this->db->last_query(); to see what the query run against the database was and then try that against the database outside of your application.
0

I believe there may be an error in your SQL query.

"SELECT * FROM Questions WHERE Question Title LIKE '%foo%'"

Here, your field appears to have a space in its name;

... WHERE Question Title LIKE ...
                  ^--- space

In this case, the correct syntax would be:

... WHERE `Question Title` LIKE ...

EDIT: For MySQL, at least.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.