It is only showing one value for each row - php

I have a phpfiddle here: http://phpfiddle.org/main/code/get-rps
The problem I am getting is that in the Total Marks column, it is only display the total marks for the last question in all of the rows for all questions, rather than looping through all the total marks and displaying the correct total marks for each question. What am I doing wrong in the code?
Below is code:
<?php
$incorrect_ans = array(
array('A','C','D'),
array('B','C','D'),
array('A','B','D'),
array('A','B','C'));
$searchQuestionNo = array(
1,
2,
2,
3);
$totalMarks = array(
3,
5,
5,
2);
$ques_ans = array(); //to store incorrect answers against ques no.
$q_occ_count = array_count_values($searchQuestionNo);
foreach($searchQuestionNo as $key => $questionNo)
{
if ( ! array_key_exists($questionNo, $ques_ans))
{
if($q_occ_count[$questionNo] === 1) //if a ques has only one correct ans
{
$ques_ans[$questionNo] = $incorrect_ans[$key]; //store the array of incorrect ans against the ques no as key
}
else //if a ques has more than 1 correct ans
{
//find the intersection of incorrect_ans arrays for this ques
$q_keys = array_keys($searchQuestionNo, $questionNo);
$q_incorrect_ans = $incorrect_ans[$q_keys[0]];
foreach($q_keys as $q_key) {
$q_incorrect_ans = array_values(array_intersect($q_incorrect_ans, $incorrect_ans[$q_key]));
}
$ques_ans[$questionNo] = $q_incorrect_ans; //store the array of incorrect ans against the ques no as key
}
}
}
?>
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionnoth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th>
<th class='marksth'>Total Marks</th>
</tr>
</thead>
<tbody>
<?php
foreach($ques_ans as $questionNo => $inc_ans)
{
$q_row_span = count($inc_ans);
$row_count = 0;
?>
<tr class="questiontd">
<!-- Question No -->
<td class="questionnumtd q<?php echo $questionNo?>_qnum" rowspan="<?php echo $q_row_span ?>">
<?php echo $questionNo?><input type="hidden" name="numQuestion" value="<?php echo $questionNo?>" />
</td>
<!-- first incorrect ans -->
<td class="answertd"><?php echo $inc_ans[$row_count]; ?></td>
<!-- Marks -->
<td class="totalmarkstd" rowspan="<?php echo$q_row_span?>"><?php echo$totalMarks[$key]?></td>
</tr>
<?php
//remaining incorrect answers in separate row (if any) follows here
if($row_count < $q_row_span - 1)
{
for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>
<tr><td class="answertd"><?php echo $inc_ans[$i]; ?></td></tr>
<?php
}
}
}
?>
</tbody>
</table>

This line is your problem.
<?php echo$totalMarks[$key]?>
$key is set from your loop above, and is never reset in the bottom loop, so it is still has the last value from that loop. I'm assuming it should be <?php echo$totalMarks[$questionNo]?>

Related

How to get array from list

I have a problem for my program.
This my script:
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
<tr>
<th> echo $no++; </th>
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
}
<td>Count</td>
</tr>
}
and this is my result
result page
I want to replace the words "count" with the sum of each column in the same row. and my question, how I can add all column in one rows.
example : row 1 = 1 + 0.5 + 0,33 + 0.25 = 2,08
Thank you for helping, I am sorry if my English so bad.
Try something like this
initialize $count as zero before the inner loop
and in inner loop increment county
<?php
$x=1;
$no=1;
$tot=0;
$bobot=0;
$jumlah=4;
for($i=1;$i<=$jumlah;$i++) {
?>
<tr>
<th> <?php echo $no++;?> </th>
<?php
$count=0;
for($a=$i;$a<=$jumlah;$a++) {
echo "<td>".$x/$a."</td>";
$count=$count+($x/$a);
}
?>
<td><?=$count;?></td>
</tr>
<?php
}
?>

php mysql update 3rd column value if 1st column value > 2nd column value

