兩個foreach 怎麼迴圈?

來源:互聯網
上載者:User
我用的某多使用者商城系統,現在想把店鋪一級大類下面的二級類迴圈出來,同時二級大類下面自動讀取當前二級大類中的推薦店鋪,請問該怎麼寫。如:


代碼如下:

           $v){    $i++;  ?>                      

F

    $store){?>
  • 'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank"> " onload="javascript:DrawImage(this,240,240);" />



另外為什麼不是從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);//SEOModel('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">
    " onload="javascript:DrawImage(this,240,240);" />













要看到你的資料!
光看代碼有什麼用?如果代碼就寫錯了呢

經驗證上面的方法不行

模板檔案的代碼是:

       $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"> " onload="javascript:DrawImage(this,240,240);" />



店鋪搜尋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">
    " onload="javascript:DrawImage(this,240,240);" />












  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.