Solved

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

Posted on 2016-08-25
5
39 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

717 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