Solved

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

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

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 7

Expert Comment

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

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 109

Expert Comment

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

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a limit to how many inner joins you should use? 4 31
preventing spam with mailto 22 31
PHP Syntax Error 4 27
How to change the link of an image using md5 in php ? 3 32
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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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