How do two foreach loops?

Source: Internet
Author: User
Tags php foreach
How do two foreach loops? I am using a multi-user mall system. now I want to cycle the second-level classes under the first-level category of the store, and the second-level category will automatically read the recommendation shops in the current second-level category. how can I write this. For example:


The code is as follows:

$v){ $i++; ?>

F

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



In addition Why not start from 1?


Reply to discussion (solution)

Cannot your code be implemented? Paste $ output Content

$ I ++; execute unconditionally in the foreach ($ output ['class _ list'] as $ k => $ v) {loop
Echo $ I; conditional execution in if ($ _ GET ['Cate _ id'] = $ v ['SC _ parent_id']) {branch
Because the conditions may not be true, echo $ I; may not be consecutive.

Since $ I represents a shun number, you should delete the $ I ++ of nine rows;
Change the echo $ I of 14 rows to echo ++ $ I;

$ V ){

$ I ++;
?>
Unless this is a loop once, why do you start from 1 when initializing $ I = 0?

Cannot your code be implemented? Paste $ output Content

$ I ++; execute unconditionally in the foreach ($ output ['class _ list'] as $ k => $ v) {loop
Echo $ I; conditional execution in if ($ _ GET ['Cate _ id'] = $ v ['SC _ parent_id']) {branch
Because the conditions may not be true, echo $ I; may not be consecutive.

Since $ I represents a shun number, you should delete the $ I ++ of nine rows;
Change the echo $ I of 14 rows to echo ++ $ I;

Class shop_listControl extends BaseHomeControl {/*** shop list */public function indexOp () {/*** read Language pack */Language: read ('Home _ store_class_index '); $ lang = Language: getLangContent (); // Quick Search for shop Categories $ 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); // shop search $ model = Model (); $ condition = array (); $ keyword = trim ($ _ GET ['keyword']); if (C ('fullindexer. open ')&&! Empty ($ keyword) {// Full-text search $ 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 _ credentials '))) {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)-> orde R ($ order)-> page (10)-> select (); 20140408 bak $ store_list = $ model_store-> where ('store _ recommend = 1 ') -> order ($ order)-> page (10)-> select (); // Obtain the number of items in the store, recommended item list and other information $ store_list = $ model_store-> getStoreSearchList ($ store_list); Tpl: output ('store _ list', $ store_list); Tpl :: output ('show _ page', $ model-> showpage (2); // current position 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 there is a parent level if ($ nav ['SC _ parent_id']> 0) {$ tmp = $ class_list [$ nav ['SC _ parent_id']; // save to parent $ nav_link [] = array ('title' => $ tmp ['SC _ name'], 'link' => "index. php? Act = shop_list & cate_id = ". $ nav ['SC _ parent_id']);} // store the current level $ 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 ');}

Cannot your code be implemented? Paste $ output Content

$ I ++; execute unconditionally in the foreach ($ output ['class _ list'] as $ k => $ v) {loop
Echo $ I; conditional execution in if ($ _ GET ['Cate _ id'] = $ v ['SC _ parent_id']) {branch
Because the conditions may not be true, echo $ I; may not be consecutive.

Since $ I represents a shun number, you should delete the $ I ++ of nine rows;
Change the echo $ I of 14 rows to echo ++ $ I;

$ I is normal. thank you very much. Please help us to see how the recommended store can implement the sub-category reading. I'm a newbie, but also hope Haihan

Cannot your code be implemented? Paste $ output Content

$ I ++; execute unconditionally in the foreach ($ output ['class _ list'] as $ k => $ v) {loop
Echo $ I; conditional execution in if ($ _ GET ['Cate _ id'] = $ v ['SC _ parent_id']) {branch
Because the conditions may not be true, echo $ I; may not be consecutive.

Since $ I represents a shun number, you should delete the $ I ++ of nine rows;
Change the echo $ I of 14 rows to echo ++ $ I;

I have modified it now. $ Store) content

$ Condition ['store _ state'] = 1;
$ Condition ['store _ recommend'] = 1;
$ Condition ['SC _ id'] = 66;

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

Now we can read shops with SC _id = 66. but how can we obtain and filter the current Category ID? It seems that the SC _id of the current category can be obtained.

You should post the $ output Content (at least a complete section) and describe where to put it.

You should post the $ output Content (at least a complete section) and describe where to put it.

The following code is the template file. how to pass the $ k value in the first loop to the $ where. = "$ SC _id = $ k"; in the second loop is the red part.

On the third floor, I posted PHP processing files.


$ I = 0;
?>

$ V ){
?>


?>
F 'Shop _ search', 'Cate _ id' => $ k);?> "Style =" float: right "target =" _ blank "> More ..



    $ Store ){
    $ Where. = "$ SC _id = $ k ";
    ?>

  • 'Show _ store', 'id' => $ store ['store _ id']), 'store', $ store ['store _ domain ']);?> "Title =" "target =" _ blank ">
    "Onload =" javascript: DrawImage (this, 240,240); "/>













To see your data!
What is the use of code? What if the code is wrong?

The method on the experience certificate is not feasible.,

The template file code is:

$ V) {?>

F 'Shop _ search', 'Cate _ id' => $ k);?> "Style =" float: right "target =" _ blank "> More ..

    $ Store) {?>
  • 'Show _ store', 'id' => $ store ['store _ id']), 'store', $ store ['store _ domain ']);?> "Title =" "target =" _ blank ">" onload = "javascript: DrawImage (this, 240,240);"/>



The shop search PHP code is

// Shop search $ model = Model (); $ condition = array (); $ keyword = trim ($ _ GET ['keyword']); if (C ('fullindexer. open ')&&! Empty ($ keyword) {// Full-text search $ 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 _ credentials '))) {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 (); // Obtain the number of items in the store and the list of recommended items. $ store_list = $ model_store-> getStoreSearchList ($ store_list); Tpl :: output ('store _ list', $ store_list); Tpl: output ('show _ page', $ model-> showpage (2 ));


Please help us to see how to loop through the recommendation shops under the current category. The key is to transfer the $ k of the first foreach in the template to the second foreach for filtering. The key code for filtering is
$ Condition ['store _ state'] = 1;
$ Condition ['store _ recommend'] = 1;

Query is performed according to the display requirements.
You only need to print the final data obtained from the query to see if it meets the display requirements.

For secondary development, you can call the functions in the system column.

I solved the problem myself. I used a very stupid way to use if to judge it !! The code is as follows: Red part

$ I = 0;
?>

$ V ){
?>


$ Sid = $ k;
?>
F 'Shop _ search', 'Cate _ id' => $ k);?> "Style =" float: right "target =" _ blank "> More ..



    $ Store) {?>

  • 'Show _ store', 'id' => $ store ['store _ id']), 'store', $ store ['store _ domain ']);?> "Title =" "target =" _ blank ">
    "Onload =" javascript: DrawImage (this, 240,240); "/>












Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.