create drop down to view month and year in php - php

I am using this function to create a dropdown for months and year my problem is am getting an error like below:
Unknown column 'format' in 'where clause'
below is the code anyone help to get rid of this error
date_condition = ' and 1=1 ';
if($date_type==1)
{
$date_condition = ' and m.submittimestamp BETWEEN DATE_SUB(NOW(), INTERVAL 300 DAY) AND NOW() ';
}
else if($date_type==2)
{
$date_condition = ' and YEAR(m.submittimestamp) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) and MONTH(m.submittimestamp)=MONTH(CURRENT_DATE - INTERVAL 1 MONTH) ';
}
else if($date_type==3)
{
$date_condition = ' and YEAR(m.submittimestamp) = YEAR(date(format)) AND MONTH(m.submittimestamp)=MONTH(date) ';
}
conditions 1 and 2 are working perfectly but condition 3 is getting error
Below is the code for drop down and script
<div class="form-group ">
<input name="filter_options"
<?php echo $option3; ?>
value="3" onchange="toggleDate(1)" type="radio" class="form-control" id="from_date">
<label for="from_date">show status for the month of </label>
<select name="month" class="form-control">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('-%d months', $i));
$value = date('m', $time);
$label = date('F ', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
</select>
<select name="year" class="form-control">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('-%d years', $i));
$value = date('Y', $time);
$label = date('Y ', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
<option value=" ">year</option>
</select>
</div>

The error is telling you everything you need to know, you have a syntax error. Remove the letter "e" at the beginning of the code noted in your error.
e) AND MONTH(m.submittimestamp)=MONTH(date) GROUP
should be
) AND MONTH(m.submittimestamp)=MONTH(date) GROUP

Related

wordpress create custom filter

