在我的数据库中,我有一个状态列,我在其中使用数字来表示产品的状态。这些数字中的每一个都代表一个字符串值,例如 1 是打开的,2 是关闭的,等等。现在为了在我的网页中显示这些状态的计数,我将以下数字转换为字符串以将它们显示给用户:
array(1=>'Open',8=>'Hot',2=>'Closed',3=>'Transacted',4=>'Dead',9=>'Follow Up',11=>'Working')
现在我将这个计数作为一个可点击的链接,用户可以在其中单击计数并将他们带到一个显示该项目详细信息的新页面。为此,我正在使用它:
<a target='_blank' href='".site_url('reports/viewall?status=' . $status)."'>".$num."</a>
这正确地将参数传递给 URL 并显示 http://localhost/reports/viewall?status=Closed
现在我将这个变量作为 $status 存储在我的 Controller 中,但是我不能在我的模型查询中使用这个字符串值,这是因为它给出状态的字符串值而不是与之相关的数字:
SELECT * from TABLE1 where status = $status
我存储 $status 的 Controller 类:
$status = $this->input->get('status');
$content['individualleads'] = $this->leads_model->get_all_leads($status )->result();
最佳答案
我认为最好的方法是在 URL 中传递整数,并且仅当它是针对用户的文本时才使用数组文本值。所以只是:
http://localhost/reports/viewall?status=2
另一种方法是翻转数组并访问文本键:
$status_num = array_flip($array)[$status];
或者搜索原始数组:
$status_num = array_search($status, $array);
可能更标准的可能是有一个 status_types
表,其中包含每个状态的整数 status_id
和文本 status_text
并在检索状态。
关于php - 将字符串数组与数字匹配以获取数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69666437/