Yii2.0 pagination issue second page not working - yii2

How do I do a pagination? I have a search form and a search button which passes in a date range. The first page is working successfully, however when I click on the second page, it shows everything. How do I pass the date range into my pagination?
EDIT:
Below is my ReportSearch.php (model)
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
//'params' => array('search' => 'true', 'from_dt'=>$stockmovement_summary_from_sales_date, 'to_dt'=> $stockmovement_summary_to_sales_date),
],
]);
$dataProvider->setSort([
'defaultOrder' => [
'd_item.desc_e_tx' => SORT_ASC,
],
'attributes' => [
'd_item.desc_e_tx'
]
]);
return $dataProvider;
}
Below is my controller:
$searchModel = new ReportSearch();
$dataProvider = $searchModel->searchStockMovementSummary($stockmovement_summary_from_sales_date, $stockmovement_summary_to_sales_date);
return $this->render('stockmovementreportsummary', [
'search' => 'true',
'dataProvider' => $dataProvider,
'from_dt' => $stockmovement_summary_from_sales_date,
'to_dt' => $stockmovement_summary_to_sales_date,
'model' => $searchModel,
]);
Below is my index.php
$reports = $dataProvider->getModels();
$i=1;
echo "<table class='table table-striped table-bordered'>";
echo "<tr><th>#</th><th>Item</th><th>Total Summary Usage Qty</th><th>Current Stock On Hand</th><th>Measurement</th></tr>";
foreach ($reports as $report) {
$item_key = $report['report_item_key'];
$item_model = Item::find()->where(['item_key' =>$item_key])->one();
$item_e_desc_tx = $item_model->desc_e_tx;
$item_c_desc_tx = $item_model->desc_c_tx;
$item_qty_no = $item_model->qty_no;
$item_measurement_tx = $item_model->measurement;
echo "<tr>";
echo "<td>" . $i++ . "</td>";
echo "<td>" . Html::a($item_key, array('stockmovementreportdetailbyitem', 'item_key' => $item_key, 'search' => 'true', 'from_dt' => $from_dt, 'to_dt'=>$to_dt)) . "<br>" . $item_e_desc_tx . "<br>" . $item_c_desc_tx ."</td>";
echo "<td>" . $report['total_summary_usage_qty'] . "</td>";
echo "<td>" . $item_qty_no . "</td>";
echo "<td>" . $item_measurement_tx . "</td>";
echo "</tr>";
}
echo "</table>";
echo \yii\widgets\LinkPager::widget([
'pagination'=>$dataProvider->pagination,
]);

GridView should be used but I do not think it will help. Do you by any chance use POST instead of GET to filter the results? If you take a look here https://github.com/yiisoft/yii2/blob/master/framework/data/Pagination.php#L257 at the createUrl function you will see that it uses getQueryParams http://www.yiiframework.com/doc-2.0/yii-web-request.html#getQueryParams() to create the query string. If you use POST then this function will not pick up the params properly hence the next page pagination will fail.
Ok, because you actually do use POST then you have to create your own pagination function that takes post into consideration. Or just use GET. You are not performing any operations to the DB you can safely use GET. It is better for everybody.

You have to edit the code in your action.
Read this documentation.
And edit $dataProvider->pagination

Related

Wordpress wpdb not inserting/updating with HTML data

