我需要使用另一页上的数据

时间:2019-03-30 14:52:01

标签: php codeigniter

我想使用同一控制器中的功能加载另一个视图。我是codeIgniter的新手,所以请放轻松:D

默认情况下加载的主页->从主页上填写的字段->提交->使用数据库中的ajax抓取的数据->通过将新视图加载为默认值在同一控制器中来使用另一页上的数据。

控制器:

<?php

class logInCon extends CI_Controller
{

    public function login()
    {

        //$data['creds'] = $this->accsModel->getUser();
        $this->load->view("Login");

    }


    public function validate_LogIn()
    {
        $uname = $this->input->post('uname');
        $pass = $this->input->post('pass');
        $this->load->model("accsModel");
        $data = $this->accsModel->logInCheck($uname, $pass);
        $check = $data['verified'];

        if ($check <= 0)
        {
            echo "Not a valid member";
        }else
            {
                $data['logged'] = $this->accsModel->getUser($uname, $pass);
                $this->load->view('comms.php');//want this to load
            }
    }


}


?>

型号

<?php

    /**

     */
    class accsModel extends CI_Model
    {

        public function getBps()
        {
            $query = $this->db->get('bps');
            return $query->result();
        }

        public function getUser($uname, $psswrd)
        {
            $getCreds = $this->db->query("SELECT `bps`.*, `users`.bps_id 
                                            FROM `bps` 
                                            LEFT JOIN `users` 
                                            ON `bps`.id = `users`.bps_id 
                                            AND `users`.`uname` = '$uname' 
                                            AND `users`.`pwd` ='$psswrd'
                                            WHERE `users`.bps_id != ''
                                           OR `users`.bps_id IS NOT NULL");
            return $getCreds->result();
        }

        public function logInCheck($uname, $psswrd)
        {
            $this->db->select('COUNT(*) AS verified');
            $this->db->where('uname', $uname);
            $this->db->where('pwd', $psswrd);
            $this->db->limit(1);
            return $this->db->get('users')->row_array();
        }

    }

?>

查看

    <body>


    <div class="container">

        <div class="row">

            <div class="LogForm">
                <div class="col-md-12">
                    <input type="text" id="bpCode" name="bpCode">
                </div>
                <div class="col-md-12">
                    <input type="password" id="pass" name="pass">
                </div>
                <div class="btn">
                <span id="emptyF" hidden style="color:red;"></span>
                <button id="submit" class="btn btn-info btn-lg btn-block">Login</button>
                <span id="result"></span>
                </div>

            </div>

        </div>

    </div>


    <script type="text/javascript">

        $(document).ready(function() 
        {
            $("#submit").click(function()
            {
                var uname = $('#bpCode').val();
                var pass = $('#pass').val();

                if(uname == "" || pass =="")
                {
                    $("#emptyF").attr("hidden", false).html("Please fill in the form");
                }else{
                    $("#emptyF").attr("hidden", true);

                    $.ajax({//start of ajax function

                      type: "POST",
                      url: "<?php echo base_url('logInCon/validate_LogIn'); ?>",
                      data: 
                            {
                                uname : uname,
                                pass : pass
                            }

                    //end of ajax function
                    });

                }



            });
        });

    </script>
    </body>

View comms希望加载它并使用getUser()函数中的数据

    <body>
        <p><?php print_r($logged) ?></p>
        <h1>Commissions View</h1>
        <table style="width:100%">

            <?php

                foreach ($logged as $lgdIn) {
            ?>

            <tr>
                <th>ID</th>
                <th>bpID</th>
                <th>pc</th>
                <th>up</th>
                <th>tmLdr</th>
                <th>fName</th>
                <th>mName</th>
                <th>lName</th>
                <th>contactNo</th>
                <th>Status-Id</th>
                <th>bpclass_id</th>
            </tr>

            <tr>
                <td><?php echo $lgdIn->id; ?></td>
                <td><?php echo $lgdIn->bpID; ?></td>
                <td><?php echo $lgdIn->pc; ?></td>
                <td><?php echo $lgdIn->up; ?></td>
                <td><?php echo $lgdIn->tmLdr; ?></td>
                <td><?php echo $lgdIn->fName; ?></td>
                <td><?php echo $lgdIn->mName; ?></td>
                <td><?php echo $lgdIn->lName; ?></td>
                <td><?php echo $lgdIn->contactNo; ?></td>
                <td><?php echo $lgdIn->status_id; ?></td>
                <td><?php echo $lgdIn->bpclass_id; ?></td>
            </tr>

            <?php


                }

            ?>

        </table>

    </body>

1 个答案:

答案 0 :(得分:0)

您需要在加载视图中传递$ data,就像下面的代码一样

# Use a set instead of a (silly) dict
# Use a descriptive variable name
suites = {'hearts', 'clubs', 'diamonds', 'spades'}

# Can't use an assignment in while
while True:
    # Use a descriptive variable name
    suite = input("Pick a Suite: ")

    # Use "in" for checking membership
    # ("=" was doubly wrong; equality comparison is "==")
    if suite in suites:
        break
    # else
    print('{0} is not a suite'.format(suite))

print('Valid suite')
# ... More code here

视图中的变化也很小

public function validate_LogIn()
    {
        $uname = $this->input->post('uname');
        $pass = $this->input->post('pass');
        $this->load->model("accsModel");
        $data = $this->accsModel->logInCheck($uname, $pass);
        $check = $data['verified'];

        if ($check <= 0)
        {
            echo "Not a valid member";
        }else{
            $data['logged'] = $this->accsModel->getUser($uname, $pass);
            $this->load->view('comms',$data);//want this to load
        }
    }