0

I have a database which holds calendar events. On handle_events.php?load=1, I want the file to echo all the events. If things like 'start=20190121' or 'end=20190121' are appended to the url, I want the file to echo events only that are in this timeline. The solutions I have thought of were:

  1. write the entire query in each if-else for every possible settings. -> This would be almost impossible if the options increase.

  2. set

    $query="select from sometable where first condition"

    and append "AND option=value" on each option addition -> This would be acceptable on multiple options, but what if there is not a single necessary condition? then the statement would be like "~ where AND option=value" and would raise a syntax error.

These two ways both have drawbacks and look somewhat primitive. Would there be a more elegant way to do this?

3
  • Are you expecting the url query to be two possible states? If so I have shown a solution below. If not I think the question in unclear Commented Jan 21, 2019 at 5:03
  • @Burndogz I mean if I want a few different restrictions to fetch data at different situations, what would be the best way to include that restrictions only to the query? Refering to my example, the query can be appended by start=20190121 or end=20190121 or both: start=20190121&end=20190121. Commented Jan 21, 2019 at 5:35
  • ‘start=20190121' or 'end=20190121' Or your default state... Yes, use conditional statements based on the url parameters and have properly associated queries. It’s the best way as those are the literal variables in the condition you are looking to solve. Commented Jan 21, 2019 at 5:41

2 Answers 2

1
$where=""
if(isset($_GET[‘start’]))
{
if($where=='')
{
$where.=" option=value"
}
else
{
$where.="AND  option=value"
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

Create a conditional statement that checks If the $_GET[‘your date’] is present and the format your query accordingly.

if(isset($_GET[‘start’])) {
 Show one query condition
} else {
 Show default state of query
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.