两个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 Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

HTTP请求方法包括GET、POST、PUT和DELETE,分别用于获取、提交、更新和删除资源。1.GET方法用于获取资源,适用于读取操作。2.POST方法用于提交数据,常用于创建新资源。3.PUT方法用于更新资源,适用于完整更新。4.DELETE方法用于删除资源,适用于删除操作。

在PHPOOP中,self::引用当前类,parent::引用父类,static::用于晚静态绑定。1.self::用于静态方法和常量调用,但不支持晚静态绑定。2.parent::用于子类调用父类方法,无法访问私有方法。3.static::支持晚静态绑定,适用于继承和多态,但可能影响代码可读性。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。
