简体   繁体   English

如何使用codeIgniter在PHP中使用控制器访问三个视图页面?

[英]How do I access three view pages with controller in PHP with codeIgniter?

I am attempting to build a website where the user will input his/her first and last name and the database will store the data. 我正在尝试建立一个网站,用户将在其中输入他/她的名字和姓氏,数据库将存储数据。 Also, there will also be an option where the user can choose to add data or view all the records. 另外,还有一个选项,用户可以选择添加数据或查看所有记录。 Now my problem is that whenever I access http://localhost/assign9/ which is my index, only signup.php shows up then will not store data after I click submit. 现在我的问题是,每当我访问作为索引的http:// localhost / assign9 /时 ,仅显示signup.php,然后单击提交后将不存储数据。 How do I make the code as if the 3 codes will work in a sequence? 如何使代码像3个代码将按顺序工作一样? signContr.php(controller for assign9) class signContr extends CI_Controller{ signContr.php(assign9的控制器)类signContr扩展了CI_Controller {

public function index(){
$this->load->view('signup');
}
public function insert(){
$this->load->view('saveinfo');
}
public function display(){
$this->load->view('displayinfo');
}
 public function _construct(){
      parent::_construct();
      $this->load->helper('url');
     }
}

signup.php signup.php

    <h1>Sign in</h1>
    <div class="container">
        <div class="sign-up-content">
            <form method="POST" class="signup-form">

                <div class="form-textbox">
                    <label for="fname">First Name:</label>
                    <input type="text" name="fname" id="fname" />
                </div>
                <div class="form-textbox">
                    <label for="lname">Last Name:</label>
                    <input type="text" name="lname" id="lname" />
                </div>

                <div class="form-textbox">
                    <input type="submit" name="submit" id="submit" class="submit" value="Create account" />
                </div>
            </form>
        </div>
    </div>
</div>

saveinfo.php saveinfo.php

$fname = filter_input(INPUT_POST, 'fname');
$lname= filter_input(INPUT_POST, 'lname');
if (!empty($fname)||!empty($lname)){
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "information";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
 die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
  $sql = "INSERT Into name(fname,lname) values('$fname','$lname')";

 if ($conn->query($sql)) {
  echo '"New record inserted sucessfully"  <br>';

  echo '<a href=\assign9\application\views\displayinfo.php>'
  . 'Click here to view all usernames and passwords</a>';
 } else {
  echo "Error!";
 }

 $conn->close();
}
} else {
 echo "All fields are required";
 die();
}
?>
}

?>

displayinfo.php displayinfo.php

<body>
<h1>Records</h1>
<br><br><br>
<table>
    <tr>

        <th>First Name</th>
        <th>Last Name</th>
    </tr>

    <?php

        $conn = mysqli_connect("localhost", "root","", "information");

        if($conn->connect_error){
            die("Connection failed: " .$conn->connect_error);
        }


        $result = mysqli_query($conn,"SELECT * FROM name");

        if($result->num_rows>0){
            while($row = mysqli_fetch_array($result)){
                echo
                "<tr><td>".  $row["fname"].
                "</td><td>". $row["lname"].
                "</td></tr>";
            }
            echo "</table>";

        } else{
                echo "0 result";
            }
        $conn->close();
    ?>
</table>
<br><br>
<input type="button" value="Add Data"                    
 onclick="window.location.href='http://localhost/assign9/'"/>
</body>
</html>

Before addressing your problem, I'd really think you would benefit from reading a bit about how MVC works and the first chapters of CodeIgniter's user guide. 在解决您的问题之前,我真的认为您将从阅读有关MVC的工作原理和CodeIgniter用户指南的第一章中受益。 I'll clear a lot of doubts for you. 我会为您消除很多疑问。

Having said that, there's a lot of issues in your code: 话虽如此,您的代码中存在很多问题:

1.- constructor methods are much better placed at the top of the controller (before any user methods) 2.- your main index() method is invoking views it doesn't need. 1.-构造器方法最好放在控制器的顶部(在任何用户方法之前)2.-您的主要index()方法正在调用不需要的视图。 If you need to display the form, just invoke $this->load-view('signup'); 如果需要显示表单,只需调用$this->load-view('signup'); Views should have all the logic for storing data on a database (that's between the controller and a model) 视图应该具有将数据存储在数据库中的所有逻辑(在控制器和模型之间)

3.- your signup view creates a form but doesn't have any action defined ( <form method="post" action="controller/method"> would be advisable) 4.- the form should point to a different method (not index() ) which would take the user input, validate it and process it by inserting it in the database. 3.-您的注册视图创建一个表单,但未定义任何操作(建议使用<form method="post" action="controller/method"> )4.-表单应指向其他方法(不是index() ),它将接受用户输入,对其进行验证并通过将其插入数据库进行处理。 For example, if the form points to 'signcontr/process', you'd define: 例如,如果表单指向“ signcontr / process”,则需要定义:

