I am attempting to populate a table with values listed by date using PHP. I set the value of my dateSelected variable to $_POST and have the date range picker set to DD/MM/YYYY. Below is the setting of variable.
if ( isset($_POST['date'])){
$DateSelected = $_POST['date'];
}else {
unset($DateSelected);
$DateSelected = date('j/m/Y');
}
The only time I can display my table is when I flip the format during the call of the function in the controller. Like below -
GetHourlyPerformance(DateTime::createFromFormat('m/j/Y',
$DateSelected)->format('j/m/Y')
Obviously when I select a row that has a day after the 12th of the month. Nothing will be returned. I for the life of me cannot figure out where I am going wrong or where I should format the date. As it is now without the re-format at the controller it reads as m/j/Y.
The var_dump for the $_POST variable is as follows -
array(4) { ["workcenter"]=> string(2) "15" ["Week"]=> string(2) "37" ["date"]=> string(10) "11/09/2016" ["Shift"]=> string(1) "0" }
So the date string is being read correctly, yet nothing displays in the table.
<?php
function GetHourlySumary($date,$Workcenter, $Shift){
try {
$dbh = new PDO('sqlsrv:Server=etaddressOfTargt; Database=CPB','$dbn','$db');
$sql = "SELECT * FROM HourlySummary WHERE Workcentre = (?) AND Shift = (?) AND ProductionDate= (?) order by ProcessDate , ProcessHour" ;
$stmt = $dbh->prepare($sql);
$params = array($Workcenter,$Shift, $date);
$stmt->execute($params);
$cuRow = 0;
$toReturn = array();
while( $row = $stmt->fetch() ) {
$toReturn [$cuRow] ["Shift"] = $row['Shift'] ;
$toReturn [$cuRow] ["Hour"] = $row['ProcessHour'] ;
$toReturn [$cuRow] ["Quantity Produced"] = $row['QuantityProduced'] ;
$toReturn [$cuRow] ["No WIPS"] = $row['ReelsProduced'] ;
$toReturn [$cuRow] ["Reams Produced"]= $row['ReamsOut'] ;
$toReturn [$cuRow] ["Quantity Broke"] = $row['QuantityBroke'] ;
$toReturn [$cuRow] ["Target Broke"] = $row['TargetBroke'] ;
$toReturn [$cuRow] ["Mins Down"] = $row['MinutesDown'] ;
$cuRow++;
}
$dbh = null;
return $toReturn;
} catch (PDOException $e) {
print "Erreur !: " . $e->getMessage() . "<br/>";
die();
}
}
?>
Above is the code for the model of and below is the view - Somewhere in there the value of $dateSelected changes.
<div class="container">
<form method= 'post' action= "" id = "form">
<div class='row'>
<div class='col-md-2'>
Workcentre :
<SELECT name="workcenter" class="form-control" onchange="this.form.submit()">
<?php for($i = 0; $i < sizeof($Workcenters);$i++)
{
if ($i == $WorkcenterSelected ) $selected = 'selected';
else $selected = '';
echo "<option value = {$i} {$selected}>{$Workcenters[$i]} </option>";}?>
</SELECT>
</div>
<div class='col-md-2'>
Week :
<SELECT name="Week" class="form-control" onchange="this.form.submit()">
<?php for($i = 1; $i <= date('W');$i++)
{
if ($i == $WeekSelected ) $selected = 'selected';
else $selected = '';
echo "<option value = {$i} {$selected}> Week {$i} </option>";
}
?>
</SELECT>
</div>
<div class='col-md-4'></div>
<input type="text" id = "selectDate" name='date' class="form-control" style= "display: inline" value=<?php echo $DateSelected?> />
<input type="text" id = "selectShift" name='Shift' class="form-control" style= "display: none" value=<?php echo $ShiftSelected?> />
<div class='col-md-4'>
<?php if(isStatusUp($Workcenters[$WorkcenterSelected]))
{
echo '<div class="alert alert-success" style="text-align:center;">
<strong>Status :</strong> Up
</div>';
}else{
$Status = getStatus($Workcenters[$WorkcenterSelected]);
echo "<div class='alert alert-danger' style='text-align:center;'>
<strong>Status :</strong> ".$Status."
</div>";
}
?>
</div>
</div>
</form>
<br/>
<table class="table table-striped table-bordered">
<tr>
<th>Shift</th>
<th>QTY Produced</th>
<th>No. of WIPs</th>
<th>Reams Produced</th>
<th>QTY Broke</th>
<th>Target Broke</th>
<th>Mins Down</th>
</tr>
<?php
foreach ($ShiftSumaryTab as &$row)
{
$date = DateTime::createFromFormat('Y-m-d', $row['Production Date']);
echo
"<tr class='table-row' data-date={$date->format('d/m/Y')} data-shift ={$row['Shift']} >
<td>{$row['Shift']} {$date->format('D M-d')} </td>
<td>{$row['Quantity Produced']}</td>
<td>{$row['No WIPS']}</td>
<td>{$row['Reams Produced']}</td>
<td>{$row['Quantity Broke']}</td>
<td>{$row['Target Broke']}</td>
<td>{$row['Minutes Down']}</td>
</tr>";
}
echo
"
<tr>
<td bgcolor='#b3d9ff'><strong> Week : {$WeekSelected} </strong></td>
<td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Quantity Produced']}</strong></td>
<td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['No WIPS']}</strong></td>
<td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Reams Produced']}</strong></td>
<td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Quantity Broke']}</strong></td>
<td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Target Broke']}</strong></td>
<td bgcolor='#b3d9ff'><strong> {$SumShiftSumaryTab['Minutes Down']}</strong></td>
</tr>";
?>
</table>
</div>
<script type="text/javascript">
$(function() {
$(".table-row").click(function() {
$("#form").attr('action', '?action=HS');
$("#selectDate").val($(this).data("date"));
$("#selectShift").val($(this).data("Shift"));
$("#form").submit();
});
});
</script>
For those interested, it was formatted wrongly when performing the query, changed the format to read Y-m-d and there we have it.
$HourlyPerformanceTab =
GetHourlyPerformance(DateTime::createFromFormat('d-m-Y',$DateSelected)->format('Y-m-d'),
$Workcenters[$WorkcenterSelected], $Hours[ $HourSelected] );