Jump to content

[SOLVED] set CSS code based on a certain percentage with PHP


webguync

Recommended Posts

Hi,

 

I have a results page which calculates a percentage of a test result by taking the number correct divided by the number of total questions.

 

$pcnt[] = ($row['num_correct'])/($row['total_questions']);

 

the information and other data is displayed in a table row using the following PHP

 

<tr class="<?php echo $i%2 ? 'hilite' : 'nohilite'; ?>">
		<td><?php echo $name[$i];?></td>
		<td><?php echo $numCorr[$i];?></td>
		<td><?php echo (number_format(($pcnt[$i]*100),2).'%'); ?></td>
		<td><?php echo $incorr[$i];?></td>
		<td><?php echo (date('F j, Y  g:i A',($date[$i]+36000)));?></td>
	</tr>

 

 

what I would like to do is add some CSS to display a row of one color if the score percentage is 90% or above and another color if the score is below 90%. Doing the CSS part is no problem, but combining that with the PHP is what I need help with.

 

TIA

Link to comment
Share on other sites

The easiest way to do it would probably be to change the tag attribute:

 

CSS.css

.above
{
color: #0f0;
}

.below
{
color: #00f;
}

 

 

PHP:

<?php
if($percent > 90)
{
	echo "<div class='above'>". $someData ."</div>";
}
else
{
	echo "<div class='below'>". $someData ."</div>";
}
?>

 

That should so the trick. If not, you might be stuck with inline css and or some JavaScript....

Link to comment
Share on other sites

thanks for the reply. That didn't seem to wor though. It looks like the pass/fail isn't getting resolved. Here is the entire code.

 


<?php


$db = new Database('localhost','username','pw','DBname',0);

$sql = 'SELECT name, total_questions, incorrect_resp, num_correct, mb_results.date
	FROM mb_roster, mb_log LEFT JOIN mb_results USING (log_id)
	WHERE mb_roster.user_id = mb_log.user_id AND mb_roster.user_id > 0
	ORDER BY name, date';

$report = $db->query($sql);

if ($report->get_rows()) {
//loop to create arrays for each column
while ($row = $report->fetch_assoc()) {
	if($row['num_correct']) {
		$name[] = $row['name'];
		$numCorr[] = $row['num_correct'];
		$pcnt[] = ($row['num_correct'])/($row['total_questions']);
		$incorr[] = $row['incorrect_resp'];
		$date[] = $row['date'];
		}
	}


}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>EXAM</title>
<link href="report.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>

<body>

<div>
<table id="resultlist">
   
	<tr>
		<th scope="col">Employee Name</th>
		<th scope="col">Number Correct</th>
		<th scope="col">Score</th>
		<th scope="col">Questions Answered Incorrectly</th>
		<th scope="col">Date Completed</th>

	</tr>
	<?php if (!isset($name)) { ?>
	<tr><td colspan="5">There are no scores to display</td></tr>
	<?php
	} else {
	for ($i=0; $i<count($name); $i++) { ?>
	<tr>
		<td><?php echo $name[$i];?></td>
		<td><?php echo $numCorr[$i];?></td>
            

		<td><?php echo (number_format(($pcnt[$i]*100),2).'%'); ?></td>
		<td><?php echo $incorr[$i];?></td>
		<td><?php echo (date('F j, Y  g:i A',($date[$i]+36000)));?></td>
            <td><?php
   if($pcnt[$i] > 90)
   {
      echo "<div class='passed'>". Passed ."</div>";
   }
   else
   {
      echo "<div class='failed'>". Failed ."</div>";
   }
?></td>
	</tr>
	<?php }
	} ?>

</table>
</div>
</body>
</html>

Link to comment
Share on other sites

that prints out the number, but not the percentage. For example it would print out 0.91884, so maybe that is the problem. I tried changing the code to this though and I get an error.

 

<td><?php
   if(number_format(($pcnt[$i]*100),2).'%') > 90)
   {
      echo "<div class='passed'>" Passed"</div>";
   }
   else
   {
      echo "<div class='failed'>" Failed "</div>";
   }
?></td>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.