首页 > 解决方案 > 通过结果内的变量拉入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] 没有帮助。大括号 {} 也已被弃用,所以我需要一个替代方法来完成这项工作。

标签: phpmysqlcodeigniter

解决方案


你好,你可以尝试使用我在下面写的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]应该工作我希望你发现这个答案有帮助


推荐阅读