Question

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

I Have all it work. Only 1 PROBLEM.. I CANNOT DISPLAY the selected RESPO/OFFICE. As I select the RESPO/OFFICE it will sort out the selected RESPO/OFFICE... What should I resvise my code or add code.. Please Help!

Was it helpful?

Solution

A few suggestions first:

  • mysql is depreciated and you should move to PDO prepared statements or mysqli. You can consult the php manual for this.
  • comment your code, you are not able to follow along unless you read from top to bottom.

Now onto your code:

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

After reviewing the above code it is clear that you haven't setup a query to filter the data that you are displaying based on the selected option.

Your first query:

// 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");

You could append a new variable for the selected option, expand your WHERE statement. WHERE monthname(date_added) = '$month' && respo = '$respo' and move this query after the select option so it has access to the value chosen by the select option. Lastly, unless you are going to use jQuery to deal with the select box changing values, you would need a submit button to show the state change for the select option.

Pseudo Code:

  • Check for Submit

  • Change value of the department in the query WHERE clause

  • Display the requested data

    This should get you in the direction you need to go in order to achieve the desired result.

EDIT:

In the 3rd answer you posted:

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

You said it is not filtering by the month being passed in, you are not adding it into your query, specifically the WHERE clause. So it should be:

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

FINAL EDIT:

Your first issue stems from not sending the $_GET['date_added'] value to the view page, so you will never display anything. Properly format the select box and add the data for the RESP and Date data from the database:

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

Now that we are passing the respo and the date string we can work with the data on the next page:

/* 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 />";

This is how I choose to work with the data and get what I needed, so now the query would be:

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

This will only display the records for the RESPO and the date by year and month passed into the page via $_GET.

OTHER TIPS

<?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);
?>

This is the code i come up. This is just an alternative solution. But the problem with this solution is that. I won't display the record in MONTH as call. THe respo is working as I select the RESPO it will display the all RESPO I ask, but the problem is that it include all the previous months with the RESPO I select. When I just select the specific month only..

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

I just rushing to came up with solution.. So I add a and I create another view.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top