I am comparing 1st [ Sent ] & 2nd column [ Last Update Date ] values. if 1st column value is greater than 2nd column value , than i want to save the value "attempted" in 3rd column [ Attempted ].
<table>
<tr>
<th class="table-header">Sent</th>
<th class="table-header">Last Update Date</th>
<th class="table-header">Attempted</th>
</tr>
<?php
if(!empty($orderrecords))
{
foreach($orderrecords as $k=>$v)
{
?>
<tr>
<td><?php echo $orderrecords[$k]["sent_date"]; ?></td>
<td><?php echo $orderrecords[$k]["lud"]; ?></td>
<td>
<?php
$orderrecords[$k]["sent_date"]= strtotime($orderrecords[$k]["sent_date"]);
$orderrecords[$k]["lud"]= strtotime($orderrecords[$k]["lud"]);
$sqlecom = 'UPDATE table SET attempted = "attempted" WHERE $orderrecords[$k]["lud"] < $orderrecords[$k]["sent_date"]';
$db_handleecom = new DBController();
$resultecom = $db_handleecom->executeUpdate($sqlecom);
echo $orderrecords[$k]["attempted"];
?>
</td>
</tr>
<?php
}
}
?>
</table>
update : I tried below code, but still 3rd column values are not saving in database.
<?php
$orderrecords[$k]["sent_date"]= strtotime($orderrecords[$k]["sent_date"]);
$orderrecords[$k]["lud"]= strtotime($orderrecords[$k]["lud"]);
if ($orderrecords[$k]["lud"] < $orderrecords[$k]["sent_date"])
{
$sqlecom = 'UPDATE table SET attempted = "attempted"
WHERE id = ' . $orderrecords[$k]['id'];
$db_handleecom = new DBController();
$resultecom = $db_handleecom->executeUpdate($sqlecom);
//echo "attempted";
}
echo $orderrecords[$k]["attempted"];
?>
Update 2 :
when i add below code on top of the file, than complete table disappears!!!! :
mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
here is full code : https://pastebin.com/VwQSjgQ5
Try this (I used same code structure you required) :
<table>
<tr>
<th class="table-header">Sent</th>
<th class="table-header">Last Update Date</th>
<th class="table-header">Attempted</th>
</tr>
<?php
if(!empty($orderrecords)){
foreach($orderrecords as $k=>$v){
?>
<tr>
<td><?php echo $v["sent_date"]; ?></td>
<td><?php echo $v["lud"]; ?></td>
<?php
if ($v["lud"] < $v["sent_date"]) {
$attempted= "attempted";
$sqlecom = 'UPDATE table SET attempted = "attempted" WHERE ORDERID = '.$v["order_id"];
$db_handleecom = new DBController();
$resultecom = $db_handleecom->executeUpdate($sqlecom);
}
else {
$attempted = "";
}
?>
<td><?php echo $attempted; ?></td>
</tr>
<?php
}
}
?>
</table>

php foreach combine 2 array into 1 row (workdays & data)

I have a problem with my current "Attendance Project", so I have 2 arrays.
1st array is to show a "workdays"
the 1st array show only workdays in current month ex:April, so the result in my 1st array is (3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28)
2nd array is showing Employee Attendance in current month ex:April, so the result in my 2nd array is (17, 19)
here is my current code :
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th style="width: 200px">Siswa</th>
<!-- <?php for($i = 1; $i < 31; ++$i){?>
<th><?= $i ?></th>
<?php } ?> -->
<?php foreach($workdays as $w){ ?>
<th><?=$w;?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
// for($x = 1; $x < 27; ++$x){
foreach($records as $r){
?>
<tr>
<td style="width: 200px"><?=$r->StudentName;?></td>
<?php
?>
<?php
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } } ?>
</tr>
<?php } ?>
</tbody>
</table>
It will produce :
I want value (17 and 19) will markup the data with yellow background, and the table is NOT out of range.
Any help will appreciate..
Your code seems messy and I'm not gonna try to fix it on what you have, but I'll suggest solution:
1st - run foreach ($workdays as $w) and make header
2nd - run foreach ($workdays as $w) and make table-body like:
foreach ($workdays as $w) {
if (in_array($w, $tanggale)) //if tanggle is the one with 17 and 19
{
//code
}
else
{
//code
}
}
Simply what u can do is combine the 2 arrays in to one and then iterate the combine array as per your requirment.
Check below code for combining the array
<?php
$working_days = array(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28);
$present_days = array(17.19);
$combine_attendence_array = array();
foreach($working_days as $day) {
$combine_attendence_array[$day] = 'Absent';
if(in_array($day, $present_days)) {
$combine_attendence_array[$day] = 'Present';
}
}
?>
This code will create combine array with key as day and value is present or Absent.
Now you can iterate as per your requirement below is the iteration code.
foreach($combine_attendence_array as $day => $value){
if($value == 'Present'){ ?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } ?>
<?php } ?>
I hope this answers solves your question.
Do it in this way
<?php
foreach($tanggale as $t){
if(in_array($t,$workdays)){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } ?>
</tr>
<?php } ?>
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
$color = "#FFF000";
$text = "M";
} else {
$color = "#48C9A9";
$text = "O";
}
}
?>
<td style="background: <?php echo $color; ?>"><?php echo $text; ?></td>
<?php }?>

Random selection of dynamic check boxes

