PHP MYSQL链接下拉如何显示数据

时间:2014-04-25 01:40:08

标签: php mysql dynamic-data-display dropdownlistfor

<?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 ...我应该如何恢复我的代码或添加代码..请帮忙!

3 个答案:

答案 0 :(得分: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来处理选择框更改值,否则您需要一个提交按钮来显示select选项的状态更改。

伪代码:

  • 检查提交
  • 在查询WHERE子句
  • 中更改部门的值
  • 显示请求的数据

    这可以让你朝着你需要的方向前进,以达到预期的效果。

修改

在第3个回答中,您发布了:

$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>";

现在我们传递了res​​po和日期字符串,我们可以使用下一页的数据:

/* 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的记录。

答案 1 :(得分: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

答案 2 :(得分: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的所有前几个月。当我只选择特定月份时..