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

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!

user229044
  • 232,980
  • 40
  • 330
  • 338
  • It is unclear how you are working with the $_POST data that is passed to your PHP code, and you have no code to reflect what happens once the selections are made. Are there AJAX requests that are not posted here that do this and call those functions? – Skewled Apr 25 '14 at 03:39
  • This is the screenshot: http://i599.photobucket.com/albums/tt79/emcevo/respo_help_screenshot_zps6da13b20.png – user3571148 Apr 29 '14 at 11:12

3 Answers3

1

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.

Community
  • 1
  • 1
Skewled
  • 783
  • 4
  • 12
  • Thank you very much. I appreciate it. I will take your advice and thank you for sharing your knowledge. This was a rush project. So I came up with an alter solution. I mange to got the RESPO to display, but I FAILED to display the record in MONTH sort along with the RESPO. How can I do this? – user3571148 Apr 30 '14 at 00:35
  • As I put this code monthname(date_added) = '$month' && respo = '$respo' I got an Undefined index: respo. And I put $respo = $_GET['respo']; still got an error – user3571148 Apr 30 '14 at 00:58
  • @user3571148 I modified my answer at the bottom I believe it is what your problem is. – Skewled Apr 30 '14 at 02:57
  • When i try $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 "; Records are display only the current month. What I mean when I click APRIL, 2014. The April records only works. The rest of the Months "Jan.." wont display. Did I forget something. I think the problem is in this code $month = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('m')); This only displays the current month. So Now is April. The April records only display. The other previous months are not – user3571148 Apr 30 '14 at 05:04
  • So you want all months for the current year to be displayed? If so we can remove the month from the query and get all of the year, I apologize if I'm not understanding you clearly. I'm trying (: – Skewled Apr 30 '14 at 13:44
  • No need to apologize sir. I should thank you instead. I understand. Same here I'm trying. Im sorry too. At least we are trying sir. And in same situation someday we can help people with the same problem encounter as mine. With people like you sir should deserve a +rep.. The process flow of the REPORT Module is in this link: http://i599.photobucket.com/albums/tt79/emcevo/ReportModulePROBLEM_zps7274f0cc.jpg – user3571148 Apr 30 '14 at 21:46
  • Can you provide me with the sql setup please, I want to make this database and test your code please, create the backup and give me a link to it. Provide me with the code you are using as well. I will then create this as a test and modify the code to get the result you want. – Skewled May 01 '14 at 00:11
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>";

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

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

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..

  • i have send the dummy system and database at your email bwj1980@gmail.com. Database name: ptoms. Just use the username: mae and password: mae – user3571148 May 01 '14 at 09:53