【发布时间】:2015-02-16 11:50:36
【问题描述】:
我刚刚开始接触 CodeIgniter 框架,并且还在习惯模型-控制器-视图设置。
我有一个名为“Customer”的控制器类,它包含以下我需要访问的代码:
function loadCustomer(){
if(!empty($_POST)){
$result['Success'] = 0;
$user = $this->User->LoadUser($_POST['UserID']);
if($user){
$result['Success'] = 1;
$user->Package = $this->User->GetPrimaryPackage($user->UserID);
$user->AccountAlerts = $this->User->GetAccountAlertOptions($user->UserID);
$_SESSION['User'] = $user;
$_SESSION['UserLoggedIn'] = true;
$result['User'] = $user;
$result['UserLoggedIn'] = true;
}
echo json_encode($result);
}
else
return null;
}
名为“用户”的模型包含以下内容:
function LoadUser($UserID = null){
if(!$UserID){
trigger_error("No userid supplied.", E_USER_ERROR);
exit();
}
$result = $this->db->query("SELECT * FROM mydb.tblUsers where UserID='$UserID';")->row();
if(!empty($result)){
return $result;
}
else{
return null;
}
}
我正在使用以下代码,我需要能够访问 Customer 中的 "loadCustomer()" 函数,以便我可以根据用户 ID 执行自动登录:
<?php
$con = mysqli_connect('localhost','root','root','mydb');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"mydb");
$sql = "SELECT * FROM incoming_calls";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
if (!empty($row)) {
$number = $row['phone_number'];
}
}
$sql = "SELECT Username, UserID, Name
FROM tblUsers
WHERE PhoneHome='$number' OR PhoneCell='$number' OR PhoneWork='$number'";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result)) {
$userArray[] = array("name" => $row['Name'], "username" => $row['Username'], "user_id" => $row['UserID']);
}
if (!empty($userArray)) {
echo json_encode($userArray);
}
mysqli_close($con);
?>
我该怎么做,非常感谢任何帮助?
【问题讨论】:
-
阅读或至少阅读 codeigniter 教程:codeigniter.com/user_guide/tutorial/index.html 接下来阅读有关 Active Record 的数据库调用 codeigniter.com/user_guide/database/active_record.html 并生成查询结果 codeigniter.com/user_guide/database/results.html
标签: php codeigniter mysqli