wordpress create custom filter - php

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!

Related

Validate Date input not empty and not greater than todays date in Php

Help please, am trying to validate a form date input not empty and should not be greater that today's date. this is what I did so far. am getting 000-00-00 inserted in MySQL db. what am I doing wrong?
here is what in the form input
<div class="form-group">
<label>Join Date</label>
<input type="date" name="joindate" class="form-control <?php echo (!empty($joindate_err)) ? 'is-invalid' : ''; ?> " value="<?php echo $joindate ?>">
<span class="invalid-feedback"><?php echo $joindate_err; ?></span>
</div>
the php tag above has this validations
//date validation
$input_joindate = trim($_POST["joindate"]);
if (empty($input_joindate)) {
$joindate_err = "Select join date";
}
if (!empty($input_joindate)) {
$input_joindate = date('Y-m-d', strtotime($input_joindate));
$today = strtotime("now");
if (($input_joindate) > $today)
$joindate_err = "Date should not be in the future";
} else {
$joindate = $input_joindate;
}
<?php
$today = date("Y-m-d");
$joindate = $_POST["joindate"];
if (empty($joindate) || !isset($joindate)) {
$joindate_err = "Select join date";
} elseif (strtotime($today) < strtotime($joindate)) {
$joindate_err = "Date should not be in the future";
}

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>

Cannot set email variable from session so mail() function nto working

