Currently I have this,
This my code
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
<th scope="col">Thursday</th>
<th scope="col">Friday</th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['class_id'])) {
$class_id = $_GET['class_id'];
} else {
$class_id = '1'; //default class id => Yoga
}
$mQuery = "SELECT * FROM timetable where class_id = '".$class_id."'"; //code to select from database
$result = $pdo-> query($mQuery);
while($row = $result->fetch(PDO::FETCH_ASSOC)):
$timeslot = $row['timeslot'];
// pass delimiter and string to explode function
$timeslot_ar = explode(',', $timeslot);
?>
//code to display result in table
<tr class="table-active">
<?php if(date('l', strtotime($row['date'])) == 'Monday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Tuesday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Wednesday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Thursday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Friday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Saturday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Sunday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
</tr>
<?php endwhile; ?>
</tbody>
</table>
I've been trying to get my result to display in a single row for all the days and then make a new row when a day of the same name already exists. I've been trying to figure out the login behind this and after countless of stack overflow answers and youtube tutorials I got to this point. I still can't manage to get them in a single row, if anyone would be kind enough to explain the method behind this, I would really appreciate it.
Put the <tr></tr> outside of the while() loop:
<tr class="table-active">
<?php
// other code
while($row = $result->fetch(PDO::FETCH_ASSOC)):
//code for table cells
?>
</tr>
I'm going to assume the timetable is output in date order in the query. You should probably include an ORDER BY clause to make sure of that. I also assume that the results may cover more than one week. Lastly I'm going to assume that every single day is represented by a row in the results. If it's not the case, then please show a sample of results which demonstrates the reality (as you will need some extra logic to deal with missing days).
Using those assumptions, it looks like you only need to start a new row when the current day is Monday, and end it when it's Sunday. So...move the row creation inside the if for Mondays, and the ending inside the if for Sundays, e.g.:
<?php if(date('l', strtotime($row['date'])) == 'Monday') { ?>
<tr class="table-active">
...
<?php if(date('l', strtotime($row['date'])) == 'Sunday') { ?>
</tr>
...
Also, it looks like you actually output the exact same <td> for each day, and you don't want to output blank tds if it's not the mentioned day (because that will add extra cells you don't need in the row), so the whole loop could be simplified as follows:
while($row = $result->fetch(PDO::FETCH_ASSOC)):
$timeslot = $row['timeslot'];
// pass delimiter and string to explode function
$timeslot_ar = explode(',', $timeslot);
?>
//code to display result in table
<?php if(date('l', strtotime($row['date'])) == 'Monday') { ?>
<tr class="table-active">
<?php } ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php if(date('l', strtotime($row['date'])) == 'Sunday') { ?>
</tr>
<?php } ?>
<?php endwhile; ?>
You can also replace the implode/explode with a simple string replacement function...I'll leave that as an exercise for the reader.
Managed to fix my issue by scraping off the table and using custom div containers.
Code snippet:
<div class="card" style="width: 9rem;display:inline-block">
<ul class="list-group list-group-flush">
<center><li class="list-group-item"><b>Monday</b></li></center>
<?php
$sql = "SELECT * FROM timetable where class_id='$class_id' and day='Monday'";
$stm = $pdo->query($sql);
$slots = $stm->fetchAll();
foreach ($slots as $row) {
$timeslot = $row['timeslot'];
// pass delimiter and string to explode function
$timeslot_ar = explode(',', $timeslot);
echo '<center>'.date('d M', strtotime($row['date'])).'</center>'.'<li class="list-group-item">'.implode("<br/>", $timeslot_ar). '</li>';
}
echo "<br/>";
?>
</ul>
</div>
Final product
Related
<table class="table row-border order-column">
<thead>
<tr>
<th></th>
<?php
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($i=1;$i<=$days;$i++)
{
?>
<th><?php echo $i; ?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($student as $key => $value)
{
?>
<tr>
<td class="cap"><?php echo $value->name; ?></td>
<?php
for($i=1;$i<=$days;$i++)
{
$result = $this->db->select('*')->from('attendance')->where('studentID',$value->studentID)->where('day(date)', date($i))->where('month(date)', date($month))->where('Year(date)', date($year))->get()->row();
if(empty($result))
{
$date = date('Y-m-d');
$current_date = date('d',strtotime($date));
if($i < $current_date)
{
?>
<td class="absent">A</td>
<?php
}
else
{
?>
<td></td>
<?php
}
}
else
{
$dateday = date('d',strtotime($result->date));
if($i == $dateday)
{
?>
<td class="present">P</td>
<?php
}
}
}
?>
</tr>
<?php
}
?>
</tbody>
In the above code I want to show absent while compare $current_date and $days but now what happen here only current month date show absent and previous month date show blank. Now, What am I going to do I don't want to show absent in future date. So, How can I do this? Please help me.
Thank You
Use Unix timestamp for your calculation and after that change Unix to gmt or utc time using ready functions to show.
I have a table with weeks of a year and some data adjacent to each week, and i can't seem to merge the values correctly. Week rows with no data should stay empty
This is where i list the weeks
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?></td>
</tr>
<?php }
And this is where i try to merge
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php } ?>
it should look like this
but it looks like this
Instead of running two loops run one loop like this.
foreach ($getLeadCountDMm as $index => $leadCount) { ?>
<tr>
<td><?php echo $leadCount->theweek ?>
/ <?php echo $leadCount->theyear ?>
</td>
<td style="text-align: center">
<?php if ($getFtdCountDMm[$index]->theweek == $leadCount->theweek && !empty($getFtdCountDMm[$index]->ftdcount)) {
echo $getFtdCountDMm[$index]->ftdcount;
} else {
echo '0';
} ?>
</td>
</tr>
<?php }
I have a small invoice web tool.
So I would like to find out the total amount of all positions of items and if it reached a maximum level it should print the following positions on the second page of the invoice.
I use the sample code to "design" my invoices:
http://codepen.io/rafaelcastrocouto/pen/LFAes/
My idea is: first count all characters of the foreach loop.
But I don't know how I should continue?
Please give me some hints. Thanks
EDIT: here is my foreach loop code:
<?php foreach( $items as $it): ?>
<tr class="even">
<td style="width:8%;"><?php echo $pos; ?></td>
<td style="width:47%;"><?php
$string = $it->invoice_item_text;
if (strlen($string) > 350) $string = substr($string,0,350).'...';
echo $string;
?></td>
<td style="width:15%;">
<?php if ($it->invoice_item_type == 3) {$date = date('M Y', strtotime($it->invoice_item_date)); } else { $date = date('d.m.Y', strtotime($it->invoice_item_date)); } ?>
<?php echo $date; ?>
</td>
<td style="width:20%;"><?php if ($it->invoice_item_time != '0') { if ($it->invoice_item_type == 1 or $it->invoice_item_type == 2) echo gmdate('H:i', $it->invoice_item_time);}; ?></td>
<td style="width:10%;"><?php echo number_format($it->invoice_item_sum, 2, ',', ' '); ?> €</td>
</tr>
<?php $pos++; ?
<?php endforeach; ?>
Use php Array as session variable
$_SESSION['items'] = $items;
if you store value in session you can use this value any page
I want to color the column according to department and month in a table.
My problem is if one department having more than one month am getting only one column colored. for eg: In IT am having april and june. But i am getting color only in june column. Can any one help me plz?
code is given below
$q_fn = mysql_query("SELECT deptname FROM functiontb ") ;
$storeArray = Array();
while ($row = mysql_fetch_array($q_fn, MYSQL_ASSOC)) {
$storeArray[] = $row['deptname'];
}
foreach($storeArray as $sa)
{ ?>
<tr >
<td><?php echo $sa ?></td>
<?php
$q_audit=mysql_query("SELECT * FROM scheduletb where department = '$sa' ") );
while($r= mysql_fetch_assoc($q_audit)){
$month=date("F",strtotime($r['tdate']));?>
<td <?php
if($month == 'January'){
?> bgcolor="#1E90FF">
<?php } ?>
</td>
<td <?php
if($month == 'February'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'March'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'April'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'May'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'June'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'July'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'August'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'September'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'October'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'November'){
?> bgcolor="#1E90FF">
<?php }
?></td>
<td <?php
if($month == 'December'){
?> bgcolor="#1E90FF">
<?php }
?> </td>
</tr>
<?php }
} ?>
Yes, it's a great advice from Jay, you should consider that, you can use Mysqli too.
For set the color you could try:
Set the color using var:
if($month == 'January'){
var $color = '#ff9900';
}else if(...){}
and then echo the info:
echo '<td bgcolor="'.$color.'"></td>';
I think when you break html tags it gets a little confusing.
Hope it helps.
(you can try to use css style tag or class to get the colors too).
Aside from that you are using the deprecated mysql_ functions, I see two problems related to the issue you ask about.
First, the wall of if-statements where you open and close PHP tags has resulted in you leaving off closing > from your HTML and getting invalid HTML syntax. It would be better to do something like:
<td bgcolor='<?php echo getColorByMonth($month); ?>'>put something here</td>
And write a function that just returns the color you want based on the month.
Second, if you don't include something inside your TD, the browser may not apply the style. In other words <td bgcolor='blue'></td> will probably not even show up but <td bgcolor='blue'>yo</td> will. If you want a blank cell to actually show up reliably then put a non-breaking space ( ) in it <td bgcolor='blue'> </td>
Third, you could also CSS, define a css class for each month, then just print the month:
<style>
.January { background-color: blue; }
.February { background-color: red; }
</style>
...
...
<td bgcolor='<?php echo $month; ?>'> </td>
I have a php table that displays the days of the month in a table:
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
I have a second table that calls a separate query for each day of the month:
<tr>
<td> </td>
<td> </td>
<td><?php do { ?>
<?php echo $row_apr1['appt_time']; ?> <em><?php echo $row_apr1['nickname']; ?></em><br>
<?php } while ($row_apr1 = mysql_fetch_assoc($apr1)); ?></td>
<td><?php do { ?>
<?php echo $row_apr2['appt_time']; ?> <em><?php echo $row_apr2['nickname']; ?></em><br>
<?php } while ($row_apr2 = mysql_fetch_assoc($apr2)); ?></td>
<td><?php do { ?>
<?php echo $row_apr3['appt_time']; ?> <em><?php echo $row_apr3['nickname']; ?></em><br>
<?php } while ($row_apr3 = mysql_fetch_assoc($apr3)); ?></td>
<td><?php do { ?>
<?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?></td>
<td><?php do { ?>
<?php echo $row_apr5['appt_time']; ?> <em><?php echo $row_apr5['nickname']; ?></em><br>
<?php } while ($row_apr5 = mysql_fetch_assoc($apr5)); ?></td>
</tr>
Is there a way I can merge the code together into one table, like using the $day_num value to call the query code?
Example: April 4th - $day_num would be = 4 and display a 4 in the table. But then can't I use php concatenation or something to call the code:
<br><?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?>
Sorry if this has been done before a million times. I've been searching and can't seem to find what I'm looking for.
Thanks!
You can use the variable variables or $$ option. http://www.php.net/manual/en/language.variables.variable.php
<td>
<?php
$variable_name = 'apr' . $day_num;
do { ?>
<?php echo $row['appt_time']; ?>
<em><?php echo $row['nickname']; ?></em><br>
<?php } while ($row = mysql_fetch_assoc($$variable_name)); ?>
</td>
Not sure this is the best option for you but based off the code you have supplied it is a possible solution.
FYI you shouldn't use mysql_ functions as they are deprecated.