public function process()
{
    // code that validates and processes user input
}

After having the process() method do its work, you may invoke a confirmation view ( $this->load->view('signup_confirmation'); for example) which displays the result of the user action. process()方法完成其工作之后,您可以调用确认视图(例如$this->load->view('signup_confirmation'); ),该$this->load->view('signup_confirmation');显示用户操作的结果。

There's a lot that could be written about your code. 关于您的代码,可以写很多东西。 I strongly suggest to follow the CI user guide (it has basic examples that'll allow you to get a better grasp of how CI can make your life easier) and rewrite your code accordingly 我强烈建议您遵循CI用户指南(其中包含一些基本示例,可以使您更好地理解CI如何使您的生活更轻松)并相应地重写代码

well first we need to understand what is MVC 首先,我们需要了解什么是MVC

  • Model 模型
  • View 视图
  • Controller 调节器

means if you are going to make a form and on submit, data should be saved into database. 意味着如果您要制作表格并提交,则数据应保存到数据库中。

so here you need to define few stuff. 所以在这里您需要定义一些东西。

like codeigniter have few available libraries / helpers - database (update to your credentials) - form (load on all form related pages) 像codeigniter这样的库/助手很少-数据库(更新您的凭据)-表单(在所有与表单相关的页面上加载)

Automatically your database is accessible in your model so no need to connect again. 自动在模型中访问数据库,因此无需再次连接。

so your first page localhost/assign9 所以你的第一页localhost/assign9

in your routes.php , you have to define default router. routes.php ,您必须定义默认路由器。 which will be like: 就像这样:

$route['default_controller'] = 'welcome/index'; $ route ['default_controller'] ='欢迎/索引'; // here welcome is controller name and index is function/method name //这里的欢迎是控制器名称,索引是函数/方法名称

now in views, create folder of controller's name welcome and then create file index.php which is your view file means your all html or php related code will be here. 现在在视图中,创建控制器名称的目录welcome ,然后创建文件index.php ,这是您的视图文件,意味着所有与html或php相关的代码都将在此处。

Here you need to use Codeigniter's Helper FORM and create form using that. 在这里,您需要使用Codeigniter的Helper表单并使用该表单创建表单。 No need to define form manually one by one elements. 无需一一手动定义表单。 Use Form, it will be easy and quick. 使用表格,这将是容易和快速的。

now, in controller use same function or new to process form data. 现在,控制器中使用相同的功能或新功能来处理表格数据。 you may add various conditions like form submit or not, empty or not etc 您可以添加各种条件,例如是否提交表单,是否为空等

now in Models, create a model file of your database table name and define table name, primary key, null allowed etc and unique. 现在在Models中,创建数据库表名称的模型文件,并定义表名称,主键,允许的null等,并且是唯一的。

Codeigniter already have feature to use model's setting and insert or it will return an error. Codeigniter已经具有使用模型的设置并插入的功能,否则它将返回错误。

So use that :) 所以用那个:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量从控制器传递到Codeigniter中的视图? - How do I pass a variable from the controller to the view in Codeigniter? 如何将视图变量传递给Codeigniter中的控制器? - how do i pass a view variable to the controller in codeigniter? 如何从CodeIgniter中的视图访问控制器方法? - How to access a controller method from view in CodeIgniter? 如何将PHP代码添加到Codeigniter视图文件中? - How Do I add PHP code to my Codeigniter view file? PHP - Yii - 我可以在视图文件中访问模型的数据,但在其控制器中有getUrl。 我如何访问这个? - PHP - Yii - I can access model's data in a view file, but have the getUrl in its controller. How do i access this? codeigniter-如何删除http:// localhost / social / pages / envi-&gt;在social / application / controller中页面为php的页面 - codeigniter - how to remove http://localhost/social/pages/envi --> pages where pages is php in social/application/controller 我无法在Codeigniter的视图中访问控制器阵列 - I cant access my controller array in my view in codeigniter 我如何在我的控制器文件中访问数组以查看Codeigniter中的文件 - how can i access an array in my controller file to view file in codeigniter 如何通过 controller 访问 CakePHP 1.3 视图,以使 URL 路由符合应用程序/控制器/视图 - How do I access a CakePHP 1.3 view via its controller such that the URL route conforms to application/controller/view 如何将控制器信息传递给PHP(无框架)视图? - How do I pass information my a controller to a view in PHP (no framework)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM