php - 通过结果内的变量拉入row_array()内的列
问题描述
因此,我正在尝试验证一列并检查数量是否足以继续。我遇到的问题是我正在尝试使用变量分配列名,因此
public function check_resc($data){
$id = $this->session->userdata('char_id');
$resc = trim($data['chng_resc']);
$qty = trim($data['quantity']);
$prov = trim($data['prov']);
$ress = ucfirst($resc);
$query = $this->db->select('*')
->from('character_resources')
->where('char_id', $id)
->get();
$result = $query->row_array();
if($result[$ress] >= $qty){
return true;
} else {
$this->session->set_flashdata('error', 'You do not have enough resources.'.$result[0].' / '.$qty);
redirect(base_url('providence/view/'.$prov));
return false;
}
}
例如,检查他们是否有足够的“铁”,我试图访问 $result[$ress] >= $qty。但是我不能分配列试图以这种方式提取结果。如何在 $result[] 中输入变量才能拉出该列?我尝试过 select($ress),但比使用 $result[0] 没有帮助。大括号 {} 也已被弃用,所以我需要一个替代方法来完成这项工作。
解决方案
你好,你可以尝试使用我在下面写的codeigniter查询吗?
如果你使用 row_array 它只会返回第一行让你知道
$this->db-where('char_id', $id);
$query = $this->db->get('character_resources');
$result = $query->row_array();
// print_r($result);
if($result['the name of the column as it's showing after printing the results '] >= $qty){
return true;
}
您可以查看此 url 以了解有关 codeigniter 框架中的 sql 查询的更多信息 https://codeigniter.com/userguide3/database/query_builder.html
正如国王所说:
你想要strtolower($resc)
-ucwords()
是UpperCaseWords
- 那么$result[$ress]
应该工作我希望你发现这个答案有帮助
推荐阅读
- python - 使用python将范围从多个工作簿复制到主工作簿中的新工作表
- android - 将视图转换为位图 (OutOfMemoryException)
- azure - 使用临时凭据的 Azure 跨租户身份验证
- reactjs - 如何将 React 表单中的多个文件上传到 Flask API?
- webrtc - WebRTC:setConfiguration 不是函数
- cypress - 如何检查赛普拉斯的跨度节点内不存在伪元素(:: after)?
- c++ - G++ 链接一个仅标头 Boost 库,用于使用终端在 MAC 上进行发布模式编译
- php - 将 php 中的嵌套关联数组转换为 CSV 文件
- javascript - 如何在 jQuery whit“selectpicker”类中添加选项以选择列表?
- r - R - 基于时间间隔标记行