If-statement while loop stopping after first row - php

I have the following PHP script that takes the selected $empfullname from the user and prints 'timecard_html' based on the selection. I am trying to make it capable that if they select 'All' then the script prints out all of the employees in the list each in a separate timecard_html. Here is the php script:
if ($empfullname == 'All') {
$query = "select empfullname from ".$db_prefix."employees";
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {
$empfullname = ("".$row['empfullname']."");
print timecard_html($empfullname, $local_timestamp_in_week);
unset($empfullname);
}
} else {
print timecard_html($empfullname, $local_timestamp_in_week);
}
Also, here is timecard_html in case you need to see it:
function timecard_html($empfullname, $local_timestamp_in_week) {
// Return html of employee's timecard.
global $show_display_name, $one_week;
// SQL search parameters for one work week.
$begin_local_timestamp = work_week_begin($local_timestamp_in_week);
$end_local_timestamp = $begin_local_timestamp + $one_week;
// Define helper functions for printing timecard header, footer, and for printing every row.
function print_header($tc) {
// Print timecard html header.
global $overtime_week_limit, $timecard_display_running_total;
$overtime_col = $overtime_week_limit > 0 ? "\n <th align=\"center\" class=\"ovt\" title=\"Overtime hours\">OT</th>" : '';
$total_col = $timecard_display_running_total == "yes" ? "\n <th align=\"center\" class=\"total\" title=\"Running total of regular work hours and overtime to date.\">Total</th>" : '';
print <<<End_Of_HTML
<table class="misc_items timecard_list" border="0" cellpadding="2" cellspacing="0" style="margin:0 auto;">
<thead>
<tr>
<th align="left">In/Out</th>
<th align="center">Time</th>
<th align="center">Date</th>
<th align="center" class="hrs" title="Regular work hours.">Hrs</th>$overtime_col$total_col
<th align="left" class="notes">Notes</th>
</tr>
</thead>
<tbody>
End_Of_HTML;
}
function print_row($tc) {
// Configuration variables.
global $timefmt, $datefmt;
global $overtime_week_limit, $timecard_list_punch_outs, $timecard_display_hours_minutes;
global $timecard_hours_include_overtime, $timecard_display_running_total;
static $print_count = 0;
if (($tc->in_or_out == 1) || $timecard_list_punch_outs == 'yes') {
$h_color = htmlentities($tc->row['color']);
$h_inout = htmlentities($tc->row['inout']);
$h_time = date($timefmt, $tc->start_time);
$h_date = date($datefmt, $tc->start_time);
if ($timecard_display_hours_minutes == "yes") {
$h_hours = hrs_min((($timecard_hours_include_overtime == "yes") ? ($tc->hours + $tc->overtime) : $tc->hours));
$h_overtime = hrs_min($tc->overtime);
$h_total = hrs_min(($tc->week_hours + $tc->overtime_hours));
}
else {
$h_hours = sprintf("%01.02f",(($timecard_hours_include_overtime == "yes") ? ($tc->hours + $tc->overtime) : $tc->hours));
$h_overtime = sprintf("%01.02f",$tc->overtime);
$h_total = sprintf("%01.02f",($tc->week_hours + $tc->overtime_hours));
}
$h_notes = htmlentities($tc->row['notes']);
if ($tc->in_or_out != 1) {
// Don't display hours on "out" records.
$h_hours = $h_overtime = $h_total = '';
}
$row_class = (++$print_count % 2) ? 'odd' : 'even';
$overtime_col = $overtime_week_limit > 0 ? "\n <td align=\"right\" class=\"ovt\">$h_overtime</td>" : '';
$total_col = $timecard_display_running_total == "yes" ? "\n <td align=\"right\" class=\"total\">$h_total</td>" : '';
print <<<End_Of_HTML
<tr class="display_row $row_class">
<td align="left" class="job_code" style="color:$h_color">$h_inout</td>
<td align="right">$h_time</td>
<td align="right">$h_date</td>
<td align="right" class="hrs">$h_hours</td>$overtime_col$total_col
<td align="left" class="notes">$h_notes</td>
</tr>
End_Of_HTML;
}
}
function print_footer($tc) {
global $timecard_display_running_total, $timecard_hours_include_overtime;
global $timecard_display_hours_minutes, $overtime_week_limit;
// Set flag to print paragraph of totals if they're not already obvious.
$print_totals = ($timecard_display_running_total == "yes" || $timecard_hours_include_overtime != "yes") ? true : false;
$h_total_hours = sprintf("%01.02f",($tc->week_hours+$tc->overtime_hours));
$h_totals = ($print_totals) ? "\n<p>Total for week: ".hrs_min($tc->week_hours + $tc->overtime_hours)." ($h_total_hours hours)</p>" : '';
$h_ovt_total_hours = sprintf("%01.02f",$tc->overtime_hours);
$h_overtime_totals = ($print_totals && $tc->overtime_hours > 0) ? "\n<p>Total overtime: ".hrs_min($tc->overtime_hours)." ($h_ovt_total_hours hours)</p>" : '';
$h_day_total_hours = sprintf("%01.02f",$tc->today_hours);
$h_today_hours = ($tc->today_hours !== null) ? "<p>Total today: ".hrs_min($tc->today_hours)." ($h_day_total_hours hours)</p>" : '';
if ($timecard_display_running_total != "yes") {
// Print row of totals
$total_hours = $timecard_hours_include_overtime == "yes" ? ($tc->week_hours+$tc->overtime_hours) : $tc->week_hours;
$h_hours = $timecard_display_hours_minutes == "yes" ? hrs_min($total_hours) : $h_total_hours;
$overtime_col = $overtime_week_limit > 0 ? "\n <td align=\"right\" class=\"ovt\">".($timecard_display_hours_minutes == "yes" ? hrs_min($tc->overtime_hours) : $h_ovt_total_hours)."</td>" : '';
$total_col = $timecard_display_running_total == "yes" ? "\n <td align=\"right\" class=\"total\">".($timecard_display_hours_minutes == "yes" ? hrs_min($tc->week_hours+$tc->overtime_hours) : $h_total_hours)."</td>" : '';
print <<<End_Of_HTML
<tr class="total_row">
<td align="left"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right" class="hrs">$h_hours</td>$overtime_col$total_col
<td align="left" class="notes"></td>
</tr>
End_Of_HTML;
}
print <<<End_Of_HTML
</tbody>
</table>
End_Of_HTML;
if ($timecard_display_running_total == "yes" || $timecard_hours_include_overtime != "yes" || $h_today_hours) {
// Add totals text if totals are not already displayed or if summing the hours column is confusing.
print <<<End_Of_HTML
<div class="totals">
$h_today_hours$h_totals$h_overtime_totals
</div>
End_Of_HTML;
}
}
// End of helper function definitions.
// Print timecard page header.
$h_name_header = htmlentities( ($show_display_name == 'yes' ? get_employee_name($empfullname) : $empfullname) );
$begin_date = date('l F j, Y',$begin_local_timestamp);
print <<<End_Of_HTML
<div id="punchclock" class="timecard">
<h2>Timecard</h2>
<h3>$h_name_header</h3>
<h4>Week beginning $begin_date</h4>
End_Of_HTML;
// Print timecard.
$tc = new Timecard($empfullname,$begin_local_timestamp, $end_local_timestamp);
list($row_count, $total_hours, $overtime_hours, $today_hours) = $tc->walk(print_header, print_row, print_footer);
if ($row_count <= 0) print error_msg("No records were found.");
// Print timecard page footer.
print <<<End_Of_HTML
</div> <!-- timecard -->
End_Of_HTML;
}
?>
As of right now if 'All' is selected it prints the results for the first employee in the table, but not any of the other ones. Also, if I just print ("".$row['empfullname']."") it prints out all the employees. Do I maybe need to use a for each loop instead of a while loop?
Thanks in advance.

