I have a foreach loop below where it displays data for each table row:
foreach ($studentData['questions'] as $questionId => $questionData) {
...
echo '<td width="30%" class="answers">'.htmlspecialchars($questionData['answer']).'</td>' . PHP_EOL;
...
echo '<td width="30%" class="studentanswer">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
}
What I want to do is that if an studentanswer matches an answer, then that studentanswer will turn green, if not a match then display incorrect answers in red, and if full studentanswer matches 100% with answer then I want a variable such as $check to display string station fully correct in green else if not 100% match then display string not full correct in red.
So for example the above code could display this:
Answer: B C
Student Answer: B D
The output of the above should display student answer B as green as it matches with answer B but student answer D should be red as there is no D in answer. The variable $check should state in red not fully correct as the student's answer is not fully correct, just partial.
But how can this be achieved?
UPDATE:
It is not changing color of text:
if($questionData['answer'] == $questionData['studentanswer']) {
$style = 'green';
$checked = 'Fully Correct';
} else {
$style = 'red';
$checked = 'Not Correct / Not Fully Correct';
}
echo '<td width="30%" class="answers '.$style.'">'.htmlspecialchars($questionData['answer']).'</td>' . PHP_EOL;
...
echo '<td width="30%" class="studentanswer '.$style.'">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
CSS:
.red{
color: red;
}
.green{
color: green;
}
Try this:
$check = true;
foreach ($studentData['questions'] as $questionId => $questionData) {
$studentAnswer = htmlspecialchars($questionData['studentanswer']);
$answer = htmlspecialchars($questionData['answer']);
...
echo '<td width="30%" class="answers">'.htmlspecialchars($questionData['answer']).'</td>' . PHP_EOL;
...
if($answer == $studentAnswer)
{
echo '<td width="30%" class="studentanswer greenAnswer">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
}
else
{
echo '<td width="30%" class="studentanswer redAnswer">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
$check = false;
}
}
if($check)
{
echo '<p class="greenAnswer">Fully Correct!</p>';
}
else
{
echo '<p class="redAnswer">Not Fully Correct!</p>';
}
In your CSS put the following:
greenAnswer
{
color:green;
}
redAnswer
{
color:red;
}
<?php
if($questionData['answer'] == $questionData['studentanswer'] {
$style = 'color:green';
$checked = 'right';
} else {
$style = 'color:red';
$checked = 'not fully correct';
}
?>
...
<td width="30%" class="answers" style="<?php echo $style; ?>">
<?php echo $checked; ?>
You can use conditions to achieve this.
With php's if() function, you can make sure your script meets certain conditions that you choose before processing a certain piece of code.
So to check if a student's answer matches the teachers answer, inside the loop, you'll add something like:
if( $questionData['studentanswer'] == $questionData['answer'] )
{
// The answer was correct
}
else {
// This is for the answer not being correct
}
This code just shows an example of how to achieve what you are trying to accomplish, of course you'll need to make your own to exactly match your needs.
If you have any other questions or need further assistance feel free to ask.
Related
I'm not too sure how to call the Associative array to verify if the number is true or false because I am doing a simple checker for enrollment class. The max class capacity is 40 and the file is combined with HTML and PHP.
I did it like this:-
<?php
//Create the association array.
$classInfo = array("J1" => 20 ,"J2" => 30,"J3" => 10,"J4" => 43,
"J5" => 40,"J6" => 45,"J7" => 15,"J8" => 34,"J9" => 10,"J10" => 45);
$class = array_keys($classInfo);
$totalEnroll = count($classInfo);
?>
The Code:-
<table width="300" style="border: 1px solid black">
<tr>
<?php
// class and enroll Lists
echo "<td width=20>";
echo "Class"."        "." Enroll"."<br><hr>";
for($i=0; $i < $totalEnroll; ++$i) {
echo $class[$i] . "     " .
$classInfo[$class[$i]] . "<br>";
}
echo "</td>";
// Enroll to check whether the classroom is full.
echo "<td width=20>";
echo "Class States" . "<br><hr>";
for($check = 0; $check < 10; $check++){
if($classInfo[$totalEnroll[$check]] >= 0 &&
$classInfo[$totalEnroll[$check]] <= 40){
echo "Full";
echo " <br>";
} else {
echo "Not Full";
echo " <br>";
}
}
echo "</td>";
?>
</tr>
</table>
The Output That I want:-
Class
Enroll
Full States
J1
20
Not Full
J5
40
Full
Do check on the part say `//Enroll to check whether the classroom is full. This is where the code of the enrollment class is checked.
However, by the way, the output can it be aligned more cleanly in a table-like column.
You are displaying the data in two separate loops which makes it difficult to align the data, you should make it into 1 loop and use the <tr> and <td> tags around each group of items...
<table width="300" style="border: 1px solid black">
<?php
foreach ( $classInfo as $className => $enrolled ) {
echo "<tr>";
// class and enroll Lists
echo "<td>{$className}</td>";
echo "<td>{$enrolled}</td>";
// Enroll to check whether the classroom is full.
echo "<td>";
if($enrolled <= 40){
echo "Full";
} else {
echo "Not Full";
}
echo "</td>";
echo "</tr>";
}
?>
</table>
(This doesn't include a header, but I'm sure you can add that).
I am trying to determine questions answered correctly and questions answered incorrectly. If answered correctly then display "Fully Correct" else if incorrect then display the "Incorrect" message. Problem is no matter if correct or not it always displays that question is answered incorrectly.
Below is code:
$check = true;
foreach ($studentData['questions'] as $questionId => $questionData) {
if($questionData['answer'] == $questionData['studentanswer'])
{
echo '<td width="30%" class="studentanswer green"><strong>'.htmlspecialchars($questionData['studentanswer']).'</strong></td>' . PHP_EOL;
}
else
{
echo '<td width="30%" class="studentanswer red"><strong>'.htmlspecialchars($questionData['studentanswer']).'</strong></td>' . PHP_EOL;
$check = false;
}
if($check)
{
echo '<p class="green"><strong>Fully Correct</strong></p>';
}
else
{
echo '<p class="red"><strong>Not Correct / Not Fully Correct</strong></p>';
}
}
after this line again set to true for $check
if($questionData['answer'] == $questionData['studentanswer'])
{
echo '<td width="30%" class="studentanswer green"><strong>'.htmlspecialchars($questionData['studentanswer']).'</strong></td>' . PHP_EOL;
$check = true;
} else
$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
$total_price2=$rows3['qty']*$rows3['number'];
$banana = $banana + 1;
if ($total_price2!=0)
{
if ($banana %2 ==0)
{
echo "<tr class=\"alt\">";
}
else
{
echo "<tr>";
}
echo "<td>".$rows3['member']."</td>";
echo "<td>".$rows3['payment']."</td>";
echo "<td>$".number_format($total_price2,2)."</td>";
}
echo "</tr>";
}
Problems:
The "banana" alternates the colour of the table row (class="alt") by using modulus (%) to check if banana is a odd number.
Not Working, I see the browser is self-closing the opening <tr> tag.
eg:
<tr><td>person</td><td>data</td><td>$10.00</td></tr>
<tr class="alt"></tr> (Repeats in this fashion)
UPDATE
I have discovered that the reiterating banana always returns ODD NUMBERS: 1 , 3, 5, etc
MySQL is not running correctly
SELECT table1.member, table1.paid, table1.payment,table2.qty,table3.number FROM table1,table2,table3 WHERE table1.member = table2.member AND table1.payment="fruit"
It is giving me wrong data like so:
person1 $10.00
person1 $0.00
person2 $10.00
person2 $0.00
etc
If all else fails, you could just use CSS:
tr {background-color: blue;}
tr:nth-of-type(2n) {background-color: red;}
Try doing this for a debug first:
echo "<tr class=\"alt\"> ";
My guess is that you have no data contained in your <tr> and is being squished to 0 px tall by your browser.
EDIT: I'm not entirely sure giving a class to your <tr> will filter down into the <td> based on certain browser DOM parsing. Whenever I do a zebra row, I'll assign the class to the <td>. I'm no designer by any standard, though. :)
Humor me and try this please:
$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
$total_price2=$rows3['qty']*$rows3['number'];
$banana++;
if ($banana % 2 == 0) {
$td_class = "alt";
} else {
$td_class = "";
}
echo "<tr>";
if ($total_price2!=0) {
echo "<td class='{$td_class}'>".$rows3['member']."</td>";
echo "<td class='{$td_class}'>".$rows3['payment']."</td>";
echo "<td class='{$td_class}'>$".number_format($total_price2,2)."</td>";
}
echo "</tr>";
}
That's not going to work. You're only opening the table row if $total_price2!=0. It seems you should only output the closing </tr> tag inside that IF block.
Try this instead:
<?php
$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
$total_price2=$rows3['qty']*$rows3['number'];
if ( !$total_price2)
continue;
$banana = $banana + 1;
if ($banana %2 ==0)
{
echo "<tr class=\"alt\">";
}
else
{
echo "<tr>";
}
echo "<td>".$rows3['member']."</td>";
echo "<td>".$rows3['payment']."</td>";
echo "<td>$".number_format($total_price2,2)."</td>";
echo "</tr>";
}
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>'
}
I am pretty sure that if I use something like this in css
#list {
float:left;
}
I can format the display of my array to look like
A B C
rather than
A
B
C
The problem is I am having problems formatting the php of the with the proper <div>'s to give me what I want.
foreach ( $model->address as $address ) {
if ($address->Address) echo $address->Address.'<br />';
if ($address->city->Name) echo $address->city->Name;
}
Can someone help me modify the foreach above to achieve the different layout. I also need to be able to format the Address differently then the Name
EDIT: For clarification...
A =
Address
City
B =
Address
City
C =
Address
City
You need to wrap each address in a div and use a class to float it:
PHP/HTML:
foreach ( $model->address as $address ) {
echo '<div class="address-item">';
if ($address->Address) echo $address->Address.'<br />';
if ($address->city->Name) echo $address->city->Name;
echo '</div>';
}
CSS:
.address-item {
float: left;
}
$out = "<ul>";
foreach($model->address as $address){
if(!isset($address->Address) && !isset($address->city->Name)) continue;
$out .= "<li>" . isset($address->Address) ? $address->Address : "" . " "
. isset($address->city->Name) ? $address->city->Name : "" . "</li>";
}
$out .= "</ul>";
print $out;
CSS
li {
float: left;
}
something like:
foreach ( $model->address as $address ) {
echo '<div class="list">';
if ($address->Address) echo $address->Address.'<br />';
if ($address->city->Name) echo $address->city->Name;
echo '</div>';
}
wherein, the CSS will be:
.list {float:left;}