I'm very new to php and sql and I'm trying to set a _SESSION variable for my user_email, I know that the column name is correct and I'm pulling the other information from the session like the user_id etc correctly but cannot seem to get the email to set even though I've done it the same as my other variables. I'm also trying to use the mail() function but I'm not sure if this is set up correctly? Any help would be really appreciated!
I've tried doing the variable as a _POST one using a hidden input but that hasn't worked either.
<div>
<?php
if (isset($_POST['request_date'])) {
$user_email = $_SESSION['user_email'];
$user_id = $_SESSION['user_id'];
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
// $request_time = date("d-m-Y H:i:s");
$lend_status = 1;
$requested_start_date = date('Y-m-d H:i:s', strtotime($_POST['requested_start_date'] . ' ' . $_POST['requested_start_time'] . ':00:00'));
$requested_end_date = date('Y-m-d H:i:s', strtotime($_POST['requested_end_date'] . ' ' . $_POST['requested_end_time'] . ':00:00'));
$comments = $_POST['comments'];
// Declare available laptops array
$available_laptops = array();
// GET ALL LAPTOPS IN OPERATION
$STH = $DBH->prepare("
SELECT laptop_id,
laptop_name
FROM laptops
WHERE 1
");
$STH->execute(array());
while ($row = $STH->fetch()) {
// CHECK EACH LAPTOP FOR THE REQUESTED DATES
$STH2 = $DBH->prepare("
SELECT lend_id
FROM laptop_system
WHERE (
(
approved_start_date <= ?
AND approved_end_date >= ?
) OR (
approved_start_date <= ?
AND approved_end_date >= ?
) OR (
approved_start_date >= ?
AND approved_end_date <= ?
)
)
AND laptop_id = ?
");
$STH2->execute(array(
$requested_start_date,
$requested_start_date,
$requested_end_date,
$requested_end_date,
$requested_start_date,
$requested_end_date,
$row->laptop_id
));
// IF IT'S NOT BOOKED OUT, ADD TO ARRAY
if ($STH2->rowCount() < 1) {
$available_laptops[$row->laptop_id] = $row->laptop_name;
}
}
if (empty($available_laptops)) {
echo '<h3>Sorry, this date is not available.</h3>';
} else {
$STH = $DBH->prepare("
INSERT INTO laptop_system (
user_id,
first_name,
last_name,
lend_status,
requested_start_date,
requested_end_date,
comments
)
VALUES(?, ?, ?, ?, ?, ?, ?)
");
$STH->execute(array(
$user_id,
$first_name,
$last_name,
$lend_status,
$requested_start_date,
$requested_end_date,
$comments
));
echo '<h2 style="color:#D80B8C; margin-bottom:1em;">' . $first_name . ', your laptop request is now pending approval.</h2>';
$to = $user_email;
$subject = "Laptop Request";
$message = "Thank you for your laptop request for " . $requested_start_date . " - " . $requested_end_date . "It is now pending and you will be notified if it's been approved or declined.";
$message = wordwrap($message,70);
$headers = "From: Timmy and Edwardo";
mail($to,$subject,$txt,$headers);
}
} ?>
<form action="" method="post" >
<div>
<label for="requested_start_date"> Requested Start Date </label>
<input type="date" name="requested_start_date" value="<?php echo $requested_start_date; ?>">
<label for="requested_start_time">Requested start time </label>
<select name="requested_start_time" style="width:auto;margin:1em 1em 1em 0;">
<?php for ($i = 0; $i < 25; $i++) {$i = str_pad($i, 2, "0", STR_PAD_LEFT); ?>
<option value="<?php echo $i; ?>"><?php echo $i . ':00'; ?></option>
<?php } ?>
</select>
</div>
<div>
<label for="requested_end_date">Requested End Date </label>
<input type="date" name="requested_end_date" value="<?php echo $requested_end_date; ?>">
<label for="requested_end_time">Requested end time</label>
<select name="requested_end_time" style="width:auto;margin:1em 1em 1em 0;">
<?php for ($i = 0; $i < 25; $i++) {$i = str_pad($i, 2, "0", STR_PAD_LEFT); ?>
<option value="<?php echo $i; ?>"><?php echo $i . ':00'; ?></option>
<?php } ?>
</select>
</div>
<div>
<p style="margin-bottom:0;">
Please can you let us know below why you need the laptop and if there are any special requirements needed -
</p>
<input type="textarea" rows="4" cols="50" name="comments" placeholder="" required>
<input type="submit" name="request_date" value="Request Date">
<!-- <input type="hidden" name="user_email" value="<?php echo $user_email;?>" > -->
</div>
</form>
<?php
?>
</div>

date input field should be optional in php

Hi guys i have a from where user is going to select a course from drop down and second input user will select expired date that should be optional.so when user didn't give date it is showing error as Your Due date should be in future.
I don't know why it is showing like that if i didn't give any date in input field also.
here is my code:
if (isset($_POST['assigncourse'])) {
$courseid = $_POST['courseid'];
//$duedate = date('Y-m-d', strtotime($_POST['duedate']));
$duedate = strtotime($_POST['duedate']);
$now = strtotime(date('Y-m-d'));
if ($now < $duedate)
{
$courseid = required_param('courseid', PARAM_TEXT);
$groupid = required_param('groupid', PARAM_TEXT);
$insert_record = new stdClass();
$insert_record->courseid = $courseid;
$insert_record->groupid = $_SESSION['groupid'];
$insert_record->duedate = $duedate;
$insert_record->createdby= $id;
$insert_record->createdon = date('Y-m-d H:i:s');
$sql1 = "SELECT *
FROM mdl_ppc_assigncourses_group
WHERE groupid='".$_SESSION['groupid']."'
AND courseid='$courseid'";
$courses = $DB->get_records_sql($sql1);
if (count($courses) > 1) {
$errorMessage = "The course already taken!";
} else {
$DB->insert_record('ppc_assigncourses_group', $insert_record);
}
} else {
$errorMessage = "Your Due date should be in future.";
}
}
Here is my form:
<!-- /.card-header -->
<form class="form-horizontal" id="sign-in" style="margin-left: -2px; padding: 15px 15px 0px 15px" method="post" role="form" data-parsley-validate="" novalidate="">
<input type="hidden" name="groupid" class="col-md-12 form-control" id="inputEmail3" required="" value="<?php echo $_SESSION['groupid']; ?>" >
<!-- select -->
<input type="hidden" name="depid" class="col-md-12 form-control" id="inputEmail3" required="" value="<?php echo $_SESSION['depid']; ?>" >
<div class="form-group">
<label>Choose Course</label>
<select name="courseid" id="courseid" class="form-control" required>
<?php
$sql = "select id,fullname from {course} where id!='1' ";
$courses = $DB->get_records_sql($sql);
?>
<option value="">Choose Course</option>
<?php
if (sizeof($courses)): foreach ($courses as $row):
$coursename = $row->fullname;
?>
<option value="<?php echo $row->id; ?>"><?php echo $row->fullname; ?></option>
<?php
endforeach;
endif;
?>
</select>
</div>
<div class="form-group label-floating">
<label class="control-label">Due Date</label>
<input type="text" class="form-control" id="datepicker" name="duedate" >
</div>
<div class="form-group">
<button type="submit" name="assigncourse" id="submit" class="btn btn-info form-control">Assign</button>
</div>
</form>
Can anyone help me what mistake i have done.
Thanks in advance.
The problem is if duedate is not set. $now cannot be smaller than duedate.
This line:
if ($now < $duedate)
you will have to change. Make it so it looks if $duedate is set in first place.
if (isset($_POST['assigncourse'])) {
$courseid = $_POST['courseid'];
//$duedate = date('Y-m-d', strtotime($_POST['duedate']));
if(isset($_POST['duedate']) && ($_POST['duedate'] != "")) {
$duedate = strtotime($_POST['duedate']);
}
else {
$duedate = false;
}
$now = strtotime(date('Y-m-d'));
if ((!$duedate) || ($now < $duedate))
{
$courseid = required_param('courseid', PARAM_TEXT);
$groupid = required_param('groupid', PARAM_TEXT);
$insert_record = new stdClass();
$insert_record->courseid = $courseid;
$insert_record->groupid = $_SESSION['groupid'];
if(is_numeric($duedate)) {
$insert_record->duedate = $duedate;
}
else {
$insert_record->duedate = time();
}
$insert_record->createdby= $id;
$insert_record->createdon = date('Y-m-d H:i:s');
$sql1 = "SELECT *
FROM mdl_ppc_assigncourses_group
WHERE groupid='".$_SESSION['groupid']."'
AND courseid='$courseid'";
$courses = $DB->get_records_sql($sql1);
if (count($courses) > 1) {
$errorMessage = "The course already taken!";
} else {
$DB->insert_record('ppc_assigncourses_group', $insert_record);
}
} else {
$errorMessage = "Your Due date should be in future.";
}
}
ViewPage:
<td><?php if($duedate > 0) { echo date('Y-M-d',$duedate); } ?></td>
You need to check if the $duedate has a value. You would do that like this.
if(isset($duedate) && $duedate){
if(strtotime($now) < strtotime($duedate)){
echo 'Do something';
}else{
echo 'Do something else.';
}
}else{
echo 'You need to enter a due date.';
}
Your code would look like this:
if (isset($_POST['assigncourse'])) {
$courseid = $_POST['courseid'];
//$duedate = date('Y-m-d', strtotime($_POST['duedate']));
//$duedate = strtotime($_POST['duedate']);
$now = date('Y-m-d');
if(isset($_POST['duedate']) && $_POST['duedate']){
if(strtotime($now) < strtotime($_POST['duedate']))
{
$courseid = required_param('courseid', PARAM_TEXT);
$groupid = required_param('groupid', PARAM_TEXT);
$insert_record = new stdClass();
$insert_record->courseid = $courseid;
$insert_record->groupid = $_SESSION['groupid'];
$insert_record->duedate = $duedate;
$insert_record->createdby= $id;
$insert_record->createdon = date('Y-m-d H:i:s');
$sql1 = "SELECT *
FROM mdl_ppc_assigncourses_group
WHERE groupid='".$_SESSION['groupid']."'
AND courseid='$courseid'";
$courses = $DB->get_records_sql($sql1);
if (count($courses) > 1) {
$errorMessage = "The course already taken!";
}else{
$DB->insert_record('ppc_assigncourses_group', $insert_record);
}
}else{
$errorMessage = "Your Due date should be in future.";
}
}
}

create drop down to view month and year in 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

Categories