两个foreach 怎么循环?
我用的某多用户商城系统,现在想把店铺一级大类下面的二级类循环出来,同时二级大类下面自动读取当前二级大类中的推荐店铺,请问该怎么写。如下图:
代码如下:
<div class="louceng"> <?php if(!empty($output['class_list']) && is_array($output['class_list'])){ $i = 0; ?> <?php foreach($output['class_list'] as $k=>$v){ $i++; ?> <div class="floor_area"> <?php if ($_GET['cate_id'] == $v['sc_parent_id']){ ?> <h2 id="php-nbsp-echo-nbsp-i-F-nbsp-nbsp-php-nbsp-echo-nbsp-v-sc-name"><?php echo $i;?>F <?php echo $v['sc_name'];?></h2> <?php if(!empty($output['store_list']) && is_array($output['store_list'])){?> <ul> <?php foreach($output['store_list'] as $skey => $store){?> <li><a href="<?php echo ncUrl(array('act'=>'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank"><span class="thumb size72"> <img src="/static/imghw/default1.png" data-src="<?php echo SiteUrl.'/'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);? alt="两个foreach 怎么循环?" >" class="lazy" /'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);?>" onload="javascript:DrawImage(this,240,240);" /></span></a> </li> <?php }?> </ul> <div class="clear"></div> <?php }?> <?php }elseif (!isset($v['child']) && $output['class_list'][$_GET['cate_id']]['sc_parent_id'] == $v['sc_parent_id']){?> <h2><?php echo $v['sc_name'];?></h2> <?php }?> </div> <?php }?> <?php }?></div>
另外为什么不是从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 更多..
-
" class="lazy" /'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);?>" onload="javascript:DrawImage(this,240,240);" />
$store){
$where .= "$sc_id = $k";
?>
要看到你的数据!
光看代码有什么用?如果代码就写错了呢
经验证上面的方法不行,
模板文件的代码是:
<div class="louceng"> <?php if(!empty($output['class_list']) && is_array($output['class_list'])){ $i = 0; ?> <?php foreach($output['class_list'] as $k=>$v){ ?> <div class="floor_area"> <?php if ($_GET['cate_id'] == $v['sc_parent_id']){ $Sid= $k; ?> <h2 id="php-nbsp-echo-nbsp-i-F-nbsp-php-nbsp-echo-nbsp-v-cate-id-nbsp-php-nbsp-echo-nbsp-v-sc-name-a-nbsp-href-php-nbsp-echo-nbsp-ncUrl-array-act-shop-search-cate-id-k-nbsp-style-max-width-nbsp-nbsp-target-blank-更多-a"><?php echo ++$i;?>F <?php echo $v['cate_id'];?> <?php echo $v['sc_name'];?><a href="<?php echo ncUrl(array('act'=>'shop_search','cate_id'=>$k));?>" style="max-width:90%" target="_blank">更多..</a></h2> <?php if(!empty($output['store_list']) && is_array($output['store_list'])){?> <ul> <?php foreach($output['store_list'] as $skey => $store){?> <li><?php echo $Sid;?><a href="<?php echo ncUrl(array('act'=>'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank"><span class="thumb size72"> <img src="/static/imghw/default1.png" data-src="<?php echo SiteUrl.'/'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);? alt="两个foreach 怎么循环?" >" class="lazy" /'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);?>" onload="javascript:DrawImage(this,240,240);" /></span></a> </li> <?php }?> </ul> <div class="clear"></div> <?php }?> <?php }elseif (!isset($v['child']) && $output['class_list'][$_GET['cate_id']]['sc_parent_id'] == $v['sc_parent_id']){?> <h2><?php echo $v['sc_name'];?></h2> <?php }?> </div> <?php }?> <?php }?></div>
店铺搜索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){
?>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











종종 키워드와 추적 매개 변수로 혼란스러워하는 긴 URL은 방문자를 방해 할 수 있습니다. URL 단축 스크립트는 솔루션을 제공하여 소셜 미디어 및 기타 플랫폼에 이상적인 간결한 링크를 만듭니다. 이 스크립트는 개별 웹 사이트 a에 유용합니다

Instagram은 2012 년 Facebook에서 유명한 인수에 이어 타사 사용을 위해 두 개의 API 세트를 채택했습니다. Instagram Graph API 및 Instagram Basic Display API입니다. 개발자는

Laravel은 직관적 인 플래시 방법을 사용하여 임시 세션 데이터 처리를 단순화합니다. 응용 프로그램에 간단한 메시지, 경고 또는 알림을 표시하는 데 적합합니다. 데이터는 기본적으로 후속 요청에만 지속됩니다. $ 요청-

이것은 Laravel 백엔드가있는 React Application을 구축하는 데있어 시리즈의 두 번째이자 마지막 부분입니다. 이 시리즈의 첫 번째 부분에서는 기본 제품 목록 응용 프로그램을 위해 Laravel을 사용하여 편안한 API를 만들었습니다. 이 튜토리얼에서는 Dev가 될 것입니다

Laravel은 간결한 HTTP 응답 시뮬레이션 구문을 제공하여 HTTP 상호 작용 테스트를 단순화합니다. 이 접근법은 테스트 시뮬레이션을보다 직관적으로 만들면서 코드 중복성을 크게 줄입니다. 기본 구현은 다양한 응답 유형 단축키를 제공합니다. Illuminate \ support \ Facades \ http를 사용하십시오. http :: 가짜 ([ 'google.com'=> 'Hello World', 'github.com'=> [ 'foo'=> 'bar'], 'forge.laravel.com'=>

PHP 클라이언트 URL (CURL) 확장자는 개발자를위한 강력한 도구이며 원격 서버 및 REST API와의 원활한 상호 작용을 가능하게합니다. PHP CURL은 존경받는 다중 프로모토콜 파일 전송 라이브러리 인 Libcurl을 활용하여 효율적인 execu를 용이하게합니다.

고객의 가장 긴급한 문제에 실시간 인스턴트 솔루션을 제공하고 싶습니까? 라이브 채팅을 통해 고객과 실시간 대화를 나누고 문제를 즉시 해결할 수 있습니다. 그것은 당신이 당신의 관습에 더 빠른 서비스를 제공 할 수 있도록합니다.

2025 PHP Landscape Survey는 현재 PHP 개발 동향을 조사합니다. 개발자와 비즈니스에 대한 통찰력을 제공하는 프레임 워크 사용, 배포 방법 및 과제를 탐색합니다. 이 조사는 현대 PHP Versio의 성장을 예상합니다
