Solved

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

Posted on 2016-08-25
5
15 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 6

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 6

Expert Comment

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

Accepted Solution

by:
Ray Paseur earned 500 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 108

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now