Compare and Count Variable Dates? - javascript

Using the given statement below I am Receiving the Value of Course end date which is in varchar and coming from MySQL I tried to convert this to date format in $ced & $newformat...all goes fine but when I am trying to compare course end date with the current date it's not comparing... it's only comparing the date and not comparing with year and month
$enddate = $get_students_row['course_end_date'];
$ced = strtotime("$enddate");
$newformat = date('d/m/Y',$ced);
$currentdate = date('d/m/Y');
The function is if $newformat is less than and eqals to current date then field become red
<tr <?php if(($newformat <= $currentdate)): ?> style="color:red;" <?php endif; ?>>
But its not working as excepted its make row red but its only comapring with current date not with month and year....Also i was looking for to get Total count of $newformat.
My Code
<tr <?php if(($ced <= time())): ?> style="color:red;" <?php endif; ?>>
<td><?php echo $students_rollno_class;?></td>
<td><?php echo $students_admission_no;?> </td>
<td><?php echo $students_firstname;?></td>
<td><?php echo $students_contact;?></td>
<td><?php echo $students_reference_no;?></td>
<td><?php echo date('d/m/Y', strtotime($students_date));?></td>
<td><?php echo $newformat;?></td>
</tr><?php
}
?>

You can see the comparison in this national format
$format = "d_m_y";
$date1 = \DateTime::createFromFormat($format, "03_01_12");
$date2 = \DateTime::createFromFormat($format, "31_12_11");
var_dump($date1 > $date2);
Using DateTime::createFromFormat:

$('#timeTable tr td').each(function () {
var dtTd = new Date($(this).html());
var dtNew = new Date();
// 15 minutes is 900000 milliseconds
if (dtTd.getTime() - dtNew.getTime() < 900000 && dtNew < dtTd) {
$(this).parent('tr').addClass('min15');
} else {
if (dtNew > dtTd) {
$(this).parent('tr').addClass('old');
}
}
});
.old {
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table id="timeTable">
<tbody>
<tr>
<td>02/07/2015 23:15</td>
</tr>
<tr>
<td>03/09/2015 11:16</td>
</tr>
<tr>
<td>18/02/2020 11:30</td>
</tr>
</tbody>
</table>

$newformat and $currentdate are just simple strings for PHP. And comparing two strings would not give the answer you expect. So it's better to use UNIX time format.
try this:
<tr <?php if(($ced <= time())){
$totalCount++;
echo 'style="color:red;" ';
<?php } ?>>
My <tr> Code
<tr <?php if(($ced <= time())): ?> style="color:red;" <?php endif; ?>>
<td><?php echo $students_rollno_class;?></td>
<td><?php echo $students_admission_no;?> </td>
<td><?php echo $students_firstname;?></td>
<td><?php echo $students_contact;?></td>
<td><?php echo $students_reference_no;?></td>
<td><?php echo date('d/m/Y', strtotime($students_date));?></td>
<td><?php echo $newformat;?></td>
</tr><?php } ?>

Related

