将数据库结果转换为codeigniter中的数组

时间:2016-10-13 13:13:41

标签: php database codeigniter

我是codeigniter的新手,我正在尝试创建一个具有admin,moderator和user角色的登录身份验证系统。

我的问题是我试图在'role'上设置条件,这是数据库中的一列。但我不知道如何使用和比较列角色的值。

我编写了以下代码,但收到错误:

  

[严重性:错误消息:在数组上调用成员函数result_array()]

我的代码是:

import mongoose from 'mongoose';
import cuid from 'cuid';
import timestamps from 'mongoose-timestamp';
import validators from 'mongoose-validators';

const Schema = mongoose.Schema;

const tabAccessSchema = new Schema({
  entityid: { type: 'String', required: true },
  tabs: [{ type: 'String' }],
});

const departmentsSchema = new Schema({
  entitytid: { type: 'String', required: true },
  deptid: { type: 'String', required: true },
});

const userSchema = new Schema({
  auth0id: { type: 'String', required: true },
  cuid: { type: 'String', default: cuid(), required: true },
  dateAdded: { type: 'Date', default: Date.now, required: true },
  email: { type: 'String', required: true, validate: validators.isEmail() },
  avatarURL: { type: 'String', required: true, validate: validators.isURL() },
  username: { type: 'String' },
  accessLevel: { type: 'Number', required: true },
  entities: [{ type: 'String', required: true }],
  preferredEntity: { type: 'String', required: true },
  tab_access: [tabAccessSchema],
  departments: [departmentsSchema],
});

userSchema.plugin(timestamps);

export default mongoose.model('User', userSchema);

3 个答案:

答案 0 :(得分:0)

在模型中

$res = $this->db->get(); 
return $res->result_array();

然后申请

foreach($answer as $a)
{
  var_dump( $a );
} 

使用$a获取值

答案 1 :(得分:0)

View root = findViewById(R.id.root_main);

int[] xy = new int[2];
root.getLocationOnScreen(xy);

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
int devHeight = displayMetrics.heightPixels;

int viewHeight = root.getHeight();

Toast toast = Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG);
int yOffset = Math.abs(xy[1] - devHeight + viewHeight) + toast.getYOffset();
toast.setGravity(Gravity.BOTTOM, 0, yOffset);
toast.show();

使用此结果转换为数组格式

答案 2 :(得分:0)

您正在调用模型中的->result(),然后尝试从控制器调用结果数组。

从模型中的->result();移除return $query->result();或从控制器中删除->result_array()

调用脚本并使用->get()后,您一次只能调用其中一个结果。如果您可能希望在同一查询结果上调用->result()->num_rows等不同的结果参数,则只需将查询返回给控制器并在控制器中使用->result()

希望这有助于澄清一些事情。

MODEL

class user extends CI_Model
{ 
  function __construct()
  { 
    parent::__construct(); 
  } 

  function usergetdata( $username,$userpass,$role )
  { 
    $this->db->select(); 
    $this->db->from( 'user' ); 
    $this->db->where( 'username',$username ); 
    $this->db->where( 'userpass',$userpass ); 
    $query= $this->db->get(); 
    if( $query->num_rows() == 1 )
    { 
      return $query; 
    }
    else
    { 
      return false; 
    } 
  } 
}

CONTROLLER

$answer = $this->user->usergetdata( $username,$userpass,'admin' );   
$data   = $answer->result_array();