0

我正在使用数据库和 PHP 来管理我的财务。在我的主页上,我想选择一个月份并使其执行查询,以便仅显示该月数据库中的记录。

现在我有这个:

if (isset($_GET['january2013'])) 
{
//Select the incomes
try
    {
        $sql = 'SELECT id, type, date, amount, description, category FROM `transactions`
        WHERE type = "income"
        AND month(date) = ' . $monthselect . '
        ORDER BY `transactions`.`id` DESC
        LIMIT 0,50';
        $result2 = $pdo->query($sql);
    }
//Error handling.
catch (PDOException $e)
    {
        $output3 = 'Error fetching records: ' . $e->getMessage();
        include '/errors/output.html.php';
        exit();
    }
//Display the records.
foreach ($result2 as $row)
    {
    $incomesJan2013[] = array(
            'id' => $row['id'],
            'type' => $row['type'],
            'date' => $row['date'],
            'amount' => $row['amount'],
            'description' => $row['description'],
            'category' => $row['category']
        );
    }

我怎样才能使它更通用,而不是每个月都制作这个代码?我想使用这个$monthselect变量,但我不知道从哪里开始。

4

2 回答 2

0

如果我正确理解您的问题,我认为这就是您想要做的:

if (isset($_GET['date'])) {
    $monthselect = $_GET['date']
}

您需要将$_GET参数发送为?date=youDateHere

然后$monthselect将等于设置的任何日期。

于 2013-06-20T18:58:02.047 回答
0

在您的网页上,有类似的东西

<form action="where_you_handle_your_requests.php" method="POST">
    <select name="mo">
      <option value="january">January</option>
      <option value="february">February</option>
      <option value="march">March</option>
    </select> 
    <select name="yr">
      <option value="2010">2010</option>
      <option value="2011">2011</option>
    </select>
</form>

等等。您可以节省时间并编写脚本来为您生成 HTML。

接下来,在您的 php 脚本上,执行

if(isset($_POST['yr']) && isset($_POST['mo'])){
    $monthselect=$_POST['mo'].$_POST['yr'];
}else{
    //no input passed to script
}

如果用户选择 2013 年 1 月,monthselect 将设置为january2013

于 2013-06-20T19:06:09.047 回答