Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-08-25
5
Medium Priority
?
49 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

636 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