当前位置:Gxlcms > PHP教程 > 两个foreach怎么循环?

两个foreach怎么循环?

时间:2021-07-01 10:21:17 帮助过:7人阅读

我用的某多用户商城系统,现在想把店铺一级大类下面的二级类循环出来,同时二级大类下面自动读取当前二级大类中的推荐店铺,请问该怎么写。如下图:


代码如下:

           $v){		  		  $i++;		  ?>          	      	      

F

    $store){?>
  • 'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank">



另外为什么不是从1开始?


回复讨论(解决方案)

你的代码不能实现吗?贴出 $output 的内容

$i++; 在 foreach($output['class_list'] as $k=>$v){ 循环中无条件执行
echo $i; 在 if ($_GET['cate_id'] == $v['sc_parent_id']){ 分支中有条件执行
因为条件可能不成立,所以 echo $i;就可能不连续

既然 $i 表示一个顺号,那么就应该删除 9 行的 $i++;
而 14 行的 echo $i; 改为 echo ++$i;

$v){

$i++;
?>
除非这里只循环一次,你初始化$i=0,为什么会从1开始呢?

你的代码不能实现吗?贴出 $output 的内容

$i++; 在 foreach($output['class_list'] as $k=>$v){ 循环中无条件执行
echo $i; 在 if ($_GET['cate_id'] == $v['sc_parent_id']){ 分支中有条件执行
因为条件可能不成立,所以 echo $i;就可能不连续

既然 $i 表示一个顺号,那么就应该删除 9 行的 $i++;
而 14 行的 echo $i; 改为 echo ++$i;

class shop_listControl extends BaseHomeControl {	/**	 * 店铺列表	 */	public function indexOp(){		/**		 * 读取语言包		 */		Language::read('home_store_class_index');		$lang	= Language::getLangContent();		//店铺类目快速搜索		$class_list = ($h = F('store_class')) ? $h : H('store_class',true,'file');		if (!key_exists($_GET['cate_id'],$class_list)) $_GET['cate_id'] = 0;		Tpl::output('class_list',$class_list);		//店铺搜索		$model = Model();		$condition = array();		$keyword = trim($_GET['keyword']);		if(C('fullindexer.open') && !empty($keyword)){			//全文搜索			$condition = $this->full_search($keyword);		}else{			if ($keyword != ''){				$condition['store_name|store_zy'] = array('like','%'.$keyword.'%');			}			if ($_GET['user_name'] != ''){				$condition['member_name'] = trim($_GET['user_name']);			}		}		if (!empty($_GET['area_id'])){			$condition['area_id'] = array('in',$this->getAreaNextId(intval($_GET['area_id'])));		}		if ($_GET['cate_id'] > 0){			$child = array_merge((array)$class_list[$_GET['cate_id']]['child'],array($_GET['cate_id']));			$condition['sc_id'] = array('in',$child);		}		$condition['store_state'] = 1;		if (!in_array($_GET['order'],array('desc','asc'))){			unset($_GET['order']);		}		if (!in_array($_GET['key'],array('store_sales','store_credit'))){			unset($_GET['key']);		}		if(is_null($_GET['key'])){			$order = 'store_sort asc';		}else{			$order = $_GET['key'].' '.$_GET['order'];		}        if (isset($condition['store.store_id'])){            $condition['store_id'] = $condition['store.store_id'];unset($condition['store.store_id']);        }        $model_store = Model('store');//        $store_list = $model_store->where($condition)->order($order)->page(10)->select();  20140408 bak        		$store_list = $model_store->where('store_recommend=1')->order($order)->page(10)->select();        //获取店铺商品数,推荐商品列表等信息        $store_list = $model_store->getStoreSearchList($store_list);		Tpl::output('store_list',$store_list);		Tpl::output('show_page',$model->showpage(2));				//当前位置		if (intval($_GET['cate_id']) > 0){			$nav_link[1]['link'] = 'index.php?act=shop_list';			$nav_link[1]['title'] = $lang['site_search_store'];			$nav =$class_list[$_GET['cate_id']];			//如果有父级			if ($nav['sc_parent_id'] > 0){				$tmp = $class_list[$nav['sc_parent_id']];				//存入父级				$nav_link[] = array(					'title'=>$tmp['sc_name'],					'link'=>"index.php?act=shop_list&cate_id=".$nav['sc_parent_id']				);			}			//存入当前级			$nav_link[] = array(				'title'=>$nav['sc_name']			);		}else{			$nav_link[1]['link'] = 'index.php';			$nav_link[1]['title'] = $lang['homepage'];			$nav_link[2]['title'] = $lang['site_search_store'];		}		Tpl::output('nav_link_list',$nav_link);		//SEO		Model('seo')->type('index')->show();		Tpl::output('html_title',(empty($_GET['keyword']) ? '' : $_GET['keyword'].' - ').C('site_name').$lang['nc_common_search']);		Tpl::showpage('shop_list');	}

你的代码不能实现吗?贴出 $output 的内容

$i++; 在 foreach($output['class_list'] as $k=>$v){ 循环中无条件执行
echo $i; 在 if ($_GET['cate_id'] == $v['sc_parent_id']){ 分支中有条件执行
因为条件可能不成立,所以 echo $i;就可能不连续

既然 $i 表示一个顺号,那么就应该删除 9 行的 $i++;
而 14 行的 echo $i; 改为 echo ++$i;

$i 已经正常了,非常感谢。麻烦帮忙看看读取子分类下面推荐店铺该如何实现。偶是新手,还望海涵

你的代码不能实现吗?贴出 $output 的内容

$i++; 在 foreach($output['class_list'] as $k=>$v){ 循环中无条件执行
echo $i; 在 if ($_GET['cate_id'] == $v['sc_parent_id']){ 分支中有条件执行
因为条件可能不成立,所以 echo $i;就可能不连续

既然 $i 表示一个顺号,那么就应该删除 9 行的 $i++;
而 14 行的 echo $i; 改为 echo ++$i;

我现在已经修改了 $store)的内容

$condition['store_state'] = 1;
$condition['store_recommend'] = 1;
$condition['sc_id'] = 66;

$store_list = $model_store->where($condition)->order($order)->page(10)->select();

现在可以读取sc_id=66的店铺,但是如何实现当前分类ID获取,并筛选? 貌似能获取到当前分类的sc_id

你应该贴出 $output 的内容(至少需要完整的一节),并说明哪个要放在哪里

你应该贴出 $output 的内容(至少需要完整的一节),并说明哪个要放在哪里

下面的代码是模板文件的,如何将第一个循环中的$k值传递到第二个循环中的 $where .= "$sc_id = $k"; 件红色部分。

3楼我贴出的是PHP的处理文件


$i = 0;
?>

$v){
?>

?>

F 'shop_search','cate_id'=>$k));?>" style="float:right" target="_blank">更多..





    $store){
    $where .= "$sc_id = $k";
    ?>

  • 'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank">














要看到你的数据!
光看代码有什么用?如果代码就写错了呢

经验证上面的方法不行

模板文件的代码是:

       $v){		  ?>          	      	      

F 'shop_search','cate_id'=>$k));?>" style="float:right" target="_blank">更多..

    $store){?>
  • 'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank">



店铺搜索PHP的代码是

//店铺搜索		$model = Model();		$condition = array();		$keyword = trim($_GET['keyword']);		if(C('fullindexer.open') && !empty($keyword)){			//全文搜索			$condition = $this->full_search($keyword);		}else{			if ($keyword != ''){				$condition['store_name|store_zy'] = array('like','%'.$keyword.'%');			}			if ($_GET['user_name'] != ''){				$condition['member_name'] = trim($_GET['user_name']);			}		}		if (!empty($_GET['area_id'])){			$condition['area_id'] = array('in',$this->getAreaNextId(intval($_GET['area_id'])));		}		if ($_GET['cate_id'] > 0){			$child = array_merge((array)$class_list[$_GET['cate_id']]['child'],array($_GET['cate_id']));			$condition['sc_id'] = array('in',$child);		}        $storeid=array($Sid);		$condition['store_state'] = 1;		$condition['store_recommend'] = 1;		if (!in_array($_GET['order'],array('desc','asc'))){			unset($_GET['order']);		}		if (!in_array($_GET['key'],array('store_sales','store_credit'))){			unset($_GET['key']);		}		if(is_null($_GET['key'])){			$order = 'store_sort asc';		}else{			$order = $_GET['key'].' '.$_GET['order'];		}        if (isset($condition['store.store_id'])){            $condition['store_id'] = $condition['store.store_id'];unset($condition['store.store_id']);        }        $model_store = Model('store');//        $store_list = $model_store->where($condition)->order($order)->page(10)->select();  20140408 bak        		$store_list = $model_store->where($condition)->order($order)->page(10)->select();        //获取店铺商品数,推荐商品列表等信息        $store_list = $model_store->getStoreSearchList($store_list);		Tpl::output('store_list',$store_list);		Tpl::output('show_page',$model->showpage(2));


麻烦帮忙看看如何循环出当前分类下的推荐店铺,现在的关键是如果将模板中第一个foreach的$k 传递到第二个foreach中进行筛选。筛选的关键代码是
$condition['store_state'] = 1;
$condition['store_recommend'] = 1;

查询是要按按显示的要求进行的
你只要打印出查询得到的最终数据,就可知道是否符合显示的要求

二次开发的话,可以调用系统栏目的函数啊,不用自己写

自己解决了,用了一个很笨的办法 用if进行判断!!代码如下:红色部分

$i = 0;
?>

$v){
?>

$Sid= $k;
?>

F 'shop_search','cate_id'=>$k));?>" style="float:right" target="_blank">更多..





    $store){?>

  • 'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank">













人气教程排行