I cant run condition IF [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have code to filter by dates, I have 2 condition,
First condition: get 'tanggal' from date picker on view
Second condition: variable 'tanggal' isNull
However, the code only runs the second condition, even though the condition variable date is given
This is the code:
View:
.... some code ....
<form method="POST">
<div class="form-group">
<label>Date</label>
<div class="input-group date">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
<form method="get">
<input type="date" name="tanggal">
<input type="submit" value="FILTER">
</form>
.... some code ....
<tbody>
<?php
foreach ((array)$getDepartDetail as $us) { ?>
<tr>
<td><?php echo $us->Name ?></td>
<td><?php echo $us->NIK ?></td>
<td><?php echo $us->gender ?></td>
<td><?php echo $us->PositionDesc ?></td>
<td><?php echo $us->Shift ?></td>
<td><?php echo $us->tgl ?></td>
<td><?php echo $us->Attendance ?></td>
<td><a class="btn btn-info" href="<?php echo site_url('data_detail/detail_datas/vieworc/' . $us->NIK); ?>">Detail</a></td>
</tr>
<?php } ?>
</tbody>
.... some code ....
Model:
.... some code ....
public function get_allDprtDetail($DepartmentID)
{
if(isset($_GET['tanggal'])){
$tgl = $_GET['tanggal'];
$datas = "
SELECT DISTINCT
.... some code ....
FROM
emp0001
INNER JOIN emp0003 ON emp0003.DepartmentID = emp0001.DepartmentID
LEFT JOIN v_dvc0004_test ON emp0003.NIK = v_dvc0004_test.NIK
AND DATE(v_dvc0004_test.Enroll) = '$tgl'
JOIN emp0002 ON emp0002.PositionID = emp0003.PositionID
LEFT JOIN shift ON emp0003.Shift_ID = shift.Shift_ID
WHERE
emp0001.DepartmentID = '$DepartmentID'
AND emp0003.IsActive = 'T'
ORDER BY
v_dvc0004_test.Enroll ASC
";}
else{
$datas = "
SELECT DISTINCT
.... some code ....
}
$query = $this->db->query($datas);
return $query->result()
;}
Firstly you should add a check for the results count ...
<tbody>
<?php if(count($getDepartDetail)> 0){ ?>
<?php foreach ($getDepartDetail as $us) { ?>
<tr>
<td><?php echo $us->Name ?></td>
<td><?php echo $us->NIK ?></td>
<td><?php echo $us->gender ?></td>
<td><?php echo $us->PositionDesc ?></td>
<td><?php echo $us->Shift ?></td>
<td><?php echo $us->tgl ?></td>
<td><?php echo $us->Attendance ?></td>
<td><a class="btn btn-info" href="<?php echo site_url('data_detail/detail_datas/vieworc/' . $us->NIK); ?>">Detail</a></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
Function get_allDprtDetail can return null if $_GET['tanggal'] doesn't set set. You may try to return empty array, like that
public function get_allDprtDetail($DepartmentID)
{
if(isset($_GET['tanggal'])){
some code
}
return [];
}

Exporting php data in a loop to CSV file

I have written a code which uses the API of fantasypremierleague.com to extract data. The data is being extracted for 38 gameweeks using a for loop. I print the data using echo to verify that it is correct.
I am having a problem converting the data into CSV files using a script since its in a loop.
After printing information each gameweek i have added a button (in total 38 buttons) which should convert the information of each gameweek to a CSV file.
Currently, all the buttons are printing the information of gameweek 1 even though the script is part of the loop.
ini_set('max_execution_time', 300);
for ($i=1; $i <39 ; $i++) {
$json=file_get_contents("https://fantasy.premierleague.com/drf/entry/1224012/event/".$i."/picks");
$data = json_decode($json, true);
$json1=file_get_contents("https://fantasy.premierleague.com/drf/elements/");
$data1 = json_decode($json1, true);
?>
<table id ="gameweek_history">
<tr>
<th>Average Gameweek Score</th>
<th>Highest Score</th>
<th>Highest Scorer ID</th>
</tr>
</br>
</br>
<td><?PHP echo $data['event']['average_entry_score']; ?></td>
<td><?PHP echo $data['event']['highest_score']; ?></td>
<td><?PHP echo $data['event']['highest_scoring_entry']; ?></td>
</table>
<table id ="gameweek_info">
<tr>
<th>Gameweek</th>
<th>Active Chip</th>
<th>ID</th>
<th>Points</th>
<th>Total Points</th>
<th>Rank</th>
<th>Overall Rank</th>
<th>Gameweek Transfers</th>
<th>Gameweek Transfer Cost</th>
<th>Points on Bench</th>
<th>Bank</th>
</tr>
</br>
</br>
<td><?PHP echo $data['entry_history']['event']; ?></td>
<td><?PHP echo $data['active_chip']; ?></td>
<td><?PHP echo $data['entry_history']['id']; ?></td>
<td><?PHP echo $data['entry_history']['points']; ?></td>
<td><?PHP echo $data['entry_history']['total_points']; ?></td>
<td><?PHP echo $data['entry_history']['rank']; ?></td>
<td><?PHP echo $data['entry_history']['overall_rank']; ?></td>
<td><?PHP echo $data['entry_history']['event_transfers']; ?></td>
<td><?PHP echo $data['entry_history']['event_transfers_cost']; ?></td>
<td><?PHP echo $data['entry_history']['points_on_bench']; ?></td>
<td><?PHP echo $data['entry_history']['bank']; ?></td>
</table>
</br>
</br>
</br>
<script>function doCSV() {
var table1 = document.getElementById("gameweek_history").innerHTML;
var data1 = table1.replace(/<thead>/g, '')
.replace(/<\/thead>/g, '')
.replace(/<tbody>/g, '')
.replace(/<\/tbody>/g, '')
.replace(/<tr>/g, '')
.replace(/<\/tr>/g, '\r\n')
.replace(/<th>/g, '')
.replace(/<\/th>/g, ',')
.replace(/<td>/g, '')
.replace(/<\/td>/g, ',')
.replace(/\t/g, '')
.replace(/\n/g, '');
var table2 = document.getElementById("gameweek_info").innerHTML;
var data2 = table2.replace(/<thead>/g, '')
.replace(/<\/thead>/g, '')
.replace(/<tbody>/g, '')
.replace(/<\/tbody>/g, '')
.replace(/<tr>/g, '')
.replace(/<\/tr>/g, '\r\n')
.replace(/<th>/g, '')
.replace(/<\/th>/g, ',')
.replace(/<td>/g, '')
.replace(/<\/td>/g, ',')
.replace(/\t/g, '')
.replace(/\n/g, '');
var data= data1.concat(data2);
var mylink = document.createElement('a');
mylink.download = "Gameweek.csv";
mylink.href = "data:application/csv," + escape(data);
mylink.click();
}</script>
<button onclick="doCSV()">Export HTML Table To CSV File</button>
<?PHP
}
?>
I expect each button to convert the information of the particular gameweek which the loop is currently on (for e.g. i i=1 then gameweek 1 team should be exported, if i=2 then gameweek2 team should be exported)
You'll need to add unique ids to every table like:
<table id ="gameweek_history<? echo $i; ?>">
Then you'll need to have your javascript refer to the id. Your existing code creates a new script for every table, so to keep doing that, each function will need a unique name that has refers to that table.
<script>function doCSV<? echo $i; ?>() {
var table1 = document.getElementById("gameweek_history<? echo $i; ?>").innerHTML;
It would probably be better to just have one function declared outside the loop with a variable passed into it.
<script>function doCSV(table_number) {
var table1 = document.getElementById("gameweek_history" + table_number).innerHTML;
Then add the table number to each event handler.
<button onclick="doCSV(<? echo $i; ?>)">Export HTML Table To CSV File</button>

disable checkbox/textbox dynamically after insert

I've been trying to disable checkbox(which I fetched using while loop in php) after form submit. Any help would be appreciated.
<td><?php echo $num['roomno']; ?></td>
<td><?php echo $num['regno']; ?></td>
<td><?php echo $num['pname']; ?></td>
<td><input type="checkbox" name="diet" id="diet" value="<? echo $num['regno'];?>"
<?php if(($regno!="") && ($thiss == $regno)) { echo 'disabled'; } ?> />
</td>
This is the php section to get the value from checked checkbox. I dont know if I'm doing the right way.
<?
$regno = isset($_REQUEST['diet'])?$_REQUEST['diet']:"";
if(isset($regno)) {
$selec = "select regno from roomlist where regno='".$regno."'";
$sql = mysqli_query($con,$selec);
if(!$sql) {
echo "Error : ".mysqli_error($sql);
exit();
}
$num = mysqli_fetch_array($sql,MYSQLI_ASSOC);
$thiss = $num['regno'];
}

Magento: Hide dropdown attribute if no value

I use this code to hide attributes when they do not have any data:
<?php foreach ($_additional[ 'items'] as $_data): ?>
<?php $_attribute=$ _product->getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<tr>
<th class="label">
<?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data">
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
This works great, but not for attributes type dropdown.
How can I also hide dropdown attributes, that do not have any value?
If an attribute with the type dropdown isn't set, it usually displays as N/A on the front-end of your website, so you can simply add && $_data['value'] != 'N/A' to the if-statement
So the code would look something like this
<?php foreach ($_additional[ 'items'] as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']); if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '' && $_data['value'] != 'N/A')) { ?>
<tr>
<th class="label">
<?php echo $this->htmlEscape($this->__($_data['label'])) ?>
</th>
<td class="data">
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>

Disable button when db is populated enable when null on javascript

I have my database table named material
material:
mat_id mat_name status
1 bolts
2 steel approved
How can I make my button or maybe href link disable when status = 'approved'
on my html table?
like:
option mat_id material name
show button 1 bolts
no button 2 steel
my code for html/php:
<?php
$resource=mysql_query("Select * from material",$con);
while($result2=mysql_fetch_array($resource))
{
?>
<tr>
<th>option</th>
<th>mat_id</th>
<th>material name</th>
</tr>
<tr>
<td align="center">
<?php
$id = $rows['reqraw_id'];
if($rows['status']=="")
{
echo '<a href="addstockout.php?id=$id" > Update </a>';
} ?>
</td>
<td><?php echo $result2['mat_id']; ?></td>
<td><?php echo $result2['mat_name']; ?></td>
</tr>
<?php };?>
There is a problem on this line
echo '<a href="addstockout.php?id=$id" > Update </a>';
It doesn't get the id array from the db.
...
<?php if($result2['status'] == 'approved') { ?>
<td><button disabled="true">Button</button></td>
<?php
} else{ ?>
<td><button>Button</button></td>
<?php } ?>
...
if($result2['status']=="" OR $result2['status']===NULL)
{
echo '<button type="button">Click Me!</button>';
}else{
echo '<button type="button" disabled>Click Me!</button>';
}
<td><?php echo ($result2['status']===NULL)?"button here":"disabled"; ?></td>
I think this should do it:
Instead of
<td>button</td>
put:
<?php
echo '<td><button ';
if(!($result2['status']==="approved"))
{
echo 'disabled="true"';
}
echo '>Click</button></td>';
?>

Categories

Resources