I created the custom table in DB and use this tutorial: LINK
Everything is worked and printed well, without filters.
I read a lot of posts, but whatever I do, filters don't work.
table name: vladi_stock_status_daily_update
this is first part of getting info from db:
// form sql query due to filter fields
function getWhere() {
global $wpdb;
$where = ["main.order_item_type='product_name'"];
$start_unix = time() - $this->defaultPeriod*86400;
$start = date('Y-m-d H:i:s', $start_unix);
$end = date('Y-m-d H:i:s', time());
if (strlen($_GET['date']) > 0) {
$date_arr = explode(' - ', $_GET['date']);
$start = $date_arr[0];
$end = $date_arr[1];
}
if (strlen($_GET['product_name'])) {
$where[] = " (SELECT `product_name` * FROM ".$wpdb->prefix."vladi_stock_status_daily_update)";
}
if ($start != $end) {
$where[] = "(SELECT `date` FROM ".$wpdb->prefix.vladi_stock_status_daily_update) >= '$start' AND (SELECT `date` FROM ".$wpdb->prefix."vladi_stock_status_daily_update) <= '$end'";
} else {
$where[] = "(SELECT `date` FROM ".$wpdb->prefix."vladi_stock_status_daily_update) >= '$start'";
}
return $where;
}
Second part is to build filters:
public function generate_search_box() {
global $wpdb;
$pickup_locations = [];
// if ( $options = get_option( 'woocommerce_pickup_locations' ) ) {
$pickup_locations = array_filter( (array) $options );
// }
$query = " SELECT date, product_name FROM `wp_vladi_stock_status_daily_update`";
$pickup_locations = $wpdb->get_results($query);
if (isset($_GET['date'])) {
$date_arr = explode(' - ', $_GET['date']);
$start = $date_arr[0];
$end = $date_arr[1];
} else {
$start_unix = time() - $this->defaultPeriod*86400;
$start = date('Y-m-d', $start_unix);
$end = date('Y-m-d', time());
}
$dateSelected = $start . ' - ' . $end;
$shownLocations = [];
?>
<form class="search-box actions" id="search-form">
<input type="hidden" name="post_type" value="product">
<input type="hidden" name="page" value="quantity">
<input type="hidden" name="tab" value="qrt">
<div class="search-field">
<label class="screen-reader-text" for="product_name">Name:</label>
<input type="search" id="product_name" name="product_name" value="<?=$_GET['product_name']?>" placeholder="Product Name" />
</div>
<div class="search-field">
<label class="screen-reader-text" for="date">Date:</label>
<input type="search" id="date" name="date" value="<?=$dateSelected?>" placeholder="Select Date" />
</div>
<div class="search-field">
<?php submit_button( 'search', '', '', false, array( 'id' => 'search-submit' ) ); ?>
<input type="button" value="Reset" id="reset" class="button" onClick="resetForm()">
</div>
</form>
<?php add_thickbox(); ?>
<?php
wp_enqueue_script('moment', 'https://cdn.jsdelivr.net/momentjs/latest/moment.min.js');
wp_enqueue_script('daterangepicker', 'https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js');
wp_add_inline_script('daterangepicker', "
function resetForm(){
jQuery('#product_name').val('');
jQuery('#date').val('');
jQuery('#date').val(jQuery('#date').attr('data-default'));
jQuery('#search-form').submit();
}
jQuery('.remove-btn').click(function(e) {
var variation = jQuery(this).attr('data-variation');
if (!confirm('Are you sure, that you wnt to remove '+variation+'?')) {
e.preventDefault();
}
});
", 'after');
wp_add_inline_script('daterangepicker', "jQuery('#date').daterangepicker(
{
startDate : '$start',
endDate : '$end',
alwaysShowCalendars : true,
locale : {
format : 'YYYY-MM-DD',
},
ranges : {
'Today' : [moment(), moment()],
'Yesterday' : [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days' : [moment().subtract(6, 'days'), moment()],
'Last 30 Days' : [moment().subtract(29, 'days'), moment()],
'This Month' : [moment().startOf('month'), moment().endOf('month')],
'Last Month' : [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
}
}
);", 'after');
wp_enqueue_style('daterangepicker', 'https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css');
}
I get this link for filtering:
/wp-admin/edit.php?post_type=product&page=quantity&tab=qrt&product_name=product1&date=2021-02-07+-+2021-03-09
But doesnt filter anything. Am i doing something wrong like getting sql query from table?
Any help will be appreciated!

JS: Disable second Select Option when first Select Option value is "Select Start Time"

based on my question above, the scenario like this:
1) When I click 'start time' and select any time, then the 'end time' will enable. When I choose any time at 'end time', then I click back 'start time' and choose "Select Start Time", the 'end time' still enable and still display the value that I choose before. Thus. What I want is I want the 'end time' value clear and disabled it.
Below is my current code:
<div class="form-group">
<label>Start Time</label>
<?php get_times( $default = '00:00', $interval = '+5 minutes' ); ?>
<select class="form-control" id="starttime" name="timeFrom" required>
<option value="">Select Start Time</option>
<?php echo get_times(); ?></select>
</div>
<div class="form-group">
<label>End Time</label>
<?php get_times( $default = '00:00', $interval = '+5 minutes' )?>
<select class="form-control" id="endtime" name="timeTo" disabled>
<?php echo get_times(); ?></select>
</div>
<?php
function get_times( $default = '00:00', $interval = '+5 minutes' ) {
$output = '';
$current = strtotime( '00:00' );
$end = strtotime( '23:59' );
while( $current < $end ) {
$time = date( 'H:i:s', $current );
$sel = ( $time == $default ) ? ' selected' : '';
$output .= "<option value=\"{$time}\"{$sel}>" . date( 'H:i', $current ) . '</option>';
$current = strtotime( $interval, $current );
}
return $output;
}
?>
<script>
$('#starttime').change(function(){
var starttime = $(this).val();
console.log(starttime);
$('#endtime option').each(function(){
if($(this).text() < starttime){
$(this).remove();
}
});
$('#endtime').removeAttr('disabled')
})
</script>
Can anyone know how to solve it?
Even if you made this work the way you are asking, I think you would still have a problem because you are removing options from you endtime select, but you never put them back. So if someone chooses the last option in your starttime select, then chooses "Select Start Time" again, then your endtime select would be empty. So basically you need to add the options back to your endtime select every time your starttime changes, then remove the ones options in your endtime that are less than your starttime.
E.G
https://jsbin.com/poruyuwovo/1/edit?html,js,console,output
In your code it would be something like this.
<div class="form-group">
<label>Start Time</label>
<?php get_times( $default = '00:00', $interval = '+5 minutes' ); ?>
<select class="form-control" id="starttime" name="timeFrom" required>
<option value="">Select Start Time</option>
<?php echo get_times(); ?></select>
</div>
<div class="form-group">
<label>End Time</label>
<?php get_times( $default = '00:00', $interval = '+5 minutes' )?>
<select class="form-control" id="endtime" name="timeTo" disabled>
<?php echo get_times(); ?></select>
</div>
<?php
function get_times( $default = '00:00', $interval = '+5 minutes' ) {
$output = '';
$current = strtotime( '00:00' );
$end = strtotime( '23:59' );
while( $current < $end ) {
$time = date( 'H:i:s', $current );
$sel = ( $time == $default ) ? ' selected' : '';
$output .= "<option value=\"{$time}\"{$sel}>" . date( 'H:i', $current ) . '</option>';
$current = strtotime( $interval, $current );
}
return $output;
}
?>
<script id="select_options" type="text/html">
<?php echo get_times(); ?>
</script>
<script>
function endtimeUpdate(){
var $endtime = $('#endtime');
var starttime = $('#starttime').val();
console.log(starttime);
$endtime.find('option').remove();
$endtime.html($('#select_options').html());
if(starttime == ''){
$endtime.prop('disabled', true);
}
else {
$endtime.prop('disabled', false);
$endtime.find('option').each(function(){
if($(this).text() < starttime){
$(this).remove();
}
});
}
}
$('#starttime').change(endtimeUpdate);
</script>

Filter days of week and just show working days in php

I am trying to create a drop down with just the working days of the week on. Monday - Friday. Here's my code:
<?php if ($_SESSION['month'] == $current_month) { $current_day = date("j") + 1;} else {$current_day = 1;} ?>
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date));
echo $weekday." "; ?>
<?php echo $i; ?>
</option>
<?php } ?>
</select>
</form>
This gives me the days of the week for the current month, but it shows all days. How can I only show Monday - Friday?
It looks like $weekday is getting the names for you. Just do a nocase string compare:
$weekday = date('D', strtotime($tmp_date));
if (strcasecmp($weekday, 'Sun') != 0
&& strcasecmp($weekday, 'Sat') != 0){
// Do something with valid days
}
<?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i; ?>
<?php if (!in_array(date('w', strtotime($tmp_date)), array(0, 6)) { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
$weekday = date('D', strtotime($tmp_date));
echo $weekday." "; ?>
<?php echo $i; ?>
</option>
<?php } ?>
This is a lot clearer then strtotime():
$start = DateTime::createFromFormat('Y-n-j', $_SESSION['year'].'-'.$_SESSION['month'].'-01');
$daysInMonth = $start->format('t');
$end = new DateTime("+{$daysInMonth} Days");
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $day) {
if (in_array($day->format('D'), array('Sat', 'Sun'))) continue;
printf('<option value="%s"%s>%s %u</option>',
$day->format('j'),
($_SESSION['day'] == $day->format('j')) ? ' selected' : '',
$day->format('D'),
$day->format('j')
);
}
Demo
This works for me:
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) {
$tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date));
if (strcasecmp($weekday, 'Sun') != 0
&& strcasecmp($weekday, 'Sat') != 0){ ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php echo $weekday." ".$i; ?>
<?php } ?>

