I'm working on a website that manages food. Keeping track of expiration dates and the such.
When a users dashboard loads, it shows a list of the food items they've currently added along with info about them, Brand, Location (Fridge,Pantry,Freezer), and expiration date.
I have the items in a html table, drawing their values from php echos of the rows of a food table I created.
What I want to do is change the text of the expiration date to red when there is about a 3 day difference between the current date and the set expiration date.
Here's my code for putting the values in the table.
<?php
while($rows = mysql_fetch_array($sql2))
{
?>
<tr>
<td><b><?php echo $rows['Name'] ?></td>
<td><b><?php echo $rows['Brand'] ?></td>
<td><b><?php echo $rows['Type'] ?></td>
<td><b><?php echo $rows['Container'] ?></td>
<td><b><?php echo $rows['ExpirationDate'] ?></td>
</tr>
<?php
}
?>
$sql2 holds a query of all the foods owned by the user.
So how would I create a conditional that would make this work out?
$date1 = new DateTime("now");
$date2 = $rows['ExpirationDate'];
$difference = $date1->diff($date2);
$days = $difference->d;
$class = '';
if ($days <= 3)
$class = 'class="expired"';
Then add the $class variable to each table cell. If the difference between dates is 3 days or less, it will add class="expired" otherwise it will add nothing. Then just style the .expired class to have red text.
You will most likely have to format your $date2 variable, but how to do that depends on the format it's currently in...
You can assign a different class to the expiration date when it's getting close.
Something like:
<td><b><?php
$exptime = strtotime($rows['ExpirationDate']);
if ($exptime - time() < 3*24*60*60){
$class = 'expBad';
}else{
$class = 'expOK';
}
echo "<span class=\"$class\">".$rows['ExpirationDate']."</span>" ?></td>
Then in your .css you can define the class expBad to be red.
span.expBad {
color: red;
}
Related
Im a begginer in the whole SO and programming enviroment, and here comes my first question(which i tried finding in here but with no luck so far..)
I have an html table that prints specific patients of the doctor that is active (in a simple web-application that i created). The doctor gets filtered using $_SESSIONS global var in php.
My problem is that i want to implement a few actions in the same HTML table that displays the patients, like view history (which is stored in a local DB table, from an HTML from using POST method) and create a new form for the same person.
I saw that providing the table with row.ids could be a solution, but my table isn't static, i woulda like for the user to have the option to add/delete patients..
Following is a sample of my code that displays the HTML table "Existing Patients" :
<table>
<tr>
<th>Patient Id</th><th>Patient Name</th><th>Phone Number</th><th>Email</th><th>History</th>
<th>Add a Follow Up Visit</th><th>Remove Patient</th>
</tr>
<?php $sql = "SELECT * FROM patients WHERE Doctor_ID = $usersid";
$result = $pdo->query($sql);
if($result->rowCount() > 0){
while($row = $result->fetch()){?>
<tr>
<td><?php echo $row['Patient_id']; ?></td>
<td><?php echo $row['Patient_name'] ; ?></td>
<td><?php echo $row['Phonenum'] ; ?></td>
<td><?php echo $row['Email']; ?></td>
<td><?php echo "<a href='/patienthistory.php'</a>" . 'Previous Visits'; ?></td> //problem here
<td><?php echo "<a href='/patientform.html'</a>" . 'Add Follow Up' ; }?></td> //and here
</tr>
</table>
I want the last 2 lines to connect immediately to the specific database stored Patient_id and retrieve the existing information stored there from the patients previous visits.
I hope i gave enough of a description, but if there is any more info neccesary, please let me know.
Thank you in advance!
You can specify the patient ID as a query parameter in the URL that you're linking to. Also the HTML you're generating for your links is invalid currently. And the second link is going to need to go to a .php script, not a .html file, if you want it to execute code and fetch data.
Try this example:
<td><?php echo "<a href='/patienthistory.php?id='".$row['Patient_id']."'>Previous Visits</a>"; ?></td>
<td><?php echo "<a href='/patientform.php?id='".$row['Patient_id']."'>Add Follow up</a>"; ?></td>
Then in each of the PHP scripts, use $_GET["id"] to retrieve that ID of the patient and use that in a query
e.g.
$patientID = $_GET["id"];
I have created a PHP ledger book system in the admin area to manage clients accounts amount.
I have created the below table in phpmyadmin:
I have tried using the below code for getting the result:
$list_balance = mysqli_query($con_db,"SELECT * FROM tbl_balancesheet where client_id='".$_GET['cid']."' ORDER by id desc" );
$total = 0;
while($row = mysqli_fetch_array($list_balance)){
if($row['action_type']=='dr'){
$debit = $row['amount'];
$total += $row['amount'];
}else{
$credit = $row['amount'];
$total -= $row['amount'];
}
<tr>
<td><?php echo $row['created_date']; ?></td>
<td><?php echo $row['particular']; ?></td>
<td>
<?php
if($row['action_type']=='dr'){
echo $debit;
}else{
echo ' ';
}
?>
</td>
<td>
<?php
if($row['action_type']=='cr'){
echo $credit;
}else{
echo ' ';
}
?>
</td>
<td><?php echo $total; ?></td>
</tr>
I am getting the below result, but I don't want to show a minus (-) icon in front of balance:
Also, I want to display the last entry in first but the result is not displaying correctly.
The expected result should be as shown below
Please help me how I can get the above result. Also, how I can display the dr and cr.
Ex: If the admin asks money from customers 400 only. But some times customers had paid an advance amount to admin then the customer borrow money from admin. In that case, some times admin amount dr form customers and some times customers dr from admin.
Thanks
I don't want to show a minus (-) icon in front of balance:
You should use abs function:
<td><?php echo abs($total); ?></td>
Also, I want to display the last entry in first but the result is not displaying correctly.
In the code, in the sql query you have ORDER by id desc which means descending order by id (last inserted entry goes first)
If you want to change the order to ascending then remove desc from the query.
So, I have an sql table that doesn't have a unique column. I have to display it in a table by it's ID. But, like I said, the ID is not unique
OBSĀ¹: Notice that it comes from a select statement
OBSĀ²: the user id can appear in the select from 0 up to 4 times (according to id_questionario)
So, the thing is, how am I going to make a table that will show only one time user_id but will display by it's side all the melhor_tentativa according to id_questionario (notice that id_questionario is not to be displayed, only so I can display the melhor_tentativa in the correct order)
What I understand is that I have lines that should be columns. I've been trying for 2 days to figure this out and turns out I can't. Thought that it might be miss in how I made my sql
This is how the table looks
This is how the sql table looks
And bellow is the code I made for it
<?php
$count = 0;
$nota1 = 0;
$nota2 = 0;
$nota3 = 0;
$nota4 = 0;
?>
<?php
foreach($notas as $nota):
$total = 0;
?>
<tr>
<td><?php echo $nota['user_id']?></td>
<td>NULL</td>
<?php
if($nota['id_questionario']==1){
$nota1 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota1 ?></td>
<?php
if($nota['id_questionario']==2){
$nota2 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota2 ?></td>
<?php
if($nota['id_questionario']==3){
$nota3 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota3 ?></td>
<?php
if($nota['id_questionario']==4){
$nota4 = $nota['melhor_tentativa']*20;
}
?>
<td><?php echo $nota4 ?></td>
<td><?php $total = ($nota1 + $nota2 + $nota3 + $nota4)/4; echo $total; ?></td>
</tr>
<?php $count++; ?>
<?php
endforeach;
echo $count;
?>
</table>
P.S: I know that total is completly wrong ;)
After a bunch of work I figured this out. So what I did was the following. I had that table which would show 4 results for a one ID. I made a view out of this table
Then I did a subquery from that view, so I would be able to show all results in one line
Hope it helps anybody in the future :)
I'm trying to display the date from the database using the following code.
<?php
$query_course = mysqli_query($databaseConnection,"SELECT * FROM grading WHERE courseid=".$courseid);
$count=0;
while($row = mysqli_fetch_array($query_course))
{
$gradename[$count]= $row['gradingname'];
$percentage[$count]= $row['percentage'];
$date[$count]= $row['date'];
?>
<tr>
<td><?php echo $gradename[$count]; ?></td>
<td><?php echo $percentage[$count]; ?></td>
<td><?php echo $date[$count]; ?></td>
</tr>
<?php $count++;
} ?>
But it displays only the first letter/digit for the date. Database looks fine. What might be the problem?
The only thing that would explain this is if $gradename etc. were initialised as strings. E.g.:
$foo = '';
$foo[0] = 'bar';
echo $foo[0]; // b
Make sure you initialise your variables as arrays before the loop:
$gradename = array();
...
(I'd question the usefulness of those extra arrays to begin with, but that's outside the scope of this question.)
it displays the first digit because you use the count as index. Since the count is 0 it display just the first digit from date. Remove the $count and echo the $row['date'].
I am using the code below to generate a simple HTML table that displays the next 90 calendar days. Each day is a row in this simple table.
$now = time();
echo "<table>";
for ($i=0;$i<90;$i++)
{
$thisDate = date("d/m/Y",$now + ($i*86400));
echo "<tr><td>".$thisDate."</td></tr>\n";
}
echo "</table>";
Also, I have a MySQL table with the following fields:
event varchar(1000)
datescheduled date
How can I make a second column in the aforementioned HTML table, containing "event" from the MySQL table, matched by date?
This can be tackled in numerous ways. Consider this example:
PHP
<?php
$con = mysqli_connect("localhost","dbuser","dbpass","database");
$query = mysqli_query($con, "SELECT * FROM event");
// First build the array of events, put the dates in keys, then the values as events
$events = array();
while($result = mysqli_fetch_assoc($query)) {
$events[$result['datescheduled']] = $result['event'];
}
?>
// Structure should be something like this:
Array
(
[2014-05-02] => Day of lorem
[2014-06-02] => Day of ipsum
[2014-07-02] => Day of days
)
HTML
<!-- the compare selected values on the current loop, check its keys -->
<?php $now = time(); ?>
<table border="1" cellpadding="10">
<?php for($i=0;$i<90;$i++): ?>
<?php $thisDate = date("Y-m-d", $now + ($i*86400)); ?>
<tr>
<td><?php echo $thisDate; ?></td>
<td><?php echo array_key_exists($thisDate, $events) ? $events[$thisDate] : ''; ?></td>
</tr>
<?php endfor; ?>
</table>
$now = time();
echo "<table>";
for ($i=0;$i<90;$i++)
{
$thisDate = date("d/m/Y",$now + ($i*86400));
echo "<tr><td>".$thisDate."</td>";
$result_set = mysql_query("SELECT event FROM eventTable WHERE datescheduled = STR_TO_DATE('{$thisDate}','%d/%m/%Y')'",$connection);
$result = mysql_fetch_assoc($result_set);
echo "<td>{$result['event']}</td></tr>\n";
}
echo "</table>";
Its worth noting that you will need to use a string to date function in mysql depending on how the date is stored.
Edit: in case you need further hand holding, here is the STR_TO_DATE function done for you.
STR_TO_DATE('{$thisDate}','%d/%m/%Y')
I have edited my code above to reflect this as to not strain your brain.
Even tossed in some screenshots of the table and the output, just because you were kind of an ass in your comment. With 5 years of experience i would have thought you would know how to echo out a simple table like this, or at the very least, have a little common courtesy when someone tries to help you.