简体   繁体   中英

how can i make this dynamic BETWEEN '2014-07-01 00:00:00' AND '2014-07-31 23:59:59'";

Im still a noob php programmer. Please help me with my problem. I was trying to code a timeline. Im stuck at the query part. Let's say i have a select element with the months in it. If ever I want to select the July, the timeline displays all the events listed on July 2014. What if its already 2015? my query remains static (BETWEEN '2014-07-01 00:00:00' AND '2014-07-31 23:59:59'";)

<?php

mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("prototype") or die(mysql_error());


if(isset($_POST['submit'])) {
    if($_POST['month'] == 'July') {


        $sql = "SELECT * FROM events WHERE event_schedule BETWEEN '2014-07-01 00:00:00' AND '2014-07-31 23:59:59'";
        $query = mysql_query($sql);


        while ($row = mysql_fetch_array($query)) {
            echo "Title:".$row['event_title']."<br>";
            echo "Description:".$row['event_description']."<br>";
            echo "Schedule:".$row['event_schedule']."<br>";
            echo "Date Created:".$row['date_created']."<br>";
            echo "Posted By:".$row['posted_by']."<br>";
        }
    }

    if($_POST['month'] == 'August') {
        $sql = "SELECT * FROM events WHERE event_schedule BETWEEN '2014-08-01 00:00:00' AND '2014-08-31 23:59:59'";
        $query = mysql_query($sql);


        while ($row = mysql_fetch_array($query)) {
            echo "Title:".$row['event_title']."<br>";
            echo "Description:".$row['event_description']."<br>";
            echo "Schedule:".$row['event_schedule']."<br>";
            echo "Date Created:".$row['date_created']."<br>";
            echo "Posted By:".$row['posted_by'];
        }
    }
}

echo date("F Y");




?>

<html>
    <form method="post">
        <select name="month">
            <option selected="selected">Select a month...</option>
            <option>January</option>
            <option>February</option>
            <option>March</option>
            <option>April</option>
            <option>May</option>
            <option>June</option>
            <option>July</option>
            <option>August</option>
            <option>September</option>
            <option>October</option>
            <option>November</option>
            <option>December</option>
        </select>
        <input type="submit" name="submit" value="Submit">
    </form>
</html>

You can try something like this

$sql = "SELECT * FROM events WHERE event_schedule LIKE '%-07-%'";

You can embed PHP vars into your query text. So you can pull the current year with date():

$queryYear = date('Y');

$sql = "SELECT * FROM events WHERE event_schedule BETWEEN '{$queryYear}-07-01 00:00:00' AND '{$queryYear}-07-31 23:59:59'";

As already been commented you should consider in changing your code to work with MYSQLi or PDO functions. Mysql_* functions is deprecated.

As for your answer I would recomment two things.

1 - Add the month values on your html form

<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
...

2 - Change your PHP with just on set of code, you don't need to test each case and repeat your code. Something like this:

if(isset($_POST['submit'])) {


    /*AGAIN THIS IS NOT RECOMMENDED AS IT CAN BE SQLInjected*/
    $sql = "SELECT * 
              FROM events ";
    if ($_POST['month']!=''){
      $sql .= " WHERE extract(YEAR from event_schedule) = extract(YEAR from now())
                  and extract(MONTH from event_schedule) = $_POST['month']";
    }

    $query = mysql_query($sql);

    while ($row = mysql_fetch_array($query)) {
        echo "Title:".$row['event_title']."<br>";
        echo "Description:".$row['event_description']."<br>";
        echo "Schedule:".$row['event_schedule']."<br>";
        echo "Date Created:".$row['date_created']."<br>";
        echo "Posted By:".$row['posted_by']."<br>";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM