循环遍历类中的数组以及如何在php中返回值和html div

时间:2017-08-03 04:06:02

标签: php html

我希望按类别显示所有产品。但是数据输入在前端并没有正确,但是当我显示类print_r($ product2)时;它在视图源模式下工作得很好。我想在html前端显示它们。

我在课堂上的代码:Product.php

public function getAllProduct(){
    $query = "SELECT p.*, c.cat_name, b.brand_name
              FROM product as p, category as c, brand as b
              WHERE p.cat_id = c.cat_id AND p.brand_id= b.brand_id 
              ORDER BY p.product_id DESC";

    $data = $this->db->select($query);
    $result = $data->fetch_all();
    $product = array();
    $product1  = array();
    foreach($result as $row) {
        $product[$row[2]] = array(
            'id' => $row[2],
            'name' => $row[8]
        );
        $product1[$row[2]][$row[0]] = array(
            'id' => $row[0],
            'image' => $row[6],
            'pro_name' => $row[1],
            'cat_name' => $row[8],
            'br_name' => $row[9],
            'body' => $row[4],
            'price' => $row[5]
        );
    }
    $product2 =array();
    foreach($product as $id=> $val ) {
        $tmp = array(
            $id => array(
                'id' => $val['id'],
                'name' => $val['name'],
                'product' => $product1[$id],
        ));
        array_push($product2, $tmp);
    }
    print_r($product2);

}

我的html前端代码是:

<div class="content">
    <div class="content_top">
     <?php
        $getProduct = $product->getAllProduct();
        if ($getProduct) {
            while($result = $getProduct->fetch_all()){
        ?>
    <div class="heading">
        <h3>Latest from <?php echo $result['cat_name'];?></h3>
    </div>
        <div class="clear"></div>

    </div>
        <div class="section group">
            <div class="grid_1_of_4 images_1_of_4">
                <a href="details.php?product_id=<?php echo $result['product_id']?>">
                <img src="admin/<?php echo $result['image']?>" alt="" /></a>
                <h2><?php echo $result['product_name']?></h2>
                <p><?php echo $fm->textShorten($result['body'], 60)?></p>
                <p><span class="price">&#165;<?php echo $result['price']?></span></p>
                <div class="button">
                    <span>
                        <a href="details.php?product_id=
                            <?php echo $result['product_id']?>" class="details">Details
                        </a>
                    </span>
                </div>
            </div>  

        </div>  
            <?php }}?>
</div>

请帮我看看如何返回值和html div?我希望数据以用户视图模式显示,例如:现在数据如下:

    Array( [0] => Array (
            [3] => Array( [id] => 3
                    [name] => Mobile Phones
                    [product] => Array(
                            [26] => Array(
                                    [id] => 26
                                    [image] => uploads/9813ba5bc5.jpg
                                    [pro_name] => iPhone 7
                                    [cat_name] => Mobile Phones
                                    [br_name] => iPhone
                                    [body] => <p>this is iPhone7</p>
                                    [price] => 70000
                                )
                            [25] => Array(
                                    [id] => 25
                                    [image] => uploads/72aca3c29b.jpg
                                    [pro_name] => iPhone 6
                                    [cat_name] => Mobile Phones
                                    [br_name] => iPhone
                                    [body] => <p>this is iPhone6</p>
                                    [price] => 60000
                                )                           
                        )
                )
        )

    [1] => Array(
            [5] => Array([id] => 5
                    [name] => Software
                    [product] => Array(
                            [21] => Array(
                                    [id] => 21
                                    [image] => uploads/7a683e5f82.jpg
                                    [pro_name] => Lorem Ipsum is simply
                                    [cat_name] => Software
                                    [br_name] => Nokia
                                    [body] => <p>this is Nokia</p>
                                    [price] => 5000
                                )
                            [11] => Array
                                (
                                    [id] => 11
                                    [image] => uploads/05104164e5.jpg
                                    [pro_name] => Lorem Ipsum is simply
                                    [cat_name] => Software
                                    [br_name] => Lenovo
                                    [body] => <p><span>this is  Lenovo/span></p>
                                    [price] => 10000
                                )
                        )
                )
        )
[2] => Array( [4] => Array([id] => 4
                    [name] => Accessories
                    [product] => Array(
                            [20] => Array(
                                    [id] => 20
                                    [image] => uploads/5a113bf1d0.jpg
                                    [pro_name] => japan
                                    [cat_name] => Accessories
                                    [br_name] => iPhone
                                    [body] => this is  Lenovo 4
                                    [price] => 40000
                                )
                        )
                )
        )

但我想这样:

 3
Mobile Phones
   26
   uploads/9813ba5bc5.jpg
   iPhone 7
   Mobile Phones
   iPhone
   this is iPhone7
   70000


   25
   uploads/72aca3c29b.jpg
   iPhone 6
   Mobile Phones
   iPhone
   this is iPhone6
   60000


5
Software
   21
   uploads/7a683e5f82.jpg
   Lorem Ipsum is simply
   Software
   Nokia
   this is Nokia
   5000

   11
   uploads/05104164e5.jpg
   Lorem Ipsum is simply
   Software
   Lenovo
   this is  Lenovo
   10000


4
Accessories
   20
   uploads/5a113bf1d0.jpg
   japan
   Accessories
   iPhone
   this is  Lenovo 4
   40000

2 个答案:

答案 0 :(得分:1)

getAllProduct()

返回数组
  return $product2;

到达这里

$getProduct = $product->getAllProduct();

foreach($getProduct as $result)
{
   echo $result['cat_name'];
}   

如果您的product数组有multi level而不是使用

进行检查
 print_r($result);

答案 1 :(得分:0)

更新:

您获得整个数组输出的原因是因为您正在使用print_r。相反,您需要使用return

您需要从

更改product.php中的最后一行
print_r($product2);

return $product2;

然后代码中的 更改

$getProduct = $product->getAllProduct();

print_r($getProduct);

使用print_r输出为:

Array( [0] => Array (
            [3] => Array( [id] => 3
                    [name] => Mobile Phones
                    [product] => Array(
                            [26] => Array(
                                    [id] => 26
                                    [image] => uploads/9813ba5bc5.jpg
                                    [pro_name] => iPhone 7
                                    [cat_name] => Mobile Phones
                                    [br_name] => iPhone
                                    [body] => <p>this is iPhone7</p>
                                    [price] => 70000

使用return,输出为:

3
Mobile Phones
   26
   uploads/9813ba5bc5.jpg
   iPhone 7
   Mobile Phones
   iPhone
   this is iPhone7
   70000