How can I join this table together, showing the header only once? I have tried to take th out of the while loop but I had no luck, maybe I made a mistake?
Here is my code so far:
<?php
function cart() {
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
<?php while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<img src="x.png"><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '£' . number_format($get_row['price'], 2);?></td>
<td><?echo '- ' .$value. ' +' ?> </td>
<td> <?echo '£ ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: £' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
} else {
header ('Location: index.php');
}
}
?>
Currently this code displays:
Change the script as follow. Hope it will work
$value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
if($i==0)
{
print("
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
</tr>
");
}
$i++;
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<img src="x.png"><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '£' . number_format($get_row['price'], 2);?></td>
<td><?echo '- ' .$value. ' +' ?> </td>
<td> <?echo '£ ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: £' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
} else {
header ('Location: index.php');
}
}
?>
Below is the corrected code, the creation of the table is moved outside of the loop.
Also the table header was added into a tr to create valid html, and also collected data into an $output variable to prevent a bug with your location redirect since data would already be written to the browser (you may vary well still have a problem with this since there is likely other output prior to this cart() function being called.
<?php
function cart() {
$output = '';
$output .= '<center>';
$output .= '<table class='menufinal' border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
$output .= '<tr>';
$output .= '<td><img src="x.png"><br></td>';
$output .= '<td>' . $get_row['name'] . '</td>';
$output .= '<td>£ ' . number_format($get_row['price'], 2) . '</td>';
$output .= '<td>- ' .$value. ' +</td>';
$output .= '<td>£ ' . number_format($sub, 2) . '</td>';
$output .= '</tr>';
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
$output .= '</table>';
if (empty($total)){
header ('Location: index.php');
exit;
}
$output .= '<br>Total: £' . number_format($total, 2) . '<br>';
$output .= '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
echo $output;
}
?>
Related
I wanna do if in by my status field.
if my status is "blocked" so the font background color will be in red and if the status is "authorized" so in green.
how should i do that?
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
<td>'.$row->status.'</td>
</tr>
';
}
Try this
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
#if($row->status == "blocked")
<td> <button type="button" class="btn btn-danger">blocked</button> </td>
#elseif($row->status == "authorized")
<td> <button type="button" class="btn btn-success">authorized</button> </td>
#endif
</tr>
OR
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
You shouldn't really be dealing with view logic in your controller.
But to do what you want in the controller
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
However what you should do is handle it in your blade file. Also you probably want to create styles in your style sheet and apply classes rather than inline the css.
this is the code that worked
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
I am using mysql queries to fetch data from db. All my data are showing fine in tables. Now I want to color the status by value less than 2 or more than 3.
The below code is not working.
Need help.
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
?>
<?php
function status_style($row) {
if ($row < 2) return 'background-color: #ff0000'; //red
if ($row > 3) return 'background-color: #33cc33'; //green
return '';
}
?>
<?php
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
?>
You have already created function to color the rows but haven't called the function during showing output. That's why it is not working.
Change your $output variable with:
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td style="'.status_style($row["status"]).'">'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
You can try something like this:
<?php
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
function status_style($row) {
if ($row < 2) return $color = '#ff0000'; //red
if ($row > 3) return $color = '#33cc33'; //green
return $color = '';
}
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr style='"'background-color: '"' . $color . '"'>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
You didn't do anything with the background color.
I'm quite new to html and php, I've tried a few things trying to achieve what i want but it never works.
It's a shopping cart, I'm trying to display data from mysql using php in a table, it would be simple if I didn't have other elements that I want to display in the table as well ($value and $sub) which aren't stored in mysql but are calculations.
When I don't try to display it in a table it all works perfectly, and when I add the table I can only get the 'name' to display in the table, If I try to include anything else it fails.
Code below displays 'name' under the table name column, next thing to display would be '$value' under quantity and so on (haven't added anymore columns yet)
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<tr>
<?php echo '<td>' .$get_row['name']. '</td>' .$value. ' # $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' [-] [+] <a href="cart.php?delete='.$id.'" >[Delete]</a><br />';
}
}
$total += $sub;
}
}
?>
</tr>
</table>
Possible code to render column and row properly.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<?php echo '<td>' .$get_row['name']. '</td>' .$value. ' # $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' [-] [+] <a href="cart.php?delete='.$id.'" >[Delete]</a><br />';
}
}
$total += $sub;
}
}
?>
</tr>
</table>
Thanks
Amit
First thing i want to tell you is mysql_* is officially deprecated as of PHP 5.5. And removed entirely as of PHP 7.0. So please dont use it.
Refer : http://php.net/manual/en/migration55.deprecated.php
For mysqli_* : http://php.net/manual/en/book.mysqli.php
Now about you code :
You are using table tag inside the loop. This will generate multiple tables. Try out this code :
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query("SELECT id_product, name, price FROM elec_guit WHERE id_product='$id'");
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo '<tr><td>' . $get_row['name']. '</td>' .
'<td>' . $value. ' # $'.number_format($get_row['price'], 2).
' = $'.number_format($sub, 2).' [-] [+]'
'<a href="cart.php?delete='.$id.'" >[Delete]</a></td></tr>';
}
}
$total += $sub;
}
}
?>
</table>
Added new code as per your comment.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<?php echo '<td>' .$get_row['name']. $value. ' # $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' [-] [+] <a href="cart.php?delete='.$id.'" >[Delete]</a><br /></td> </tr>';
}
}
$total += $sub;
}
}
?>
</table>
I moved the table outside of your loop, that will fix a majority of your problems. Another issue is you only had one <td> wrapping the name column in your while loop, but you didn't have a <td> wrapping the other information in that row, so I wrapped that.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo '<tr><td>' . $get_row['name']. '</td>' .
'<td>' . $value. ' # $'.number_format($get_row['price'], 2).
' = $'.number_format($sub, 2).' [-] [+]'
'<a href="cart.php?delete='.$id.'" >[Delete]</a></td></tr>';
}
}
}
}
?>
</table>
</div>
Use the below code:
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<td><?php echo $get_row['name']; ?></td>
<td><?php echo $value;?> # $<?php echo number_format($get_row['price'], 2); ?> = $<?php echo number_format($sub, 2); ?> [-] [+] <a href="cart.php?delete=<?php echo $id; ?>" >[Delete]</a><br /></td>
</tr>
<?php }
}
$total += $sub;
}
}
?>
</table>
</div>
I know this has been asked before and I have got it working using the following code:
<?php
$maxcols = 8; $i = 0;
echo "<table id='table1'><tr>";
foreach ($id as $k => $v) {
echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
if ($i == $maxcols) { $i = 0; echo "</tr><tr>"; }
} $i++;
while ($i <= $maxcols) {
$i++; echo "<td></td>";
}
echo "</tr></table>";
?>
This results in a table that looks like this :
I'd like to add headers to this so the end result looks like this:
I'd like to do it dynamically so if I create a table that is only 5 columns wide I'd get on the first header row ID01 - ID05 and on the second header row ID06 - ID10
I want to limit the header ID values to be no more than $maxid any extra header fields should be blank, like this : If $maxid = 12; then :
I need the header rows are made as follows and not using <TH>
<td class="mark">
I'm using some javascript to allow the movement of cell data.
The class is used to set the formatting on the header and stop items from being dragged into the fields.
Can anyone point me in the right direction on how to do this.
This should help you.
$maxcols = 8;
$maxid = 12;
$startid = 1;
echo "<table id='table1'>\n";
for ($i = 1;$i<=ceil($maxid/$maxcols);$i++) {
echo "<tr>\n";
for ($j=1;$j<=$maxcols;$j++)
if ($startid <= $maxid)
echo " <td class='mark'>ID".$startid++."</td>\n";
else
echo " <td> </td>\n";
echo "</tr>\n<tr>\n";
for ($j=1;$j<=$maxcols;$j++)
echo "<td>Content</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
Generates
<table id='table1'>
<tr>
<td class='mark'>ID1</td>
<td class='mark'>ID2</td>
<td class='mark'>ID3</td>
<td class='mark'>ID4</td>
<td class='mark'>ID5</td>
<td class='mark'>ID6</td>
<td class='mark'>ID7</td>
<td class='mark'>ID8</td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
<tr>
<td class='mark'>ID9</td>
<td class='mark'>ID10</td>
<td class='mark'>ID11</td>
<td class='mark'>ID12</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
<td>Content</td>
</tr>
</table>
try this. It will work in the same way as you desired
<?php
$id= array("1","2","3","4","5","6","7","8","9","10","11","12");
$maxcols = 8; $i = 0;$j=0;$t=0;$s=0;
$maxid = count($id);
echo "<table id='table1'><tr>";
foreach ($id as $k => $v)
{
if($t == 0)
{
while ($t <= $maxcols-1) {
if($s < $maxid)
{
$s++;$t++; echo "<td class='mark'>id$s</td>";
}
else
{
echo "<td class='mark'></td>";$t++;$s++;
}
}
echo "</tr><tr>";
}
else
{
}
echo "<td id='0'><div id='{$k}' class='drag t1'>{$v}</div></td>"; $i++;
if ($i == $maxcols)
{
echo "</tr><tr>";
if($j == 0)
{
while ($j <= $maxcols-1) {
if($s < $maxid)
{
$s++;$j++; echo "<td class='mark'>id$s</td>";
}
else
{
echo "<td class='mark'></td>";$j++;$s++;
}
}
echo "</tr><tr>";
}
$i=0;
}
}
echo "</tr></table>";
?>
Output
Hi You can Use My Library:
class generate{
private $row = "<tr>{columns}</tr>";
private $td = "<td {attr}>{data}</td>";
private $attributeTR="";
private $attributeTD="";
private $tdBuilder="";
public function addCol($ColumValsArr=array("class='motota'"=>"Example")){
foreach($ColumValsArr as $key=>$val){
$newCol = str_replace("{data}",$val,$this->td);
$newCol = str_replace("{attr}",$key,$newCol);
$this->tdBuilder .= str_replace("{data}",$key,$newCol);
}
}
public function getRow(){
return str_replace("{columns}",$this->tdBuilder,$this->row);
}
}
<?php
function TableFunc($Data)
{
$Table = "<table>" . PHP_EOL;
foreach ($Data as $tags => $array) {
$Table .= "<$tags>" . PHP_EOL;
foreach ($array as $thead) {
$tag=$tags==="tbody"?"td":"th";
$Table .= "<tr>" . PHP_EOL;
if (is_array($thead)) {
foreach ($thead as $theadItem) {
if (is_array($theadItem))
$Table .= "<$tag colspan='$theadItem[1]'>$theadItem[0]</$tag>" . PHP_EOL;
else
$Table .= "<$tag>$theadItem</$tag>" . PHP_EOL;
}
}
$Table .= "</tr>" . PHP_EOL;
}
$Table .= "</$tags>" . PHP_EOL;
}
$Table .= "</table>" . PHP_EOL;
return $Table;
}
$Data = array(
"thead" => [
[["GENEL BİLGİ", 2], ["KALORİMETRE (ISINMA)", 2], ["HESAPLAMA", 2]],
["NO", "AD SOYAD", "FARK", "TUTAR", "OKUMA", "ÖDENECEK"]
],
"tbody"=>array(
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
array("1","MURAT DURAN","100","100.00","10.00","110.00"),
),
"tfoot" => [["NO", "AD SOYAD", "M2", "MAHSUP", "SAYAÇ", "15°", "FARK", "TUTAR", "ORTAK ALAN", "EKSTRA", "MUAFİYET", "OKUMA", "ÖDENECEK"]]
);
echo TableFunc($Data);
I have 2 small questions I need help on which deals with the code below:
echo "<table border='1' id='markstbl'>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='noofmarksth'>Total Marks</th>
</tr>\n";
$previous_question_id = null;
foreach ($searchQuestionContent as $key=>$question) {
if ($previous_question_id == $searchQuestionId[$key]) {
$searchQuestionId[$key] = '';
$question = '';
$searchMarks[$key] = '';
}else{
$previous_question_id = $searchQuestionId[$key];
}
echo '<tr class="questiontd">'.PHP_EOL;
echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td class="answertd">';
echo $searchAnswer[$key];
echo '</td>' ;
echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL;
echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
}
echo '</tr>';
echo "</table>" . PHP_EOL;
Question 1: If you look at the table above, I have a a column where it will contain text inputs. Now each text input will belong to each answer in the "Answer" column. My question is that is the text input set up correctly with me including a [] in the name attribute to set up an array? Should I include [] and is there anything else I should include in the text input code?
Question 2: At the moment the code above makes the table look like this example in JSFIDDLE1. But I want the table to be displayed as this example JSFIDDLE2. As you can see the blank rows in the other table have been rowspan so that it looks like the question and the total marks looks like its in one cell. How can I get the table above to look like JSFIDDLE2?
UPDATE:
Below is the full code, I am still receiving undefined index whenver $rowspans[.... is used. Is the code below correct:
$assessment = $_SESSION['id'] . $sessionConcat;
$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, an.Answer, q.QuestionMarks
FROM Session s
INNER JOIN Question q ON s.SessionId = q.SessionId
JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s", $assessment);
// execute query
$stmt->execute();
// This will hold the search results
$searchQuestionId = array();
$searchQuestionContent = array();
$searchAnswer = array();
$searchMarks = array();
// Fetch the results into an array
// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionContent, $dbAnswer, $dbQuestionMarks);
while ($stmt->fetch()) {
$searchQuestionId[] = $dbQuestionId;
$searchQuestionContent[] = $dbQuestionContent;
$searchAnswer[] = $dbAnswer;
$searchMarks[] = $dbQuestionMarks;
}
?>
</head>
<body>
<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<?php
echo "<table border='1' id='markstbl'>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='noofmarksth'>Total Marks</th>
</tr>\n";
$previous_question_id = null;
$rowspans = array_count_values($searchQuestionId);
foreach ($searchQuestionContent as $key=>$question) {
if ($previous_question_id == $searchQuestionId[$key]) {
$searchQuestionId[$key] = '';
$question = '';
$searchMarks[$key] = '';
}else{
$previous_question_id = $searchQuestionId[$key];
}
echo '<tr class="questiontd">'.PHP_EOL;
echo '<td class="optiontypetd">'.htmlspecialchars($rowspans[$searchQuestionId[$key]]).'</td>' . PHP_EOL;
echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td class="answertd">';
echo $searchAnswer[$key];
echo '</td>'; echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
}
echo '</tr>';
echo "</table>" . PHP_EOL;
?>
<p><input id="submitBtn" name="submitMarks" type="submit" value="Submit Marks" /></p>
</form>
Q1. Yes, you could even specify a name between the [].
Q2. It seems that $searchQuestionId is filled before your loop, right? In that case you could use array_count_values to store the frequency of each ID and access that array during the loop:
before your loop: $rowspans = array_count_values($searchQuestionId);
access rowspan value during loop with: $rowspans[$searchQuestionId[$key]].
You'll have to remove the line $searchQuestionId[$key] = ''; or else you won't be able to access the correct $rowspan key.
Finally, you'll have to set up some more if statements to skip the <td>s on certain rows.
$previous_question_id = null;
$rowspans = array_count_values($searchQuestionId);
foreach ($searchQuestionContent as $key=>$question) {
// removed logic, not necessary to set empty strings if you're skipping them
echo '<tr class="questiontd">'.PHP_EOL;
if ($previous_question_id != $searchQuestionId[$key]) {
echo '<td class="optiontypetd" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
echo '<td rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($question).'</td>' . PHP_EOL;
}
echo '<td class="answertd">';
echo $searchAnswer[$key];
echo '</td>' ;
echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL;
if ($previous_question_id != $searchQuestionId[$key]) {
echo '<td class="noofmarkstd" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
}
// moved this to the end
if ($previous_question_id != $searchQuestionId[$key]) {
$previous_question_id = $searchQuestionId[$key];
}
}