代码:
<?php
$courses = ',BTech,MTech,PHD-Sci,PHD-Engg,';
$course = explode(",", $courses);
foreach ($course as $rows)
{
$this->db->select('*');
$this->db->from('course_master');
$where = "course_short_name = '".$row['courses']."'";
$this->db->where($where);
$query = $this->db->get();
echo $this->db->last_query();
}
?>
输出:
我是ci的新人。在这段代码中,我使用explode函数,并希望运行查询,例如select * from course_master where course_short_name =',BTech,'select * from course_master where course_short_name =',MTech,'就像这样。那么,我该怎么做?请帮忙。
谢谢
答案 0 :(得分:0)
这将是一项正在进行的工作......
第一项:爆炸。它简单的PHP
当你不确定某事时 - 玩它(代码)并看看你得到了什么。即。
$courses = 'BTech,MTech,PHD-Sci,PHD-Engg';
$course = explode(",", $courses);
// DEBUG - What am I actually Getting before I
// unleash this upon the rest of my code...
var_dump($course);
这将生成您期望的数组......
array (size=4)
0 => string 'BTech' (length=5)
1 => string 'MTech' (length=5)
2 => string 'PHD-Sci' (length=7)
3 => string 'PHD-Engg' (length=8)
请注意,我的$课程列表不包含前导和尾随。 (逗号)和你一样,不知道为什么......
第二项:在循环中进行多次选择,您打算如何使用结果?
所以问题是 - 你的期待是什么,因为你的图像有多个选择它...
答案 1 :(得分:0)
试试这个:
$courses = ',BTech,MTech,PHD-Sci,PHD-Engg,';
$course = explode(",", $courses);
foreach ($course as $rows)
{
$this->db->select('*');
$this->db->from('course_master');
$this->db->where("course_short_name", $rows);
$query = $this->db->get();
echo $this->db->last_query();
}
答案 2 :(得分:0)
执行此操作非常简单,只需计算数组长度就是示例
<?php
$courses = 'BTech,MTech,PHD-Sci,PHD-Engg';
$course = explode(",", $courses);
for ($i=0;$i<count($course);$i++)
{
$this->db->select('*');
$this->db->from('course_master');
$where = "course_short_name = '".$course[$i]."'";
$this->db->where($where);
$query = $this->db->get();
echo $this->db->last_query();
}
?>
在这里你需要删除&#39;,&#39;从$ courses的开始和结束开始,否则explode将在数组中生成两个空元素。
这里$ course [$ i]将逐个转动每个元素,这样你的查询将针对数组中可用的每个元素运行。
答案 3 :(得分:0)
使用带有'or_where'条件的单一选择
<?php
$courses = array('BTech','MTech','PHD-Sci','PHD-Engg');
$this->db->select('*');
$this->db->from('course_master');
foreach ($courses as $rows)
{
$this->db->or_where('course_short_name',$rows);
}
$query = $this->db->get();
$query->array_result();
?>