如何在 codeigniter 中使用嵌套 select 查询以通过每个查询获得不同的结果
how to use nested select query in codeigniter to get different result by each query
`
$poplrRec = mysql_query("SELECT * FROM ".ADD_RECIPE." ORDER BY POPULARITY DESC LIMIT 4");
while($poplrRec1=mysql_fetch_array($poplrRec))
{
$likecount=mysql_query("SELECT RECIPE_ID, COUNT(RECIPE_ID) FROM ".RECIPE_LIKE." WHERE RECIPE_ID=".$poplrRec1['RECIPE_ID']);
while($b=mysql_fetch_array($likecount))
{
$cmnt=mysql_query("SELECT RECIPE_ID, COUNT(RECIPE_ID) FROM ".RECIPE_CMMNT." WHERE RECIPE_ID=".$poplrRec1['RECIPE_ID']." AND TYPE=0");
while($c=mysql_fetch_array($cmnt))
{
}} } `
嗨,我在这里使用核心 php while 循环和 MySQL 查询,所以我必须在 codeigniter 的 MVC 结构中使用这些类型的查询。就像这里一样,我希望每个查询的结果单独用于 mvc 结构。
每个查询都提供一些需要在相关的某个地方使用的数据。
请建议我如何在 codeigniter 或 mvc 结构中实现这些类型的 php 查询。
$result=$this->db->query("select * from tableName");
foreach($result->result() as $row){
// $row has the associative array
// you can again query the database with $this->db->query
}
尝试按照以下格式编写查询
$query = $this->db->get('vehicles_types');
$result = $query->result_array();
// loop through the types e.g. the parents
foreach( $result as $key => $row )
{
// add the "examples" e.g. the children to the result array
$query = $this->db->get_where('vehicles',array('type'=>$row['id']));
$row['examples'] = $query->result_array();
$result[$key] = $row;
}
return $result;
`
$poplrRec = mysql_query("SELECT * FROM ".ADD_RECIPE." ORDER BY POPULARITY DESC LIMIT 4");
while($poplrRec1=mysql_fetch_array($poplrRec))
{
$likecount=mysql_query("SELECT RECIPE_ID, COUNT(RECIPE_ID) FROM ".RECIPE_LIKE." WHERE RECIPE_ID=".$poplrRec1['RECIPE_ID']);
while($b=mysql_fetch_array($likecount))
{
$cmnt=mysql_query("SELECT RECIPE_ID, COUNT(RECIPE_ID) FROM ".RECIPE_CMMNT." WHERE RECIPE_ID=".$poplrRec1['RECIPE_ID']." AND TYPE=0");
while($c=mysql_fetch_array($cmnt))
{
}} } `
嗨,我在这里使用核心 php while 循环和 MySQL 查询,所以我必须在 codeigniter 的 MVC 结构中使用这些类型的查询。就像这里一样,我希望每个查询的结果单独用于 mvc 结构。 每个查询都提供一些需要在相关的某个地方使用的数据。 请建议我如何在 codeigniter 或 mvc 结构中实现这些类型的 php 查询。
$result=$this->db->query("select * from tableName");
foreach($result->result() as $row){
// $row has the associative array
// you can again query the database with $this->db->query
}
尝试按照以下格式编写查询
$query = $this->db->get('vehicles_types');
$result = $query->result_array();
// loop through the types e.g. the parents
foreach( $result as $key => $row )
{
// add the "examples" e.g. the children to the result array
$query = $this->db->get_where('vehicles',array('type'=>$row['id']));
$row['examples'] = $query->result_array();
$result[$key] = $row;
}
return $result;