Your problem is that your are defining functions in your function. That will work the first time when you call your function, but the second time it will cause a fatal error as the function is already defined.
See a simple example here.
Moving the inner function declarations out of the main function should solve this specific problem.
You should also always enable error handling and displaying while developing. In this case you would have caught the problem inmediately.
Just put this at the top of the main script:
ini_set('display_errors',1);
error_reporting(E_ALL | E_STRICT);

Related

How to convert json value database to html

I have notification table on my database where the value is using like json.
Here's my table
id | touserid | data
1 2 a:1:{i:0;s:10:"INV-000001";}
2 2 a:1:{i:0;s:10:"INV-000003";}
3 2 a:1:{i:0;s:15:"The Mej Hotel";}
4 1 a:5:{i:0;s:28:"Total Goalsi:1;s:7:"6250000";}
5 1 a:1:{i:0;s:10:"INV-000007";}
I want to use that value in html table, but I don't know how to convert the value to html table in codeigniter
Here's my view code
<table class="table table-dark">
<tbody>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Data</th>
<
</tr>
</thead>
<tbody>
<?php foreach($notifications as $notif){ ?>
<tr>
<td><?php echo $notif['id'] ?></td>
<td><?php echo $notif['data'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Here's controller code
$this->db->limit($this->misc_model->get_notifications_limit(), $offset);
$this->db->where('touserid', get_staff_user_id());
$this->db->order_by('date', 'desc');
$data['notifications'] = $this->db->get(db_prefix() . 'notifications')->result_array();
$this->load->view('admin/sales/sales', $data);
But I don't see the data value get into html table like I want, in table it's show the error message " not_goal_message_failedArray"
I'm trying to encode the json, but I still don't know how to pass the json encode in controller to view in codeigniter
Here's the json encode
$page = $this->input->post('page');
$offset = ($page * $this->misc_model->get_notifications_limit());
$this->db->limit($this->misc_model->get_notifications_limit(), $offset);
$this->db->where('touserid', get_staff_user_id());
$this->db->order_by('date', 'desc');
$notifications = $this->db->get(db_prefix() . 'notifications')->result_array();
$i = 0;
foreach ($notifications as $notification) {
if (($notification['fromcompany'] == null && $notification['fromuserid'] != 0) || ($notification['fromcompany'] == null && $notification['fromclientid'] != 0)) {
if ($notification['fromuserid'] != 0) {
$notifications[$i]['profile_image'] = '<a href="' . admin_url('staff/profile/' . $notification['fromuserid']) . '">' . staff_profile_image($notification['fromuserid'], [
'staff-profile-image-small',
'img-circle',
'pull-left',
]) . '</a>';
} else {
$notifications[$i]['profile_image'] = '<a href="' . admin_url('clients/client/' . $notification['fromclientid']) . '">
<img class="client-profile-image-small img-circle pull-left" src="' . contact_profile_image_url($notification['fromclientid']) . '"></a>';
}
} else {
$notifications[$i]['profile_image'] = '';
$notifications[$i]['full_name'] = '';
}
$data = '';
if (!empty($notification['data'])) {
$data = unserialize($notification['data']);
$x = 0;
foreach ($data as $dt) {
if (strpos($dt, '<lang>') !== false) {
$lang = get_string_between($dt, '<lang>', '</lang>');
$temp = _l($lang);
if (strpos($temp, 'project_status_') !== false) {
$status = get_project_status_by_id(strafter($temp, 'project_status_'));
$temp = $status['name'];
}
$dt[$x] = $temp;
}
$x++;
}
}
$notifications[$i]['description'] = _l($notification['description'], $dt);
$notifications[$i]['date'] = time_ago($notification['date']);
$notifications[$i]['full_date'] = $notification['date'];
$i++;
}
echo json_encode($notifications);
Do you know where's my error when tried convert the json value in the table to html table code ?
Thank you
Your data in your table is looking like a serialised array
You will not get the data via echo, should use
$this->load->view("notification_view", $notifications);
instead of
echo json_encode($notifications);

how to display data from an array in a table am getting errors

hi all i am trying to display array of data in a table .but the main task is i need to display all users that are under one supervisor, they may/may not have attempted the test .i am using if else condition in controller but am getting error like
Message: Undefined index: status and Message: Undefined index:
submittimestamp
below is my code
controller:
if ($supervisor) {
$users_query="SELECT u.* ,v.value FROM usr_data u, udf_text v WHERE u.usr_id != 6 AND u.usr_id = v.usr_id " . $supervisor_condition . " GROUP BY u.usr_id";
// echo $users_query;
$user_records = $this->base_model->executeSelectQuery($users_query);
$final_dataa = [];
foreach ($user_records as $user) {
$user=(object)$user;
$user_test=" SELECT u.*,o.title,ta.*,tpr.workingtime, tpr.pass, tpr.tstamp, tpr.points, tpr.maxpoints, tm.minimum_level, tcr.mark_official,(tcr.reached_points/tcr.max_points)*100 as result,v.value,ta.submittimestamp,tcr.mark_official FROM usr_data u, object_data o,tst_tests tt,tst_active ta,tst_pass_result tpr, tst_result_cache tcr,udf_text v, tst_mark tm WHERE u.usr_id != 6 AND u.usr_id=ta.user_fi AND tt.obj_fi=o.obj_id AND v.usr_id=u.usr_id AND ta.test_fi=tt.test_id AND tt.test_id = tm.test_fi AND tcr.active_fi=ta.active_id AND tm.passed = 1 AND ta.active_id=tpr.active_fi AND tcr.active_fi=ta.active_id AND v.field_id='2' AND ta.user_fi = $user->usr_id GROUP by ta.submittimestamp";
// echo $user_test;
$tst_records=$this->base_model->executeSelectQuery($user_test);
if (count($tst_records)) {
foreach ($tst_records as $tst) {
$tst=(object)$tst;
$dta['usr_id'] = $user->usr_id;
$dta['firstname'] = $user->firstname;
$dta['matriculation'] = $user->matriculation;
$dta['approve_date'] = $this->udfTextData(1, $user->usr_id);
$dta['department'] = $user->department;
$dta['job_title'] = $this->udfTextData(6, $user->usr_id);
$dta['submittimestamp'] = $tst->submittimestamp;
$dta['test_title'] = $tst->title;
$dta['division'] = $tst->value;
$mark_official = $tst->mark_official;
if ($mark_official == "passed" || $mark_official == "Passed" || $mark_official == "PASSED") {
$result_status = '<span class="label label-primary"> Completed </span>';
$completed = TRUE;
} else {
$result_status = '<span class="label label-danger"> Failed </span>';
$failed = TRUE;
}
$dta['status'] = $result_status;
$final_dataa[] = $dta;
}
}
else{
$dta['usr_id'] = $user->usr_id;
$dta['firstname'] = $user->firstname;
$dta['matriculation'] = $user->matriculation;
$dta['approve_date'] = $this->udfTextData(1, $user->usr_id);
$dta['department'] = $user->department;
$dta['job_title'] = $this->udfTextData(6, $user->usr_id);
$dta['test_title'] = $user->title;
$dta['division'] = $user->value;
$final_dataa[] = $dta;
}
}
}
$dataa['recordss'] = $final_dataa;
$this->load->view('supervisor', $dataa);
I am using if else condition if users have test else not like that .below is my view code
view:
<h3> Skill Matrix Report Process Based Training Record in LMS </h3>
<hr>
<?php $uniq_rec = array_unique($recordss,SORT_REGULAR);
// var_dump($uniq_rec);
$uniq_name = array_unique(array_column($recordss, 'firstname'));
$uniq_test = array_unique(array_column($recordss, 'test_title'));
?>
<table class="table" id="myTable">
<thead>
<tr>
<th>Employe NO</th>
<th>Employe Name</th>
<th>Date Joined</th>
<th>division</th>
<th>department</th>
<th>jobtitle</th>
<?php
foreach ($uniq_test as $row) {
?>
<th><?php echo $row?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($uniq_rec as $row) {
$row = (object)$row;
// var_dump($row);
$date_joined_y = substr($row->approve_date,0,4);
$date_joined_m = substr($row->approve_date,4,2);
$date_joined_d = substr($row->approve_date,6,2);
$date_joined = $date_joined_d."-".$date_joined_m."-".$date_joined_y;
?>
<tr>
<td><?php echo ucfirst($row->matriculation); ?></td>
<td><?php echo $row->firstname?></td>
<td> <?php echo ucfirst($date_joined); ?></td>
<td><?php echo ucfirst($row->division); ?></td>
<td><?php echo ucfirst($row->department); ?></td>
<td><?php echo ucfirst($row->job_title); ?></td>
<?php
foreach ($uniq_test as $uniq) {;
$status = "";
foreach ($recordss as $rec) {
if($uniq == $rec['test_title'] && $rec['firstname'] == $row->firstname){
echo "<td>".$rec['status'].$rec['submittimestamp']."</td>";
$status = "true";
}
}
if($status != "true"){
echo "<td></td>";
}
$status = "";
}
?>
but the main task is i need to display all users who are attempted test or not but those users are under one Supervisor
submittimestampbut the main task is i need to display all users who are attempted test or not but those users are under one Supervisor.i am using if else condition in controller but am getting error like
Message: Undefined index: status and Message: Undefined index:
submittimestamp

Foreach loops in PHP and Joomla

I'm currently managing the display of MySQL content in HTML with foreach loop like this :
<?php
echo "<table class=\"tableau\">
<tr bgcolor=\"#a72333\" class=\"first\">
<th>Repere</th>
<th>Niveau</th>
<th>Enseigne</th>
<th>Activités</th>
</tr>
<tbody>";
$db= JFactory::getDBO();
$query = 'SELECT baseData, sid, fid FROM XXXX_sobipro_field_data';
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as &$value) {
if ($value->sid == 55) {
if ($value->fid == 20) {
$repere = $value->baseData;
}
if ($value->fid == 16) {
$level = $value->baseData;
}
if ($value->fid == 22) {
$title = $value->baseData;
}
if ($value->fid == 17) {
$activity = $value->baseData;
}
if ($value->fid == 21) {
$display = $value->baseData;
}
}
[...]
// It ends at if ($value->fid == 83)
}
So I name my variable like this $title_NUM, $activity_NUM, ..., where _NUM is a number starting at "nothing", it ends at 24 for now, but it could be more if I have more data in my table.
After I get the data I display the html like this :
if ($display == 1) {
echo "<tr bgcolor=\"#eaeaeb\">
<td valign=\"top\">".$repere."</td>
<td align=\"top\">".$level."</td>
<td valign=\"top\"><a data-lightbox=\"width:600;type:iframe;\" href=\"LINK\">".$title."</a></td>
<td align=\"top\">".$activity."</td>
</tr>";
}
And the same happens here I'm displaying each linke of the html "by hand" , O don't have any loop to do the job.
Is there a way to do the job with only loops ?
what i understand so far is that you have
$title1 , $title2 , $title3 , ...
you want to do loop for it
see this example
<?php
for($i=0;$i<=8;$i++)//note it start from 0 to 8
${'test'.$i}=5*$i;
$test9=5*9;
echo "let's test <br/>";
echo $test0.'<br/>';
for($i=1;$i<=9;$i++)//note it start from 1 to 9
echo ${'test'.$i}.'<br/>';
?>

Passing value from one field to another field

I'm new to programming language. I want to know can i get value from other field and pass to other field in same form without using javascript ? Can someone explain to me ? Thank u.
This my form page
<form id="leave_form">
<table><tr>
<td width="70"><b>*</b> Date From:</td>
<td width="120"><span id="lv_date_from_btn"><input readonly class="field_required control" onchange="validateLeave('from')" id="lv_date_from" name="date_from" value="<?php echo $start_date?>" size="10" maxlength="10"/> <img src="images/calendar.gif"/></span></td>
</tr>
<tr>
<td width="70"><b>*</b> Date To:</td>
<td width="120"><span id="lv_date_to_btn"><input readonly class="field_required control" onchange="validateLeave('to')" id="lv_date_to" name="date_to" value="<?php echo $end_date?>" size="10" maxlength="10"/> <img src="images/calendar.gif"/></span></td>
</tr>
<?php if ($userid == '609'):?>
<tr>
<td><b>*</b> Relief Staff: </td>
<td>
<select name="userid" id="frm_userid2" class="field_required control" onchange="validateLeave('relief')" >
<?php
$leavefrom = $_REQUEST['from'];
$leaveto = $_REQUEST['to'];
if (empty($leavefrom))
{
echo '<option value="" selected disabled>Select...</option>';
}
else{
echo '<option value="" selected disabled>Select...</option>';
$sql = "
SELECT distinct fullname FROM core_user LEFT JOIN lms_tran ON lms_tran.userid = core_user.userid where core_user.userid NOT IN (SELECT userid FROM lms_tran WHERE date_from BETWEEN '$leavefrom' AND '$leaveto' AND app_status = 'Approved') AND core_user.userid != 609 AND core_user.status = 'Y' ORDER by fullname ASC
";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row["userid"].'">'.$row["fullname"].'</option>';
}
}?>
</select>
</td>
</tr>
<?php endif; ?>
</table>
</form>
and this is javascript
function validateLeave(type)
{
var days= jQuery('#frm_days').val();
var from = jQuery('#lv_date_from').val();
var to = jQuery('#lv_date_to').val();
var relief = jQuery('#frm_userid2').val();
if (type != 'check')
{
days_incorrect = true;
}
if (type == 'days' || type == 'from')
{
to = '';
relief = '';
}
if (type == 'to')
{
days = '';
}
if (
(
(days == '' ? 0 : 1) +
(to == '' ? 0 : 1) +
(from == '' ? 0 : 1)
) < 2
)
{
days_correct = false;
return;
}
days = parseFloat(days);
jQuery('#frm_days').val(days);
jQuery('.control').attr('disabled', true);
jQuery('#lv_loading').show();
jQuery.post('index.php?_m=lms&_a=leave_validate&from='+from+'&to='+to, {from:from,to:to,days:days}, function(res){
eval('var r = '+res+';');
if (r.status == 'OK')
{
days_incorrect = false;
if (r.to)
{
jQuery('#lv_date_to').val(r.to);
}
if (r.from)
{
jQuery('#lv_date_from').val(r.from);
}
if (r.days)
{
jQuery('#frm_days').val(r.days);
}
}
else if (r.status == 'HOLIDAYERROR')
{
alert('Incorrect leave start date. Leave start date can not fall into Weekend or Public Holidays');
days_incorrect = true;
}
else
{
alert('Incorrect leave period. Please check back Leave Start, Leave End and Leave Days')
days_incorrect = true;
}
jQuery('.control').attr('disabled', false);
jQuery('#lv_loading').hide();
});
}
and i could'nt get the value return in php code as i hv pass value via jQuery.
No, you can't. The only way to do something interactive is to do it with javascript if you the to see it. If it doesn't matter you can do this on the server by assigning the second variable with the value of the first.

PHP - How to format cell background and font color according to MySQL query result in PHP file

I have workable mysql query in PHP file.
It is giving me 3 possible different results in my table on site in one column. They are 'WON', 'LOST', or 'PENDING'.
What i want to achieve further is when there is WON word in those specific cell to be in green background, when query result turns LOST to be red background, when PENDING to be grey.
In which way to do this?
I am newbie in this so couldnt find anser myself online.
Here is code of workable query:
<?
$qry = "
SELECT timelive,match_title,selection,
CASE
WHEN game.result LIKE '' THEN 'PENDING'
WHEN game.result LIKE game.selection THEN 'WON'
WHEN game.result NOT LIKE game.selection THEN 'LOST'
END AS result
FROM game
";
$searchText = "";
if($_REQUEST['search_text']!=""){
$searchText = mysql_real_escape_string($_REQUEST['search_text']);
$qry .=" WHERE game.timelive LIKE '%$searchText%' " .
" OR game.match_title LIKE '%$searchText%' " .
" OR game.selection LIKE '%$searchText%' " .
" OR game.username LIKE '%$searchText%'";
}
$qry .= " ORDER BY timelive DESC";
$obj = new pagination_class($qry,$starting,$recpage);
$result = $obj->result;
?>
and HTML part of code for this part of output on site is this:
<table>
<?if(mysql_num_rows($result)!=0){
$counter = $starting + 1;
while($data = mysql_fetch_array($result)) {?>
<tr>
<td align="center"><? echo $data['username']; ?></TD>
<td align="center"><? echo $data['result']; ?></TD>
</tr>
<?
$counter ++;
} ?>
i need to get this desired formatting described above according to output word in 'result' column.
Thanks.
A couple of options, the brute-force way, in which you simply apply a generated style, or by predefining style classes and applying them based on the output...
In the latter case (most reasonable, IMO), you simply apply the content of $result to the class property:
<td align="center" class="<?php echo $result;?>"><? echo $data['result']; ?></td>
In the first case, you might have something like this:
function getStyleColorForStatus($status) {
if ($status == 'WON') {
return 'Green';
}
else if ($status == 'LOST') {
return 'Red';
}
else if ($status == 'PENDING') {
return 'Grey';
}
return '';
}
<td align="center" style="background-color:<?php echo getStyleColorForStatus($data['result']); ?>"><? echo $data['result']; ?></td>
I'm missing the part, which creates your $data variable.
Just add "game.result" to your $data array. Now in your code, you could do something like this:
<tr>
<td align="center" class="<? echo $data['result'];?>"><? echo $data['username']; ?></TD>
<td align="center"><? echo $data['result']; ?></TD>
</tr>
Now you can work with CSS. Create three classes for LOST, PENDING and WON. Example:
.LOST {
background-color: #F00;
}
Your username field in your table should have a red background, when game.result is "LOST"
Create the following css definitions, if possible in a separate css file:
.won
{
background-color:green;
}
.lost
{
background-color:red;
}
After this, link the css file to your page and finally use jQuery to add/remove the css class depending on the given condition.
You can read more on the following links:
http://api.jquery.com/removeClass/
http://api.jquery.com/addClass/
Assign a css class to your table cell, based on the result, like so:
$tdClass = '';
switch ($data['result']) {
case 'WON':
$tdClass = 'won';
break;
case 'LOST':
$tdClass = 'lost';
break;
case 'PENDING':
$tdClass = 'pending';
break;
}
So obviously, this is your php, in your html you do:
<td class="<?php $tdClass; ?>"><?php echo $data['result']; ?></td>
I would lose the align="center" and use in your css text-align: center; instead. Furthermore, in your css, you'd do:
.won {
background: green;
}
.lost {
background: red;
}
.pending {
background: grey;
}
But instead of green, red, etc. choose the exact color you like.
before echo the result put an condition like
if( $data['result'] == 'WON' ){
echo '<div class="green">' . $data['result'] . '</div>'
}
elseif( $data['result'] == 'LOST' ){
echo '<div class="red">' . $data['result'] . '</div>'
}
elseif( $data['result'] == 'PENDDING' ){
echo '<div class="gray">' . $data['result'] . '</div>'
}

Categories