两个foreach 怎么循环? php商城foreach 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的代码不能实现吗?贴出 $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; <?php foreach($output['class_list'] as $k=>$v){ $i++; ?>除非这里只循环一次,你初始化$i=0,为什么会从1开始呢? 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'); } 我现在已经修改了<?php foreach($output['store_list'] as $skey => $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获取,并筛选? <?php echo $k;?> 貌似能获取到当前分类的sc_id 你应该贴出 $output 的内容(至少需要完整的一节),并说明哪个要放在哪里 下面的代码是模板文件的,如何将第一个循环中的$k值传递到第二个循环中的 $where .= "$sc_id = $k"; 件红色部分。3楼我贴出的是PHP的处理文件<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']){ ?> <h2><?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="float:right" target="_blank">更多..</a></h2> <?php if(!empty($output['store_list']) && is_array($output['store_list'])){?> <ul> <?php foreach($output['store_list'] as $skey => $store){ $where .= "$sc_id = $k"; ?> <li><?php echo $k;?><?php echo $store['sc_id'];?><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="<?php echo SiteUrl.'/'.(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> 经验证上面的方法不行,模板文件的代码是:<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><?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="float:right" 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="<?php echo SiteUrl.'/'.(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进行判断!!代码如下:红色部分<?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><?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="float:right" target="_blank">更多..</a></h2> <?php if(!empty($output['store_list']) && is_array($output['store_list'])){?> <ul> <?php foreach($output['store_list'] as $skey => $store){?> <?php if ($Sid == $store['sc_id']) {?> <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="<?php echo SiteUrl.'/'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);?>" onload="javascript:DrawImage(this,240,240);" /></span></a> </li> <?php }?> <?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 }?> ECSHOP求助!! 静态缓存 与编译缓存原理 请高手指点一番 谢谢 思想的实现 不知道怎么样才能替换这些(类似于UBB) 网页被恶意加入游戏链接 怎么解决啊 求:生活网站源码一套,能用的。 php 中bind_param()报错 linux 管理有哪些常见问题 谁有PHP的培训机构的完整视频?求分享!谢谢 学生一个,学习模拟登录正方教务网,有些问题请大家帮帮忙 子站点如何正确显示文章内容 求php代码解密,求高手
echo $i; 在 if ($_GET['cate_id'] == $v['sc_parent_id']){ 分支中有条件执行
因为条件可能不成立,所以 echo $i;就可能不连续
既然 $i 表示一个顺号,那么就应该删除 9 行的 $i++;
而 14 行的 echo $i; 改为 echo ++$i;
$i++;
?>
除非这里只循环一次,你初始化$i=0,为什么会从1开始呢?
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');
}
我现在已经修改了<?php foreach($output['store_list'] as $skey => $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获取,并筛选? <?php echo $k;?> 貌似能获取到当前分类的sc_id
下面的代码是模板文件的,如何将第一个循环中的$k值传递到第二个循环中的 $where .= "$sc_id = $k"; 件红色部分。3楼我贴出的是PHP的处理文件<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']){
?>
<h2><?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="float:right" target="_blank">更多..</a></h2>
<?php if(!empty($output['store_list']) && is_array($output['store_list'])){?>
<ul>
<?php foreach($output['store_list'] as $skey => $store){
$where .= "$sc_id = $k";
?>
<li><?php echo $k;?><?php echo $store['sc_id'];?><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="<?php echo SiteUrl.'/'.(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 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><?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="float:right" 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="<?php echo SiteUrl.'/'.(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;
$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><?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="float:right" target="_blank">更多..</a></h2>
<?php if(!empty($output['store_list']) && is_array($output['store_list'])){?>
<ul>
<?php foreach($output['store_list'] as $skey => $store){?>
<?php if ($Sid == $store['sc_id']) {?>
<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="<?php echo SiteUrl.'/'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);?>" onload="javascript:DrawImage(this,240,240);" /></span></a>
</li>
<?php }?>
<?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 }?>