代码点火器在将数据从控制器传递到视图时返回空白页

时间:2014-12-10 19:09:51

标签: php html codeigniter variables

所以我试图将数据从我的控制器传递到我的模型,这将比查询数据库并将结果返回给控制器,然后控制器将它们发送到视图,以便它们可以显示但是我一直在一页空白。

这是我的Controller代码:

 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);
 }

这是我模型中的代码:     class guestsearchmodel扩展了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;
}

作为参考,这是我视图中的代码,但没有显示,它只是一个空白屏幕:

<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:对于任何其他错误或效率低下的代码感到抱歉,因为这只是开发的早期阶段。不幸的是,我不是最好的程序员。

编辑: 经过一些测试后,我发现当我添加 此代码

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

return $data;

该页面只是作为空白页面加载,因此这部分代码中的某些内容必须打破它。

2 个答案:

答案 0 :(得分:0)

我相信您的SQL查询可能存在错误。

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

此处,您的字段名称中似乎有空格;

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

在这种情况下,正确的语法是:

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

编辑:对于MySQL,至少。

答案 1 :(得分:0)

您似乎没有实际运行查询。

尝试类似以下内容

$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;