Php 嵌套foreach代码点火器
在CodeIgniter1.73中,我尝试按类别显示书籍。所以,如果我有一个叫做汽车的分类,我会看到汽车中的书籍列表。所以我尝试了一个嵌套的foreach循环来实现这一点,但似乎无法让它工作Php 嵌套foreach代码点火器,php,codeigniter,foreach,Php,Codeigniter,Foreach,在CodeIgniter1.73中,我尝试按类别显示书籍。所以,如果我有一个叫做汽车的分类,我会看到汽车中的书籍列表。所以我尝试了一个嵌套的foreach循环来实现这一点,但似乎无法让它工作 <?php class Book_model extends Model { function books_by_category() { $this->db->select('*'); $this->db->from('c
<?php
class Book_model extends Model {
function books_by_category()
{
$this->db->select('*');
$this->db->from('categories');
$this->db->join('books', 'books.category_id = categories.id');
$query = $this->db->get();
return $query->result();
}
}
Foreach没有您使用的“else”子句
foreach($category['books'] as $book)
{
// show books
} else {
// show 'no books'
}
相反,您可以这样做:(我的PHP生锈了)
if(count($category['books'])代码中有一些错误可能导致此问题
在控制器中的foreach循环结束之前有一个额外的结束括号
在您看来,您正在以$book的形式循环图书类别,但随后尝试使用循环中的$row对象。请尝试将其更改为$book
同样在您看来,您试图在不关闭PHP标记的情况下输出一些HTML。代码中的
标记位于PHP块中,然后您尝试再次打开一个新的PHP块,而不关闭第一个PHP块
几点
function books_by_category()
{
$this->db->select('*');
$this->db->from('categories');
$this->db->join('books', 'books.category_id = categories.id');
$query = $this->db->get();
return $query->result();
}
这段代码返回类别和具有类别集的书籍的所有列,这真的是您想要做的吗?函数名“books\u by\u category”会向我表明您只对一个特定类别的书籍感兴趣,从您的问题来看,我猜情况并非如此
假设您试图获取所有书籍,但按类别对它们进行分组,我将执行以下操作:
模型
控制器
function index() {
$data = array();
$book_list = $this->book_model->get_books();
if($book_list)
{
$categories = array();
$books_by_category = array();
foreach($book_list as $book)
{
$category_id = $book['category.id'];
$category_name = $book['category.name'];
if(array_key_exists($category_id, $categories))
{
$categories[$category_id] = $category_name;
}
if(array_key_exists($category_id, $books_by_category))
{
$books_by_category[$category_id] = array();
}
$books_by_category[$category_id][] = $book;
}
$data['categories'] = $categories;
$data['books_by_category'] = $books_by_category;
}
$data['main_content'] = 'books_view';
$this->load->view('includes/template', $data);
}
那景色呢
<?php if(isSet($books_by_category)) : ?>
<?php foreach($books_by_category as $category => $book_list): ?>
<p>Category: <?php echo $categories[$category]; ?></p>
<ul>
<?php foreach($book_list as $book) : ?>
<li><?php echo $book->book_number; ?>. <?php echo anchor('bible/chapters/' . $book->id, $book->book_title); ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php else: ?>
<p>Sorry dude, no books.</p>
<?php endif; ?>
类别:
对不起,伙计,没有书
我遇到了这个问题,并尝试按照@mattumotu发布的示例进行操作,这确实很有帮助,但我认为它可能有一些错误,特别是数组的索引和循环方式。如果您使用一个数字作为id值,并且您的id不是从零开始,而是从零开始按数字顺序递增0到x,则当您尝试循环数组时,视图中会出现未知的索引顺序(至少我是这样做的)。因此,我对示例进行了如下修改(注意,为了自己的使用,我稍微更改了变量名):
型号:
public function get_imagery_by_category(){
$this->db->select('extra_imagery.*', FALSE); //MH - not sure what FALSE does here
$this->db->select('extra_image_categories.id as cat_id, extra_image_categories.name as cat_name', FALSE); //MH alias the category id and name as they are the same column names as the imagery name and id
$this->db->from('extra_imagery');
$this->db->join('extra_image_categories', 'extra_image_categories.id = extra_imagery.cat'); //get all images where the image cat matches category id
$this->db->order_by('extra_image_categories.id');
$query = $this->db->get();
if($query->num_rows() == 0){
return false;
} else {
return $query->result_array();
}
}
这里我们只是按照类别ID对所有项目进行排序,以确保我们按照类别ID的数字顺序浏览所有项目,基本上是按类别列出项目
控制器:
function index() {
$data = array();
if($query = $this->book_model->books_by_category())
{
$data['books_by_category'] = $query;
foreach($query as $row)
{
if (!isset($data[$row['id']]))
{
$data[$row['id']] = $row;
$data[$row['id']]['books'] = array();
}
$data[$row['id']]['books'][] = $row;
}
$data['main_content'] = 'books_view';
$this->load->view('includes/template', $data);
}
}
$imagery_list = $this->imagery_model->get_imagery_by_category();
if($imagery_list){
$categories = array();
$imagery_by_category = array();
$curCatIndex = -1;
$curCatID = NULL;
foreach($imagery_list as $i=>$image){
$category_id = $image['cat_id'];
$category_name = $image['cat_name'];
if ($curCatID != $category_id){
$curCatIndex+=1;
$categories[$curCatIndex] = $category_name;
$curCatID = $category_id;
}
$imagery_by_category[$curCatIndex][] = $image;
}
//print_r($imagery_by_category);
$data['categories'] = $categories;
$data['imagery_by_category'] = $imagery_by_category;
}
在这里,我们将设置一个数组来保存我们的类别,并设置一个数组来保存我们的项目。$images\u by\u类别将是一个多维数组,这样第一个类别中的所有项目都将在第一个数组中结束,第二个类别中的所有项目都在第二个数组中,等等。如下所示:
$images\u按类别[0][0]=类别0,项目0
$images_按_类别[0][1]=类别0,项目1
$images_by_category[1][0]=category1,item0
我们正在设置一个计数器($curCatIndex),用于跟踪每次点击新类别id时的情况,并为类别id设置一个变量以检查类别id是否已更改($curCatID)。如果点击新类别id,我们知道已收集该类别的所有项目,因此我们增加curCatIndex计数器:
$curCatIndex+=1;
因为在开始循环时,$curCatID以NULL开头,所以我们增加$curCatIndex,以便数组从索引0开始
每当我们点击一个新的类别id,我们就会将该类别的名称推送到$categories数组中:
$categories[$curCatIndex]=$categority\u name;
并将$curCatID设置为我们的新id:
$curCatID=$category\u id;
然后,无论我们是否属于新类别,我们都会将项目推送到多维数组中的适当位置:
$images\u按类别[$curCatIndex][]=$images;
最后,我们只是让这些阵列在我们的视图中可用:
<?php if(isSet($imagery_by_category)) : ?>
<?php foreach($categories as $i=>$category_item): ?>
<p><?php echo $category_item ?></p>
<?php foreach($imagery_by_category[$i] as $image_item) : ?>
<?php
echo $image_item['id'];
?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php else: ?>
<p>No imagery</p>
<?php endif; ?>
$data['categories']=$categories;
$data['Images_by_category']=$Images_by_category;
那么,我们认为:
<?php if(isSet($imagery_by_category)) : ?>
<?php foreach($categories as $i=>$category_item): ?>
<p><?php echo $category_item ?></p>
<?php foreach($imagery_by_category[$i] as $image_item) : ?>
<?php
echo $image_item['id'];
?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php else: ?>
<p>No imagery</p>
<?php endif; ?>
没有意象
我们有一个嵌套循环。外部循环通过我们的类别循环:
注意,我们添加了一个变量$i来跟踪循环索引(即数组索引)
然后我们打印出类别的名称:
然后内部循环遍历该类别中的项目:
注意,我们在这里使用$i变量来确保从项目数组中选择正确的维度
然后在这个循环中,你可以做任何你想做的事情。这里我只是打印出我的物品的唯一id:
echo$image_项目['id'];
这可能是一种更优雅的方式,但希望它能帮助别人。我在这上面撞了一会儿。多亏mattumotu让我走上正轨。我在这里发布了一个更清晰的代码示例:
我添加了else子句,但仍然是空白页。我是否需要在第二个foreach中使用另一个空数据数组?请尝试执行print\r($data);
并查看数组的完整结构。可能没有任何可重复阅读的书籍?我应该将print\r($data)放在哪里;您可以将其添加到视图中if语句的正上方,然后在处理完后将其删除。我认为控制器有问题。由于控制器原因,页面未加载。我更正了您提到的所有错误,但它仍然未加载。您是否看到上面的任何其他错误?我在控制器中运行了上一个查询并出现此错误:发生数据库错误错误号:1096未使用表选择*我复制了您在此处所写的内容,页面未加载。控制器末尾附近的类别旁边是否应该有一个变量符号?它是否应该读取$data['categories]=$categories;而不是$data['categories']=类别;当你说页面
$imagery_list = $this->imagery_model->get_imagery_by_category();
if($imagery_list){
$categories = array();
$imagery_by_category = array();
$curCatIndex = -1;
$curCatID = NULL;
foreach($imagery_list as $i=>$image){
$category_id = $image['cat_id'];
$category_name = $image['cat_name'];
if ($curCatID != $category_id){
$curCatIndex+=1;
$categories[$curCatIndex] = $category_name;
$curCatID = $category_id;
}
$imagery_by_category[$curCatIndex][] = $image;
}
//print_r($imagery_by_category);
$data['categories'] = $categories;
$data['imagery_by_category'] = $imagery_by_category;
}
<?php if(isSet($imagery_by_category)) : ?>
<?php foreach($categories as $i=>$category_item): ?>
<p><?php echo $category_item ?></p>
<?php foreach($imagery_by_category[$i] as $image_item) : ?>
<?php
echo $image_item['id'];
?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php else: ?>
<p>No imagery</p>
<?php endif; ?>