So simply put, I have my PHP code generating a bunch of HTML. I then want to update a database entry like so
$wpdb->update("wp_table",
array( 'content' => $html ),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
This simply does not work. Nothing in the database changes.
To debug, I placed the exact same code right above with one minor change, and it DOES WORK:
$wpdb->update("wp_sn_cached_popular_display",
array( 'content' => "hello" ),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
(I swapped out the $html for a direct string.)
I can't even begin to comprehend why, and I've played around with it a lot. I've even done this and it DOES WORK too:
$string = "hello";
$wpdb->update("wp_sn_cached_popular_display",
array( 'content' => $string),
array( 'id' => 1 ),
array( '%s' ),
array( '%d' )
);
It's just the one $html variable is causing this function to not run, or something?
Compiling the $html variable is a bunch of stuff like this:
$html .= '<li>';
$html .= '<div class="upcoming-left">';
$html .= '<time datetime="' . $date . '" class="icon">';
$html .= '<em>' . $date_day.'</em>';
$html .= '<strong>' . $date_month . '</strong>';
$html .= '<span>' . $date_num . '</span>';
$html .= '</time>';
$html .= '</div>';
$html .= '<div class="upcoming-right">';
$html .= '<span class="upcoming-title">' . $upcoming_title . '</span>';
$html .= '<span class="upcoming-desc">' . $upcoming_desc . '</span>';
$html .= '<div class="clear"></div>';
$html .= '</div>';
$html .= '</li>';
//...
Any ideas why this is?
Bonus fun time: This appears to work on my local xampp insall, not the live site.
This could be an issue with table datatypes
https://www.w3schools.com/sql/sql_datatypes.asp
depending on what the table is set to it may not support the string length of your html variable or certain chars in it.
It looks like you concat your .html var multiple times. It's a pretty long string in the end. to test this try to save a very long manually entered string. try with special chars. look at the table structure. This is just some ideas and where my thoughts go.

How to parse php inside html

I tried to parse the php query into html, but on the html page it shows array.
My query looks like this: $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve') );
I tried to print it on the html page using this: $html .= $comments;
As you can see from the examples.
And as Tom J Nowell commented, you need to loop through each comment and append the comment to the $html.
$args = array(
'status' => 'approve',
'post_id' => $post->ID,
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) :
$html .= $comment->comment_author . '<br />' . $comment->comment_content . '<br />';
endforeach;

Yii2 : validate if at least one checkbox is selected

I have multiple Music releases for a user for which he wants to create a promo, on the promo create form we have all the releases listed and I am using a form model for creating the promo where I define all rules for my several inputs in the promo form model.
I want to check if at-least one release is selected by the user when saving the form but it is not working as the checkboxes are created dynamically.
my form code for that field
foreach ($releaseInfo as $releases) {
if (!is_null($releases->releaseSongs)) {
echo "<fieldset><legend>" . \yii\helpers\Html::activeCheckbox($model, 'selected_releases[]', ['id' => 'release_' . $releases->id, 'onclick' => '$("#songs_' . $releases->id . '").toggle()', 'label' => false]) . " " . $releases->name . "</legend>";
foreach ($releases->releaseSongs as $k => $v) {
echo "<div id='songs_" . $releases->id . "' style='display:none'>";
echo "<div>";
echo $v->song->name;
echo "</div>";
echo "</div>";
}
}
}
echo "</fieldset>";
my rule in model
['selected_releases', 'required', 'on' => ['catalog', 'catalog_update'], 'requiredValue' => 1,
'when' => function ($model) {return ($model->scenario == self::SCENARIO_CATALOG_BASED || $model->scenario == self::SCENARIO_CATALOG_BASED_UPDATE);},
'whenClient' => 'function(attribute,value){return ("' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED . '" || "' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED_UPDATE . '")}',
'message' => 'You must select atleast one release',
]
when i submit my form the posted input variable looks like this
[selected_releases] => Array(
[0] => 0
[1] => 0
)
the requiredValue in the rule does not work because the selected_releases is an array of values it always says that
You must select atleast one release
how shoule i use the requiredValue parameter option for rules in a way that it checks for atleast one slection of the checkbox
OR
do i have to make a custom validation method an call it when validating
i used custom validation method for this purpose , my validation rule looks like following
['selected_releases', 'checkreleaseSelection'
, 'on' => ['catalog', 'catalog_update'],
'when' => function ($model) {return ($model->scenario == self::SCENARIO_CATALOG_BASED || $model->scenario == self::SCENARIO_CATALOG_BASED_UPDATE);},
'whenClient' => 'function(attribute,value){return ("' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED . '" || "' . $this->scenario . '"=="' . self::SCENARIO_CATALOG_BASED_UPDATE . '")}',
]
Custom method
public function checkreleaseSelection()
{
$error = true;
foreach ($this->selected_releases as $selection) {
if ($selection) {
$error = false;
}
}
if ($error) {
$this->addError('selected_releases', 'You must select atleast one of your releases to continue');
}
};

Magento get all orders numbers shipped as overnight between two dates

Title pretty much describes it, I just need to get all the order numbers
shipped with Overnight shipping between 6-8 to 6-12.
Help appreciated
Here is at least one way of achieving this...
$from = "2012-06-08";
$to = "2012-06-12";
$shippingMethod = "overnight_shipping";
$orderIds = Mage::getResourceModel('sales/order_shipment_collection')
->addAttributeToFilter('created_at', array(
'from' => $from,
'to' => $to,
))
->getColumnValues('order_id')
;
$incrementIds = Mage::getResourceModel('sales/order_collection')
->addAttributeToFilter('shipping_method', array('eq' => $shippingMethod))
->addAttributeToFilter('entity_id', array('in' => $orderIds))
->getColumnValues('increment_id')
;
echo "<ul>";
foreach($incrementIds as $incrementId) {
echo "<li>" . $incrementId . "</li>";
}
echo "</ul>";

How to get all metavalue for a specific custom field for all posts in one category?

I'm trying to get the sum for all the meta values of one custom field. I now get the sum for the metavalues of the whole site. I want it to be restricted to the category you're in.
The code now looks like this:
<?php
$thevariable = $wpdb->get_var($wpdb->prepare("
SELECT SUM($wpdb->postmeta.meta_value)
FROM $wpdb->postmeta, $wpdb->posts
WHERE $wpdb->postmeta.meta_key='mycustomfield'
AND $wpdb->postmeta.post_id=$wpdb->posts.id
"));
echo '<h1>' . $thevariable . '</h1>';
?>
Can somebody help me to filter out one category?
Would be amazing!
M
Inside your WP files, you can add this lines; I guess It will do what you are looking for.
<?php
$MySum = 0;
$args = array(
'category_name' => 'MyCategory',
'meta_key' => 'mycustomfield',
'posts_per_page' => '-1' );
// The Query
$the_query = new WP_Query( $args);
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$MySum += get_post_meta($post_id, $key, true);
endwhile;
echo '<h1>' . $MySum . '</h1>';
// Reset Post Data
wp_reset_postdata();
?>