Open an HTML select tag after one option has been selected

I have an HTML select tag like this:
<select name="since_date">
<option value="<?php $sixmonths = date('Y-m-d', strtotime('-6 months')); echo $sixmonths; ?>">6 months</option>
<option value="<?php $fivemonths = date('Y-m-d', strtotime('-5 months')); echo $fivemonths; ?>">5 months</option>
<option value="<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>">4 months</option>
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
</select>
And I want when for example 4 months is selected a new select tag to appear like this one:
<select name="until_date_4months">
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
<option value="<?php $now = date('Y-m-d'); echo $now; ?>">Now</option>
</select>
Every time a period is chosen (Ex. 6 months, 5 months) a new select must appear with options lower than the selected one plus a new option "Now" like above. How is this possible? What is the approach? I want to use these information with a submit button. (Send with JQuery a GET request to a specific PHP file <- this I know how to handle).
I can for example use JavaScript like this:
function sinceDateValue(selection) {
if (selection.value == "<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>") {
document.getElementbyId(until_date_4months).style.display = "block"
}
}
and use for the second submit id="until_date_4months" and style="display: none;". Also for the first submit onchange="testValue(this); but this will just show the submit and I want to send only two information with the form: since_date and until_date...
You can create the options dynamically using a loop:
<?php for ($i = 6; $i >= 0; $i--) { ?>
<option value="<?php echo date('Y-m-d', strtotime('-' . $i . ' months')); ?>">
<?php echo ($i == 0) ? 'Now' : $i . ' months'; ?>
</option>
<?php } ?>
As for the jQuery, you can grab the elements based on the one selected, and populate your select box with those:
$('select[name="since_date"]').on('change', function() {
var newOpts = $('option:selected', this).nextAll().clone();
$(newOpts).show().appendTo('#example');
});
Here's a complete example fiddle
You can do smomething like this, all you need to do is set the right values of the options.
$("#one").change(function(){
var htmlOf2 = "", d;
for(var i=1;i<parseInt($(this).find("option:selected").text()) + 1;i++)
{
d = new Date();
d = d.setMonth(d.getMonth() + (i * -1));
// This sets the right month, but is not set as a value yet. I don't know what format you want
htmlOf2 += "<option value=''>" + (i + " months") + "</option>";
}
htmlOf2 += "<option>NOW</option>";
$("#two").html(htmlOf2);
}).trigger("change");
Live fiddle: http://jsfiddle.net/5qaw7/1/

Select box: how to populate days php

Recently i ask how to populate years and i thought that i have all the answer i need. Thing is that i have a new problem with days. The problem seems to be more of mysql than php. In my table users, birthday field, i put "date" as the type which is in this format: 0000-00-00. Now when i use the current select box "day", it inserts 2 instead of 02 and then compromises the whole format when i join $year.$month.$day giving me and output like 1988-11-. How can i solve this? This is the code for days (the year is similar, and it's in the link above). Thanks
<?
$bday = $r['birthday'];
$part = explode("-", $bday);
$year = $part[0];
$month = $part[1];
$day = $part[2]; ?>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo $i. '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo $i;?></option> <? }?>
</select>
You can try
$bday = $r['birthday'];
$part = explode("-", $bday);
$year = $part[0];
$month = $part[1];
$day = $part[2]; ?>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo str_pad($i, 2, "0", STR_PAD_LEFT) . '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo str_pad($i, 2, "0", STR_PAD_LEFT);?></option> <? }?>
</select>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo ($i < 10 ? '0' . $i : $i). '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo $i;?></option> <? }?>
</select>

Categories