1
<?php require_once('../includes/header.php');?>
<?php require_once('../includes/connection.php');?>
<?php include('../includes/get_username.php');?>

<?php
include('sanitise.php');
$month = sanitise($_GET['month']);
?>

<table id="contentbox">
<tr>
<td>
<?php
$color="1";
//view record
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";

//RESPO/Office
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);

echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";

while ($row = mysql_fetch_array($qry))  {
$dv_id = $row['dv_id']; 
if($color==1){
echo "<tr bgcolor='#ffffff'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......

$color="2";
}   else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>

我有它的工作。只有 1 个问题.. 我无法显示所选的 RESPO/OFFICE。当我选择 RESPO/OFFICE 时,它会整理出选定的 RESPO/OFFICE... 我应该修改我的代码或添加代码.. 请帮助!

4

3 回答 3

1

先提几点建议:

  • mysql 已折旧,您应该转到 PDO 准备好的语句或 mysqli。您可以查阅 php 手册。
  • 注释您的代码,除非您从上到下阅读,否则您无法跟进。

现在进入您的代码:

<?php
 // Start document with includes needed, header, connection, functions.
 require_once('../includes/header.php');
 require_once('../includes/connection.php');
 include('../includes/get_username.php');   
 include('sanitise.php');

// Start with the month variable posted from $_GET, this will begin the initial display.
$month = sanitise($_GET['month']);
?>

<!-- Begin Table Display and Layout -->
<table id="contentbox">
<tr>
<td>

<?php // Open PHP Tag to get variables to display in the table

$color="1";

// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");

// Format the table rows for display
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";

// Fill the SELECT option with all the RESPO/Office data
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);

// Display the select with the newly populated content
echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
// While the result contains data we will display respo as a select option.
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}

// Finish the table header
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";

// While we have data from the original query we will display it
while ($row = mysql_fetch_array($qry))  {
$dv_id = $row['dv_id']; // grab the dv_id
if($color==1){ // Setup color scheme
echo "<tr bgcolor='#ffffff'> // Color for the row
// Let's begin filling the table with data
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......

$color="2";
}   else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>

查看上述代码后,很明显您尚未设置查询来根据所选选项过滤您正在显示的数据。

您的第一个查询:

// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");

您可以为所选选项附加一个新变量,展开您的WHERE语句。WHERE monthname(date_added) = '$month' && respo = '$respo'并将此查询移到 select 选项之后,以便它可以访问 select 选项选择的值。最后,除非您打算使用 jQuery 来处理选择框更改值,否则您需要一个提交按钮来显示选择选项的状态更改。

伪代码:

  • 检查提交

  • 在查询 WHERE 子句中更改部门的值

  • 显示请求的数据

    这应该使您朝着实现预期结果所需的方向前进。

编辑:

在您发布的第三个答案中:

$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added  ";

您说它没有按传入的月份进行过滤,您没有将其添加到查询中,特别是 WHERE 子句。所以应该是:

$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND month(month_added)='$month' AND year(date_added)='$year' GROUP BY date_added  ";

最终编辑:

您的第一个问题源于未将$_GET['date_added']值发送到视图页面,因此您将永远不会显示任何内容正确格式化选择框并从数据库中添加 RESP 和日期数据的数据

echo "<option value='". $row['respo'] . "+". $row['date_added'] . "'>" . $row['respo'] . "  (".$row['count(*)'].")</option>";

现在我们传递了 respo 和日期字符串,我们可以处理下一页上的数据:

/* UNCOMMENT NEXT LINE FOR ERROR DETECTION */
//error_reporting( E_ALL );
/* GET THE ENTIRE STRING PASSED FROM THE SELECT OPTION */
$respo = $_GET['respo'];
/* EXPLODE THE DATA BY + SYMBOL TO GET THE DATA */
$data = explode("+", $respo);
/* FORMAT THE DATETIME FROM THE STRING TO A FRIENDLY FORMAT */
$month = date("m", strtotime($data[1])) . "<br />";
$year = date("Y", strtotime($data[1])) . "<br />";

这就是我选择使用数据并获得所需内容的方式,所以现在查询将是:

$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($data[0])."' && year(date_added)='$year' && month(date_added)='$month' ";

这将仅显示RESPO and the date by year and month通过 传入页面的记录$_GET

于 2014-04-29T17:07:31.870 回答
1
//RESPO/Office
**echo '<form action="view.php" method="post">';**
$byrespo = mysql_query("SELECT DISTINCT count(*), respo FROM tbl_dv WHERE monthname(date_added)='$month' GROUP BY respo");
echo "<select name='respo' onchange='this.form.submit()'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($byrespo)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "  (".$row['count(*)'].")</option>";
}
echo "</select></form>";

我只是急于想出解决方案..所以我添加了一个并创建了另一个 view.php

于 2014-04-30T00:40:16.437 回答
1
<?php require_once('../includes/header.php');?>
<?php
$respo = $_GET['respo'];
$month = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('m'));
$year = (int)  (!empty($_GET['date_added']) ? $_GET['date_added'] : date('Y'));

//database call here

$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added  ";
$result = mysql_query($viewrecord, $db) or die (mysql_error());
$num_result = mysql_num_rows($result);
{

echo "<table border='1' width='100%' style='border:1px solid silver' cellpadding='5px' cellspacing='0px'>
<tr bgcolor='#666666' style='color:#FFFFFF'>
<th>Date Encoded</th>
etc...... (header here)

while ($row = mysql_fetch_row($result)) {
echo '<tr>';
echo "<tr ><td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
etc...
}
mysql_close($db);
?>

这是我提出的代码。这只是一种替代解决方案。但是这个解决方案的问题是。我不会将 MONTH 的记录显示为通话。当我选择 RESPO 时,respo 正在工作,它将显示我询问的所有 RESPO,但问题是它包括我选择的 RESPO 的所有前几个月。当我只选择特定月份时..

于 2014-04-30T00:52:13.227 回答