I am working on building a small php file. I need to select random check boxes. check boxes dynamic as they are pulled from a table with some conditions.
Please let me know if i can randomly select few check boxes say if i get 100 results from the mysql table, 10 random check box needs to be selected.
<?php
$n1=0;
$rambo = strip_tags(#$_POST["assoocc"]);
$r11 = strip_tags(#$_POST["ccdate"]);
$r12 = strip_tags(#$_POST["rrdate"]);
$submit = #$_POST["submit22"];
?>
<body><div>
<form action="selectedcc.php" method="post">
<center> <table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th colspan="3"> Total cases for QA</th>
<th colspan="2"><?php
$ticket=mysql_query("SELECT * FROM us_tickets where (status='Resolved') and (associate='$rambo') and (resolve_date >='$r11') and (resolve_date <='$r12')");
$m1=mysql_num_rows($ticket);
echo "$m1";
?></th>
<th colspan="2"><select name="assoocc1" class="form-control" id="sel1" style="width: 150px;" >
<?php echo "<option value=$rambo selected>".$rambo."</option>";
?>
</select></th>
<th colspan="1"><input type="submit" name="sub" value="Submit For QA "/></th>
</tr>
<tr>
<th>Select Here</th>
<th>Case Id</th>
<th>Task</th>
<th>Number of ASINs</th>
<th>SLA Details</th>
<th>Create date</th>
<th>Resolved date</th>
<th>Rootcause</th></tr>
<?php
if ($submit)
{
if ($rambo&&$r11&&$r12)
{
if ($r11<$r12)
{
$ticket=mysql_query("SELECT * FROM us_tickets where (status='Resolved') and (associate='$rambo') and (resolve_date >='$r11') and (resolve_date <='$r12')");
$m1=mysql_num_rows($ticket);
for($k=0; $k<$m1; $k++)
{
$caseid1 = $task1 = $asin1 = $sla1 = $associate1 = $quer = "";
${"caseid".$k}=mysql_result($ticket,$k, "case_id");
${"task".$k}=mysql_result($ticket,$k, "sub_issue");
${"asin".$k}=mysql_result($ticket,$k, "asin");
${"vendor_credit".$k}=mysql_result($ticket,$k, "vendor_credit");
${"create_date".$k}=mysql_result($ticket,$k, "create_date");
${"resolve_date".$k}=mysql_result($ticket,$k, "resolve_date");
${"root_cause".$k}=mysql_result($ticket,$k, "root_cause");
${"associate".$k}=mysql_result($ticket,$k, "associate");
echo "<tr>"."<th>"."<input type='checkbox' value='${'caseid'.$k}' name='checkboxvar[]'>"."</th>"."<th>".${'caseid'.$k}."</th>"."<th>".${'task'.$k}."</th>"."<th>".${'asin'.$k}."</th>"."<th>".${'vendor_credit'.$k}."</th>"."<th>".${'create_date'.$k}."</th>"."<th>".${'resolve_date'.$k}."</th>"."<th>".${'root_cause'.$k}."</th>"."</tr>";
}
}
else
{
echo "<script>alert('Error:START DATE should be less than END DATE. Go back to QA Home page and enter all fields')</script>";
}
}
else
{
echo "<script>alert('Error:Go back to QA Home page and enter all fields')</script>";
}
}
?>
</table></center></form></div></body></html>
Took the random function UniqueRandomNumbersWithinRange from Generating UNIQUE Random Numbers within a range - PHP
<?php
function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$arr = array_slice($numbers, 0, $quantity);
sort($arr);
return $arr;
}
$chkArr = array();
$rand = UniqueRandomNumbersWithinRange(0, 100, 10);
$rIdx = 0;
for($k=0; $k<100; $k++){
$caseid1 = $task1 = $asin1 = $sla1 = $associate1 = $quer = "";
${"caseid".$k}=mysql_result($ticket,$k, "case_id");
${"task".$k}=mysql_result($ticket,$k, "sub_issue");
${"asin".$k}=mysql_result($ticket,$k, "asin");
${"vendor_credit".$k}=mysql_result($ticket,$k, "vendor_credit");
${"create_date".$k}=mysql_result($ticket,$k, "create_date");
${"resolve_date".$k}=mysql_result($ticket,$k, "resolve_date");
${"root_cause".$k}=mysql_result($ticket,$k, "root_cause");
${"associate".$k}=mysql_result($ticket,$k, "associate");
$chk = '';
if( in_array($k, $rand) )
$chk = "checked";
array_push($chkArr, "<tr>"."<th>"."<input type='checkbox' ".$chk ." value='${'caseid'.$k}' name='checkboxvar[]'>"."</th>"."<th>".${'caseid'.$k}."</th>"."<th>".${'task'.$k}."</th>"."<th>".${'asin'.$k}."</th>"."<th>".${'vendor_credit'.$k}."</th>"."<th>".${'create_date'.$k}."</th>"."<th>".${'resolve_date'.$k}."</th>"."<th>".${'root_cause'.$k}."</th>"."</tr>");
}
echo implode($chkArr);
?>

Mysql display rows as columns [duplicate]

This question already has an answer here:
MySql Transpose Row into Column and Column into Row [duplicate]
(1 answer)
Closed 7 years ago.
I want to display rows as columns.
This is mysql table called Term
|Name ||Year ||HTML ||CSS ||Js ||
|Year1 ||2013-08-30||90 ||70 ||70 ||
|Year2 ||2014-08-30||100 ||65 ||80 ||
|Year3 ||2015-08-30||80 ||95 ||90 ||
What I want is to display as columns like this
|Subject||Year1 ||Year2 ||Year3 ||
|HTML ||90| ||100 ||80 ||
|CSS ||70| ||65 ||95 ||
|JS ||70| ||80 ||90 ||
Code
<tr>
<th>Code</th><th>Subject</th><th>year1</th>
<th>year2</th><th>year3</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM term WHERE Stdid='$id'");
while ($row = mysql_fetch_assoc($query)) {
foreach($row as $key => $value) {
?>
<tr>
<th><?php echo $key ?></th>
<th><?php echo $value?></th>
<th>99</th><th>00</th>
</tr>
<?php } } } ?>
</table>
My result only works for the first year
|Subject||Year1 ||Year2 ||Year3||
|HTML ||90| ||? ||? ||
|CSS ||70| ||? ||? ||
|JS ||70| ||? ||? ||
You first need to transpose all data in a temporary array before you can output it again. I'll give you 2 methods to do this.
Method 1: just fetch every row and switch the row index by the column index:
<table>
<tr>
<th>Subject</th>
<th>year1</th>
<th>year2</th>
<th>year3</th>
</tr>
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'database');
$id = 1;
$report = array();
$columnIndex = 0;
$query = $mysqli->query("SELECT HTML, CSS, Js FROM term WHERE Stdid='$id'");
while ($results = $query->fetch_assoc()) {
foreach ($results as $course => $score) {
$report[$course][$columnIndex] = $score;
}
$columnIndex++;
}
foreach ($report as $course => $results) { ?>
<tr>
<th><?php echo $course; ?></th>
<?php foreach ($results as $score) { ?>
<th><?php echo $score; ?></th>
<?php } ?>
</tr>
<?php } ?>
</table>
Method 2: Fetch all rows in an array, so it becomes an array of arrays and use array_map with callback NULL to transpose it (See example 4 at http://php.net/manual/en/function.array-map.php).
You need to add the course names in the initial array to include them in the end result.
<table>
<tr>
<th>Subject</th>
<th>year1</th>
<th>year2</th>
<th>year3</th>
</tr>
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'database');
$id = 1;
$data = array(array('HTML', 'CSS', 'Js'));
$query = $mysqli->query("SELECT HTML, CSS, Js FROM term WHERE Stdid='$id'");
while ($row = $query->fetch_assoc())
{
$data[] = $row;
}
$data = call_user_func_array('array_map', array_merge(array(NULL), $data));
?>
<?php
foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<th><?php echo $value?></th>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Try this.
<table width = "1093" align="center" bgcolor="pink">
<tr align="center">
<td colspan="6"><h2>View Term</h2></td>
</tr>
<tr align="center" bgcolor="">
<th>S.N</th>
<th>Subject</th>
<th>Year 1</th>
<th>Year 2</th>
<th>Year 3</th>
</tr>
<?php
$db = mysqli_connect("localhost","root","YOURPASSWORD","YOURDATABASE");
if (mysqli_connect_errno())
{
echo"The Connection was not established" . mysqli_connect_error();
exit();
}
$get_term = "SELECT * FROM term WHERE Stdid='$id'";
$run_term = mysqli_query($db,$get_term);
$i =0 ;
while ($row=mysqli_fetch_array($run_term ))
{
// Get data from database.
// Change the input in $row[''] if the name of column is different in your tble "term".
$subject=$row['subject'];
$year1=$row['year1'];
$year2=$row['year2'];
$year3=$row['year3'];
$i++;
?>
<tr align="center">
<td> <?php echo $i;?></td>
<td><?php echo $subject;?></td>
<td><?php echo $year1;?></td>
<td><?php echo $year2;?></td>
<td><?php echo $year3;?></td>
</tr>
<?php }?>
</table>

Categories