繁体   English   中英

CodeIgniter - foreach 循环

[英]CodeIgniter - foreach loop

TL; DR 我正在尝试循环所有行并用 html 输出它。 当我循环它时,我得到一个数组,其中包含一行的属性和值,这也是最后一行,当我不循环它时,我在一个数组中得到 2 个数组(每个数组是一行)。 出于某种原因,它没有循环它并按应有的方式显示所有行,我该如何解决这个问题?

我正在尝试制作一个部分和其中的文章的循环,因为每篇文章都有一个父部分。 在数据库中,有 5 个部分和 2 篇文章,文章 #1 有部分 #1 作为它的父级,文章 #2 有部分 #2 作为它的父级。

当它作为一个数组打印而不循环时,我得到以下数组;

 Array ( [0] => Array ( [s_id] => 1 [s_name] => News [s_slug] => news [s_visibility] => 1 [s_type] => 1 [s_status] => 1 [s_permission] => 0 [s_external] => 0 [s_location] => news [s_color] => 1 [s_homepage] => 1 [a_id] => 1 [a_section] => 1 [a_title] => Ted Cruz's 'Secret' Skill That No President Has Likely Had Since Thomas Jefferson [a_description] => Apparently Cruz, whose famed 2013 marathon filibuster speech over defunding Obamacare jumped across a range of topics, has an uncanny capability to remember things he hears verbatim. [a_content] => Apparently Cruz, whose famed 2013 marathon filibuster speech over defunding Obamacare jumped across a range of topics, has an uncanny capability to remember things he hears verbatim. [a_views] => 0 [a_visibility] => 1 [a_date] => 17.11.2015 [a_author] => 1632422528 [a_category] => 1 [a_slug] => ) [1] => Array ( [s_id] => 2 [s_name] => VOD [s_slug] => vod [s_visibility] => 1 [s_type] => 2 [s_status] => 1 [s_permission] => 0 [s_external] => 0 [s_location] => vod [s_color] => 2 [s_homepage] => 1 [a_id] => 2 [a_section] => 2 [a_title] => GTA V PC Edition Released [a_description] => Wondering where the score is? Our GTA Online review will remain scoreless, as a score does not properly reflect its continuously changing nature. Here's how and why we decided to do it this way. [a_content] => Wondering where the score is? Our GTA Online review will remain scoreless, as a score does not properly reflect its continuously changing nature. Here's how and why we decided to do it this way. [a_views] => 0 [a_visibility] => 1 [a_date] => 19.11.2015 [a_author] => 1632422528 [a_category] => 1 [a_slug] => ) )

但是当我将它运行到 foreach 循环中时,它只输出一个数组,即后面的数组 (1)。

我正在使用 CodeIgniter 3,循环在一个名为“Global_functions”的库中,模型是“Functions_model”。

Global_functions(只有相关函数,不是整个类,因为它包含其他不相关的函数):

 public function get_homepage_sections() { $getHomeData = $this->CI->functions_model->get_homepage_data(); foreach ($getHomeData as $get_sections) { switch ($get_sections['s_color']) { case 1: $sectionColor = "blue"; break; case 2: $sectionColor = "purple"; break; case 3: $sectionColor = "orange"; break; case 4: $sectionColor = "green"; break; default: $sectionColor = ""; break; } $outputData = ' <li> <div><h2 class="category ' . $sectionColor . '">' . $get_sections['s_name'] . '</h2></div>'; $outputData .= ' </li>'; } return $get_sections; }

函数_模型;

 public function get_homepage_data() { $selected_columns = array( 'sections.s_id', 'sections.s_name', 'sections.s_slug', 'sections.s_visibility', 'sections.s_type', 'sections.s_status', 'sections.s_permission', 'sections.s_external', 'sections.s_location', 'sections.s_color', 'sections.s_homepage', 'articles.a_id', 'articles.a_section', 'articles.a_title', 'articles.a_description', 'articles.a_content', 'articles.a_views', 'articles.a_visibility', 'articles.a_date', 'articles.a_author', 'articles.a_category', 'articles.a_slug' ); $query = $this->db->select( $selected_columns ) ->from( config_item('sections') . ', ' . config_item('articles') ) //->join( config_item('articles'), 'articles.a_section = sections.s_id' ) ->where( 'articles.a_section = sections.s_id' ) //->or_where( 'user_email', $user_string ) ->get(); if ( $query->num_rows() >= 1 ) { return $query->result_array(); } }

从控制器调用如下(主页部分);

 public function index() { /* if ($this->require_role('admin')) { echo $this->load->view('examples/page_header', '', TRUE); echo '<p>You are logged in!</p>'; echo $this->load->view('examples/page_footer', '', TRUE); }*/ // return $isAutoRememberMe; //extra_for_auth(); // Call a function of the model $data['getGlobalMessage'] = $this->global_functions->get_global_message(); $data['userOptions'] = $this->global_functions->extra_for_auth(); $data['homepageSection'] = $this->global_functions->get_homepage_sections(); //print_r ($data); $this->parser->parse('template/header', $data); $this->parser->parse('sections/homepage', $data); $this->load->view('template/footer'); }

section/homepage 文件包含调用 {homepageSection},如您所见,解析器被调用并解析文件,而不是使用 view() 加载它。

我认为 get_home_sections() 中有错误

代替:

        return $get_sections;

你应该:

        return $getHomeData

您正在返回数组中的最后一项,这看起来像您所说的问题。

我认为有点令人困惑的另一点是您正在累积 html 的 var。 $outputData在每个循环中都被重置,你没有对它做任何事情。 最好在函数开始时将其初始化为空字符串 $outputData = '';

在循环中,您应该使用 .= 进行附加评估

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM