2
votes

im using yii2 framework

I have array coming from controller to the view.

 public function actionProducts()
{
$product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');

        $array1 = str_replace("ns2:","",$product);
        $array=json_decode(json_encode(simplexml_load_string($array1)),true);           

        return $this->render('products',['array'=>$array]); 
}

In the above code im converting xml to an array. The $array has the array, and im passing it to view called products. Now i want to display that array in the view in Gridview, Since im new to yii2 im unable to do this, it says i have to use some dataprovider, but im not getting how to do this.

Can anyone please help me in this how do i display the array in gridview. Thank you

Thanks for the answer..

Actually its a amazon product array im fetching it from the Product API, we cant define any predefine attributes like id and all, since its different for each product. this is how the array looks like.

  Array
  (
   [GetMatchingProductResult] => Array
    (
        [0] => Array
            (
                [@attributes] => Array
                    (
                        [ASIN] => 0886467918
                        [status] => Success
                    )

                [Product] => Array
                    (
                        [Identifiers] => Array
                            (
                                [MarketplaceASIN] => Array
                                    (
                                        [MarketplaceId] => A21TJRUUN4KGV
                                        [ASIN] => 0886467918
                                    )

                            )

                        [AttributeSets] => Array
                            (
                                [ItemAttributes] => Array
                                    (
                                        [Author] => Kipling, Rudyard
                                        [Binding] => Hardcover
                                        [Edition] => Har/Cas
                                        [Format] => Import
                                        [Label] => Imprint unknown
                                        [Languages] => Array
                                            (
                                                [Language] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [Name] => english
                                                                [Type] => Published
                                                            )

                                                        [1] => Array
                                                            (
                                                                [Name] => english
                                                                [Type] => Original Language
                                                            )

                                                        [2] => Array
                                                            (
                                                                [Name] => english
                                                                [Type] => Unknown
                                                            )

                                                    )

                                            )

                                        [Manufacturer] => Imprint unknown
                                        [PackageDimensions] => Array
                                            (
                                                [Weight] => 1.74
                                            )

                                        [ProductGroup] => Book
                                        [ProductTypeName] => ABIS_BOOK
                                        [PublicationDate] => 1988-05-02
                                        [Publisher] => Imprint unknown
                                        [SmallImage] => Array
                                            (
                                                [URL] => http://ecx.images-amazon.com/images/I/412CsE6Mb8L._SL75_.jpg
                                                [Height] => 75
                                                [Width] => 50
                                            )

                                        [Studio] => Imprint unknown
                                        [Title] => Jungle Book ("Read Along")
                                    )

                            )

                        [Relationships] => Array
                            (
                            )

                        [SalesRankings] => Array
                            (
                                [SalesRank] => Array
                                    (
                                        [0] => Array
                                            (
                                                [ProductCategoryId] => book_display_on_website
                                                [Rank] => 709468
                                            )

                                        [1] => Array
                                            (
                                                [ProductCategoryId] => 1318084031
                                                [Rank] => 14260
                                            )

                                        [2] => Array
                                            (
                                                [ProductCategoryId] => 1318083031
                                                [Rank] => 47016
                                            )

                                    )

                            )

                    )

            )
1

1 Answers

11
votes

If the array contain the data like a dataProvider . you can use arrayDataProvider http://www.yiiframework.com/doc-2.0/yii-data-arraydataprovider.html eg (from yii2 guide):

    $data = [
        ['id' => 1, 'name' => 'name 1', ...],
        ['id' => 2, 'name' => 'name 2', ...],
        ...
        ['id' => 100, 'name' => 'name 100', ...],
    ];

    $provider = new ArrayDataProvider([
        'allModels' => $data,
        'pagination' => [
            'pageSize' => 10,
        ],
        'sort' => [
            'attributes' => ['id', 'name'],
        ],
    ]);

a brief guide to dataprovider http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html

in your case

    public function actionProducts()
    {
    $product = Yii::$app->httpclient->get('http://localhost/MWSProducts/src/MarketplaceWebServiceProducts/Samples/GetMatchingProductSample.php');

            $array1 = str_replace("ns2:","",$product);
            $array=json_decode(json_encode(simplexml_load_string($array1)),true);           



            $provider = new ArrayDataProvider([
                'allModels' => $array,
                'pagination' => [
                    'pageSize' => 10,
                ],
            ]);

            return $this->render('products',['array'=>$array]); 
    }

Refer to sample above where the array $data contain id, name and suppose your arrayDataProvider is in the var $array and have id,name too in gridview you should have

 <?= GridView::widget([
    'dataProvider' => $array,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        'name',
        .....

        ['class' => 'yii\grid\ActionColumn',
        'contentOptions' => ['style' => 'width:84px; font-size:18px;']],
    ],
]); ?>