CArrayDataProvider Object
(
[keyField] =>
[rawData] => Array
(
[24] => Array
(
[ids] => 24
[name] => trillia
)
[25] => Array
(
[ids] => 25
[name] => ahestina
)
[28] => Array
(
[ids] => 28
[name] => piyas
)
[59] => Array
(
[ids] => 59
[name] => sitesrs
)
[60] => Array
(
[ids] => 60
[name] => simons
)
[70] => Array
(
[ids] => 70
[name] => Mahesh Raj
)
[71] => Array
(
[ids] => 71
[name] => kathetrine
)
[72] => Array
(
[ids] => 72
[name] => babig
)
[73] => Array
(
[ids] => 73
[name] => symons
)
)
[caseSensitiveSort] => 1
[_id:CDataProvider:private] =>
[_data:CDataProvider:private] =>
[_keys:CDataProvider:private] =>
[_totalItemCount:CDataProvider:private] =>
[_sort:CDataProvider:private] =>
[_pagination:CDataProvider:private] => CPagination Object
(
[pageVar] => page
[route] =>
[params] =>
[validateCurrentPage] => 1
[_pageSize:CPagination:private] => 10
[_itemCount:CPagination:private] => 0
[_currentPage:CPagination:private] =>
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
from the above array i populate the cgridview , using the following code in view file
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'unen-custom-module-grid',
'dataProvider'=>$unenrollProvider,
//'dataProvider'=>Yii::app()->globals->getUsersByStandardnSection(1,3),
'itemsCssClass'=>'table table-striped flip-scroll',
'ajaxUpdate'=>true,
'template'=>'{summary}{pager}{items}{summary}{pager}',
'pager'=>array(
'header' => '',
'firstPageLabel' => '<<',
'prevPageLabel' => 'PREVIOUS',
'nextPageLabel' => 'NEXT',
'lastPageLabel' => '>>',
),
// 'selectableRows' => 2,
'columns'=>array(
array(
'header'=>'#',
'value'=>'++$row',
'htmlOptions'=>array(
'style'=>'width:50px;'
)
),
'name',
array(
'id' => 'unselectedIds',
// 'name' => 'ids',
'selectableRows' => 2,
'class' => 'CCheckBoxColumn',
//'value'=>'$this->ids'
),
),
));
?>
its working fine , but whenever i click the pagination the cgridview is automatically hiding , im not using $model->search() here ,
every time cgridview pagination sends get requests only, with out form post data you cant able to populate the cgrid again and again , so try to store the values in session and access it , i hope its usefull to you , try it
CGridView requires the result returned by CActiveDataProvider so try to do this in your search method in the corresponding model using CActiveDataProvider
I think you're experiencing a problem here for not having specified a keyField in your data provider. Try array('keyField'=>'ids) when creating the data provider instance. See also this answer.
Related
I have an array with the following structure:
Array
(
[DigitalAssets] => Array
(
[0] => Array
(
[PartNumber] => 0276S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0576s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:10.28
)
[1] => Array
(
[PartNumber] => 0437S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0437s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:11.687
)
[2] => Array
(
[PartNumber] => 0574S-4
[Link] => https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg
[AssetTypeCode] => P04
[FileName] => 0574s-4.jpg
[RecordModifiedDate] => 2020-05-13T18:59:12.593
)
I want to count the total Indexes of array so that I will run loop accordingly. I used Count($array) and Count ($array,RECURSIVE) but it cannot return the correct total number of indexes.
Can only guide that how to do this?
Thanks
If You want to count DigitalAssets direct children You can do this
count($array['DigitalAssets'])
Here are some tests how count should work.
Code is:
$test = array
(
'DigitalAssets' => array
(
0 => array(
'PartNumber' => '0276S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0576s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0576s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:10.28'
),
1 => array(
'PartNumber' => '0437S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0437s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0437s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:11.687'
),
2 => array
(
'PartNumber' => '0574S-4',
'Link' => 'https://1ddf4b1b856a39e33863-d785dc0e3b62b5e0ef07f55db00b0659.ssl.cf2.rackcdn.com/Holley/0574s-4.jpg',
'AssetTypeCode' => 'P04',
'FileName' => '0574s-4.jpg',
'RecordModifiedDate' => '2020-05-13T18:59:12.593'
)
)
);
echo count($test['DigitalAssets'])." ".count($test['DigitalAssets'], 0)." ".count($test['DigitalAssets'], 1);
exit();
Result in my case:
3 3 18
This means taht in my case mode in count is set to 0 by default so i will get only first level counted. If i set mode to 1 i will get all nested items counted as well. This should clear things up for You.
I have tried using this example here Return index of highest value in an array
But that does not go into a multi dimensional Array
I have tried ARRAY_COLUMN(), array_values(), array_shift, Max and Min but they are not getting the info i need
I want to be able to loop thru and get to:
[pricing]
then Check pricing to see which is the highest and lowest in the nested array
so like below which total was the Highest and lowest trades on these dates
[2017-09-22]
[2017-09-23]
obviously [2017-09-23] is a simple one, i just dint want to add much more code for ppl helping to go thru.
The Array i create looks like this:
Array
(
[2017-09-23] => Array
(
[0] => Array
(
[timestamp] => 1506169387000
[pricing] => 9.5470
[qty] => 25
[total] => 238.675
[date] => 2017-09-23
)
)
[2017-09-22] => Array
(
[0] => Array
(
[timestamp] => 1506093083000
[pricing] => 9.6300
[qty] => 25
[total] => 240.75
[date] => 2017-09-22
)
[1] => Array
(
[timestamp] => 1506077220000
[pricing] => 8.7190
[qty] => 13
[total] => 113.347
[date] => 2017-09-22
)
[2] => Array
(
[timestamp] => 1506077109000
[pricing] => 8.6800
[qty] => 83
[total] => 720.44
[date] => 2017-09-22
)
[3] => Array
(
[timestamp] => 1506065258000
[pricing] => 8.7100
[qty] => 25
[total] => 217.75
[date] => 2017-09-22
)
)
in the example above i would like it to Create a new array with only the following
date -> last timestamp -> Highest pricing -> Lowest lowest -> Total of all Totals -> first Timestamp
EDIT: the last and first Timestamp are basically the first and last index so in this case:
[timestamp] => 1506093083000 and [timestamp] => 1506065258000
or Index [0] and index [3]
The array from your question:
$array = array (
'2017-09-23' =>
array (
0 =>
array (
'timestamp' => '1506169387000',
'pricing' => '9.5470',
'qty' => '25',
'total' => '238.675',
'date' => '2017-09-23',
),
),
'2017-09-22' =>
array (
0 =>
array (
'timestamp' => '1506093083000',
'pricing' => '9.6300',
'qty' => '25',
'total' => '240.75',
'date' => '2017-09-22',
),
1 =>
array (
'timestamp' => '1506077220000',
'pricing' => '8.7190',
'qty' => '13',
'total' => '113.347',
'date' => '2017-09-22',
),
2 =>
array (
'timestamp' => '1506077109000',
'pricing' => '8.6800',
'qty' => '83',
'total' => '720.44',
'date' => '2017-09-22',
),
3 =>
array (
'timestamp' => '1506065258000',
'pricing' => '8.7100',
'qty' => '25',
'total' => '217.75',
'date' => '2017-09-22',
),
),
);
To get the values maybe you can use array_map and with this the function array_column is the key of all, try with this:
$values = array_map(function($dates) {
$timestamps = array_column($dates, 'timestamp');
$pricings = array_column($dates, 'pricing');
return [
'max_pricing' => max($pricings),
'lowest_pricing' => min($pricings),
'total_of_totals' => array_sum(array_column($dates, 'total')),
'first_timestamp' => reset($timestamps),
'last_timestamp' => end($timestamps),
];
}, $array);
$values is an array with the filter values that you need:
Array
(
[2017-09-23] => Array
(
[max_pricing] => 9.5470
[lowest_pricing] => 9.5470
[total_of_totals] => 238.675
[first_timestamp] => 1506169387000
[last_timestamp] => 1506169387000
)
[2017-09-22] => Array
(
[max_pricing] => 9.6300
[lowest_pricing] => 8.6800
[total_of_totals] => 1292.287
[first_timestamp] => 1506093083000
[last_timestamp] => 1506065258000
)
)
EDIT
Get the pricing of the [first_timestamp] and [last_timestamp]
$values = array_map(function($dates) {
$timestamps = array_column($dates, 'timestamp');
$pricings = array_column($dates, 'pricing');
return [
'max_pricing' => max($pricings),
'lowest_pricing' => min($pricings),
'total_of_totals' => array_sum(array_column($dates, 'total')),
'first_timestamp' => [
'value' => reset($timestamps),
'pricing' => $pricings[each($timestamps)['key']]
],
'last_timestamp' => [
'value' => end($timestamps),
'pricing' => $pricings[each($timestamps)['key']]
]
];
}, $array);
I added an array to both timestamps with the value of these and the pricing.
Array
(
[2017-09-23] => Array
(
[max_pricing] => 9.5470
[lowest_pricing] => 9.5470
[total_of_totals] => 238.675
[first_timestamp] => Array
(
[value] => 1506169387000
[pricing] => 9.5470
)
[last_timestamp] => Array
(
[value] => 1506169387000
[pricing] => 9.5470
)
)
[2017-09-22] => Array
(
[max_pricing] => 9.6300
[lowest_pricing] => 8.6800
[total_of_totals] => 1292.287
[first_timestamp] => Array
(
[value] => 1506093083000
[pricing] => 9.6300
)
[last_timestamp] => Array
(
[value] => 1506065258000
[pricing] => 8.7100
)
)
)
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
)
)
)
)
)
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;']],
],
]); ?>
I am currently experimenting with the Discogs API, using PHP. My query against their API is returned as JSON, which I can decoding using:
$trackMeta = json_decode($trackMeta, true);
I'd then like to be able to access certain elements within the data that is returned. Using print_r($trackMeta);outputs the below data:
Array ( [pagination] => Array ( [per_page] => 1 [pages] => 7 [page] => 1 [urls] => Array ( [last] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7 [next] => http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2 ) [items] => 7 ) [results] => Array ( [0] => Array ( [style] => Array ( [0] => Drum n Bass ) [thumb] => http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg [format] => Array ( [0] => Vinyl [1] => 12" [2] => 45 RPM ) [country] => UK [barcode] => Array ( ) [uri] => /Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189 [community] => Array ( [want] => 66 [have] => 81 ) [label] => Array ( [0] => Siren Records ) [catno] => SIREN001 [year] => 2012 [genre] => Array ( [0] => Electronic ) [title] => Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude [resource_url] => http://api.discogs.com/masters/433189 [type] => master [id] => 433189 ) ) )
When trying to use a foreach loop on $trackMeta, I can only return 171 which I believe relates to per_page pages and page.
How can I access data deeper in this array? For example values such as thumb or year or genre?
$trackMeta is an associative array:
$trackMeta = array (
'pagination' => array (
'per_page' => 1,
'pages' => 7,
'page' => 1,
'urls' => array (
'last' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=7',
'next' => 'http://api.discogs.com/database/search?artist=siren&q=snorkel&per_page=1&page=2'
),
'items' => 7
),
'results' => array (
0 => array (
'style' => array (
0 => 'Drum n Bass'
),
'thumb' => 'http://api-img.discogs.com/cf1HxM29IXQKNsSdrwYioa0uEeI=/fit-in/150x150/filters:strip_icc():format(jpeg):mode_rgb()/discogs-images/R-3581480-1336678182-5777.jpeg.jpg',
'format' => array (
0 => 'Vinyl',
1 => '12"',
2 => '45 RPM'
),
'country' => 'UK',
'barcode' => array ( ),
'uri' => '/Siren-21-Vicious-Circle-Snorkel-SPY-Remix-Solitude/master/433189',
'community' => array (
'want' => 66,
'have' => 81
),
'label' => array (
0 => 'Siren Records'
),
'catno' => 'SIREN001',
'year' => 2012,
'genre' => array (
0 => 'Electronic'
),
'title' => 'Siren (21) / Vicious Circle (3) - Snorkel (S.P.Y. Remix) / Solitude',
'resource_url' => 'http://api.discogs.com/masters/433189',
'type' => 'master',
'id' => 433189
)
)
);
For example, you can access thumb with the following code:
print_r($trackMeta['results'][0]['thumb']);
I have a theme in Wordpress which use a code to show products of Woocommerce. However, I want to use the same function for showing also pages with a certain template. Here is what I have done until now:
<div>
<?php
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'room-template.php'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div>
<div>
<?php if(has_post_thumbnail()) { the_post_thumbnail(); }?>
<div>
<div>
</div>
</div>
</div>
<div>Buy Now</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found');
}
wp_reset_postdata();
?>
</div>
Whereas, I have 2 pages with this template, the result is "No products found". P.S. The divs are there because I use them for CSS classes. I've just removed the classed to make it easier to read.
Here is the output of the WP_Query:
WP_Query Object ( [query] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php )))[query_vars] => Array ( [post_type] => page [post_status] => publish [meta_query] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( )[category__not_in] => Array ( )[category__and] => Array ( )[post__in] => Array ( )[post__not_in] => Array ( )[tag__in] => Array ( )[tag__not_in] => Array ( )[tag__and] => Array ( )[tag_slug__in] => Array ( )[tag_slug__and] => Array ( )[post_parent__in] => Array ( )[post_parent__not_in] => Array ( )[author__in] => Array ( )[author__not_in] => Array ( )[suppress_filters] => [ignore_sticky_posts] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [posts_per_page] => 10 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC )[tax_query] => WP_Tax_Query Object ( [queries] => Array ( )[relation] => AND )[meta_query] => WP_Meta_Query Object ( [queries] => Array ( [0] => Array ( [key] => _wp_page_template [value] => room-template.php ))[relation] => AND )[date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id) WHERE 1=1 AND exitn_posts.post_type = 'page' AND ((exitn_posts.post_status = 'publish')) AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') ) GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10 [posts] => Array ( )[post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => 1 [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 436de9012210c4e9c717fd5edf527108 [query_vars_changed:WP_Query:private] => 1 [thumbnails_cached] => [stopwords:WP_Query:private] => )
You want to use get_template_part to print a specific template.
https://developer.wordpress.org/reference/functions/get_template_part/
If you want to use a specific function() within a template file then you might want to move this function to a common function library or your functions.php file.
-- EDIT --
SELECT SQL_CALC_FOUND_ROWS exitn_posts.ID
FROM exitn_posts INNER JOIN exitn_postmeta ON (exitn_posts.ID = exitn_postmeta.post_id)
WHERE 1=1 AND exitn_posts.post_type = 'page'
AND ((exitn_posts.post_status = 'publish'))
AND ( (exitn_postmeta.meta_key = '_wp_page_template' AND CAST(exitn_postmeta.meta_value AS CHAR) = 'room-template.php') )
GROUP BY exitn_posts.ID ORDER BY exitn_posts.post_date DESC LIMIT 0, 10
If executing this on the Database doesn't bring up the data it means either it doesn't exist or something within is missing. Its hard to tell this from my hand. I have pretty much the same pattern in one of my personal query and its Data is returned without any issues.
Example :
$args = array(
'post_type' => explode( ',', $post_type ),
'tag' => $tag,
'p' => $id,
'category_name' => $category,
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => 'meta_value',
'meta_key' => '_my_named_key',
'meta_query' => array(
array(
'key' => '_my_named_key'
)
),
'no_found_rows' => 1
);
-- Working Edit --
Make sure the value is a existing in the Database by looking it via something like phpMyAdmin or making a SQL Query onto it. This case was solved in chat full value had an unknown prefix page-templates/.