简体   繁体   中英

php value from db table subtracted by multiple row values in different table

I am struggling to work out how to get the original entitlement figure from table1 and then subtract the 'days' from each request from table2 in start date order.

1st table gets their entitlement figure

2nd table gets the requests

Any guidance would be appreciated, the output i am looking for is:

Start Date       End Date       Days to be taken        Days remaining

-                   -                   -                      22

01 Sep 2011      03 Sep 2011            3                      19
10 Sep 2011      11 Sep 2011            2                      17

Here is my code:

$is_business_result = mysql_query('SELECT * FROM holiday_entitlement_business_manual WHERE employee = \'' . $username . '\' AND academic_year = \'' . $acyear . '\' ');

if($is_business = mysql_fetch_array($is_business_result)) {
echo '<div style="float:left; width:400px;">';
echo '<table width="100%">
<tr>
<td><strong>Name:</strong></td>
<td>'.$is_business['employee'].'</td>
</tr>
<tr>
<td><strong>Entitlement:</strong></td>
<td>'.$is_business['entitlement'].' '.$is_business['units'].'</td>
</tr>
<tr>
<td><strong>Department / Division:</strong></td>
<td>'.$is_business['division'].'</td>
</tr>
<tr>
<td><strong>Line Manager:</strong></td>
<td>'.$is_business['line_manager'].'</td>
</tr>
</table>';
echo '<br/>';
echo '<br/>';
echo '<br/>';    
echo '</div>';





echo '<table width="100%">
<tr>
<td><strong>Start Date</strong></td>
<td><strong>End Date</strong></td>
<td><strong>Days to be taken</strong></td>
<td><strong>Days remaining</strong></td>
</tr>';

echo '<tr>';

echo '<td>-</td>';
echo '<td>-</td>';
echo '<td>-</td>';
echo '<td>'.$is_business['entitlement'].'</td>';

echo '</tr>';







}

$requests_result = mysql_query('SELECT * FROM requests WHERE employee = \'' . $username . '\' AND approved = 1 AND academic_year = \'' . $acyear . '\' ORDER BY start_date ASC');

while($requests = mysql_fetch_array($requests_result)) {

$start_date = new DateTime($requests['start_date']);
$end_date = new DateTime($requests['end_date']);

$timestamp_start_date = $start_date->getTimestamp();
$timestamp_end_date = $end_date->getTimestamp();

$formatted_start_date = date("d M Y", $timestamp_start_date);           
$formatted_end_date = date("d M Y", $timestamp_end_date);           

echo '<tr>';
echo '<td>'.$formatted_start_date.'</td>';
echo '<td>'.$formatted_end_date.'</td>';
echo '<td>'.$requests['days'].'</td>';
echo '</tr>';
}

echo'</table>';

TABLES

CREATE TABLE IF NOT EXISTS `requests` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `academic_year` varchar(255) NOT NULL,
  `employee` varchar(255) NOT NULL,
  `start_date` varchar(255) NOT NULL,
  `end_date` varchar(255) NOT NULL,
  `days` varchar(255) NOT NULL,
  `user` int(20) NOT NULL,
  `approved` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

CREATE TABLE IF NOT EXISTS `holiday_entitlement_business_manual` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `academic_year` varchar(255) NOT NULL,
  `employee` varchar(255) NOT NULL,
  `full_part_time` varchar(255) NOT NULL,
  `weekly_hours` varchar(255) NOT NULL,
  `division` varchar(255) NOT NULL,
  `date_of_commencement` varchar(255) NOT NULL,
  `entitlement` varchar(255) NOT NULL,
  `units` varchar(255) NOT NULL,
  `line_manager` varchar(255) NOT NULL,
  `length_of_service` varchar(255) NOT NULL,
  `band` varchar(255) NOT NULL,
  `new_entitlement` varchar(255) NOT NULL,
  `weekly_entitlement` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=227 ;

我想员工是一个外键,如果它是,那么您可以基于员工加入两个表,然后获取所有数据并从php的权利中减去天数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM