?
Solved

on selection of month and year from dropdown not filtering the records

Posted on 2016-08-25
5
Medium Priority
?
53 Views
Last Modified: 2016-09-13
I am working on a website which is in php which conatins the details of tenants and their rent, which contains month and year dropdown, on selection of month and year it should show the records of particular month and year
0
Comment
Question by:deepki karoal
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41770115
Add the month and year columns in where clause of query you are using to fetch records from the database.
0
 

Author Comment

by:deepki karoal
ID: 41770120
thank you...can i get the code for this, as i am new to php stucked in middle, please help me with this
0
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41770135
Can you share code you are using to fetch all records?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points (awarded by participants)
ID: 41770160
Help for those of us new to PHP:
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Here's how to process date/time values in PHP, using procedural code and object-oriented code

An example showing how to use HTML select controls:
https://iconoun.com/demo/temp_deepki.php
<?php // demo/temp_deepki.php
/**
 * https://www.experts-exchange.com/questions/28965568/on-selection-of-month-and-year-from-dropdown-not-filtering-the-records.html
 *
 * https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
 * https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 */
error_reporting(E_ALL);

// THESE VARIABLES ONLY GET SET IF THE REQUEST CONTAINS USEFUL INFORMATION
$omega = NULL;
$where = NULL;

// IF THERE IS A GET REQUEST
if (!empty($_GET))
{
    $month = !empty($_GET['m']) ? (int)$_GET['m'] : 0;
    $year  = !empty($_GET['y']) ? (int)$_GET['y'] : 0;

    // IF BOTH MONTH AND YEAR ARE REQUESTED
    if ($month && $year)
    {
        // NORMALIZE A START AND END FOR THE MONTH AND YEAR TO ISO-8601 STANDARDS
        $alpha_timestamp = strtotime($year . '-' . $month . '-01');
        if ($alpha_timestamp)
        {
            $alpha = date('Y-m-01', $alpha_timestamp);
            $omega = date('Y-m-t',  $alpha_timestamp);
        }
    }

    // IF WE HAVE USEFUL DATES CONSTRUCT A WHERE CLAUSE FOR USE IN A SQL QUERY
    if ($omega)
    {
        $where = " WHERE my_date BETWEEN '$alpha' AND '$omega' ";
    }
}

// END OF PROCESSING ACTION SCRIPT -- SHOW THE WORK PRODUCT
echo $where;


// WHAT YEARS? IN "REAL LIFE" THIS PROBABLY COMES FROM A DATABASE QUERY
$years = new StdClass;
$years->a = date('Y', strtotime('Today - 2 years'));
$years->b = date('Y', strtotime('Today - 1 year'));
$years->c = date('Y', strtotime('Today'));

// CREATE THE YEAR OPTION TAGS USING HEREDOC NOTATION
$year_options = <<<EOD
<option value="$years->a">$years->a</option>
<option value="$years->b">$years->b</option>
<option value="$years->c">$years->c</option>
EOD;

// CREATE THE HTML FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>

<select name="m">
<option selected value="0">Choose Month</option>
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>

<select name="y">
<option selected value="0">Choose Year</option>
$year_options
</select>

<input type="submit" value="Go" />
</form>

ENDFORM;

echo $form;

Open in new window

Depending on the selected inputs, this script outputs something like:
WHERE my_date BETWEEN '2014-10-01' AND '2014-10-31'

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41795686
Tested code example provided.  Author abandoned.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question