webguync Posted January 22, 2009 Share Posted January 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/ Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 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.... Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/#findComment-743426 Share on other sites More sharing options...
webguync Posted January 22, 2009 Author Share Posted January 22, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/#findComment-743536 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 Assuming your css is correct, I am wondering what is in $pcnt[$i] What happens when you echo $pcnt[$i]; because there is no reason why the <div> aspects wouldn't work... Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/#findComment-743567 Share on other sites More sharing options...
webguync Posted January 22, 2009 Author Share Posted January 22, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/#findComment-743575 Share on other sites More sharing options...
bobleny Posted January 22, 2009 Share Posted January 22, 2009 Yikes! Well you don't kneed to do that... This should be all you need: if(($pcnt[$i]*100) > 90) Simply multiplying it by 100 will do the trick or, you can leave that side alone and do this: if($pcnt[$i] > .90) They both should work well. Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/#findComment-743586 Share on other sites More sharing options...
webguync Posted January 23, 2009 Author Share Posted January 23, 2009 that worked. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/#findComment-744391 Share on other sites More sharing options...
bobleny Posted January 24, 2009 Share Posted January 24, 2009 Don't forget to mark this topic as solved. Quote Link to comment https://forums.phpfreaks.com/topic/141984-solved-set-css-code-based-on-a-certain-percentage-with-php/#findComment-744859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.