For loop leaves blank td in a table - php

I am calling a for loop to get data from database with this code which is working fine but only with one problem
<table class="table" style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<tr>
</<?php
for ($x = 0; $x <= 30; $x++) {
?> <td><?= $info['ans_' . $x]; ?></td>
</tr>
<tr>
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz); ?>
<td><?= $sales_que['que']; ?></td>
<?php }; ?>
</tr>
</tbody>
</table>
The result is first td of 2nd data which is que is blank and for loop start counting it from 2nd row
I do create a separate for loop for question and answer but then the table becomes broken.

If you need new rows in a table for every loop then you need to add the tr within the loop.
Also all tds that need to be on the same row has to be within one tr.
So remove the extra <tr> and bring them inside the loop.
<table class="table"
style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<?php
for ($x = 0; $x <= 30; $x++) {
?>
<tr>
<td><?= $info['ans_' . $x]; ?></td>
<!-- </tr> Remove these-->
<!-- <tr>-->
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz);
?>
<td><?= $sales_que['que']; ?></td>
</tr>
<?php
} # the semi-colon (;) at the end is unnecessary
?>
</tbody>
</table>

You need to move your </tr><tr> to after the last td, and probably swap your $info['ans_'.$x]; and $sales_que['que']; since it seems like it should go question then answer, and the variable names make me think you are doing answer then question.
<table class="table" style="color:#000 !important;">
<tbody>
<tr>
<th>Question</th>
<th>User Answer</th>
</tr>
<tr>
<?php
for ($x = 0; $x <= 30; $x++) {
?> <td><?= $sales_que['que']; ?></td>
<?php
$sales_quiz = mysqli_query($conn_quiz, "SELECT * FROM `sales_quiz` where `que_no` = '$x'");
$sales_que = mysqli_fetch_array($sales_quiz); ?>
<td><?= $info['ans_'.$x]; ?></td>
</tr>
<tr>
<?php }; ?>
</tr>
</tbody>
</table>

I think it might be the "</" in the 10th line of your code, before the first <?php . There is any reason for that "</" to be there? From my perspective that creates a empty <tr></tr> on the resultant html file, and thats why you have that empty cell on your table. I might be wrong, just check it out and tell me if it works.

Related

MySQL not fetching all results

Trying to fetch all results in PHP from MySQL database, but it is leaving the first query.
My MySQL table is in the below image: .
My Code:
<?php
$irn = "33857";
$stmt = $user_home->runQuery('SELECT * FROM invoice WHERE Inv = :inv ORDER BY Sr ASC ');
$stmt->bindParam(':inv',$irn);
$stmt->execute();
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
?>
<table id="chiru_inv" class="table table-striped table-hover table-bordered table-responsive">
<tr>
<td colspan="4" align="center">
<h1>Company</br><span style="font-size: 75%;">Number</span></h1>
</td>
</tr>
<tr>
<td colspan="2">
<h3><span style="float: left;"><?php echo $rowc['Customer']; ?> (<?php echo $rowc['Inv']; ?>)</span></h3>
</td>
<td colspan="2">
<h3><span style="float: right;"><?php echo $rowc['Date']; ?></span></h3>
</td>
</tr>
<tr>
<th>Sr.</th>
<th>Item</th>
<th>Qty</th>
<th>Amount</th>
</tr>
<?php
$i = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $Item; ?></td>
<td>5</td>
<td>200</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"><strong><?php echo getIndianCurrency(225); ?>Only</strong></td>
<td><strong>Total:</strong></td>
<td><strong>225</strong></td>
</tr>
</table>
<?php
}
?>
The result when I execute the above code is in the below image:
There are three queries with invoice number 33857, but only two are displayed (leaving the first one)!
I need all to be displayed as per invoice number.
Please help me from sorting out the error/code I made or I left.
The problem in your code is with the line:
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
in the beginning of your code you're fetching the first row.
You have to update your while loop, like this
<?php
$i = 0;
while($rowc)
{
extract($rowc);
$i++;
?>
<!-- Your HTML/PHP code for the table -->
<?php
$rowc=$stmt->fetch(PDO::FETCH_ASSOC);
}
?>
So, the idea is to fetch the row at the end of the while loop, in order to not lose your first row.

PHP HTML table retrieve multiple data in one <td> tag

I'm trying to generate a list from database in a HTML table just like image below;
https://i.stack.imgur.com/61XLl.png
And here's what i did;
https://i.stack.imgur.com/lLsvF.png
And the code;
<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
<thead>
<tr>
<th>KURSUS</th>
<th rowspan="2">NAMA PENSYARAH</th>
<th rowspan="2">NO. SIRI</th>
</tr>
<tr>
<th>NAMA</th>
</tr>
</thead>
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();?>
<td><?php echo $rowname['user_name']; ?></td>
<td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
Help me. Thank you in advance :)
Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php
$user_names = $codes = ''; // define empty variables
while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();
$user_names .= $rowname['user_name']."<br/>"; //concat to a single string
$codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
}?>
<td><?php echo $user_names;?></td>
<td><?php echo $codes;?></td>
</tr>
<?php
}
}
?>
</tbody>
Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){
Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.

Get the total of variables in active records

Ok, I have a variable called $total_bal that is the answer of a simple equation made from two queries stored in variables x and y
for example
$y = $row->amount_one;
$z = $row->amount_two;
$total_bal = $z + $y;
However I have many entries in amount_one and amount_two.
As I am using codeigniters active records
I tried
echo $this->db->count_all($total_bal);
But this dose not work, any idea of the best way to do this ?
So Im after a way to add all the $total_balup, for more incite into my code see bellow.
<?php
if (isset($records)) : foreach ($records as $row) :
$x = $row->amount_two;
$y = $row->ammount_one;
$total_bal = $z + $y;
?>
<table>
<tbody>
<tr>
<td>amount one</td>
<td>amount two</td>
</tr>
<tr>
<td>
<?php echo $x;?>
</td>
<td>
<?php echo $y;?>
</td>
<td>
<?php echo $$total_bal;?>
</td>
</tr>
<!-- <tr>-->
<!-- <td>-->
<!-- --><?php //echo $this->db->count_all('$total_bal'); ?>
<!-- </td>-->
<!-- </tr>-->
</tbody>
</table>
<?php endforeach; ?>
<?php else : ?>
<h3>You Have No Accounts</h3>
<h4>Why No Add A Account?</h4>
<?php endif; ?>
One way to do this is to use an accumulator variable. Take for example, a $grandTotal variable. You set it to 0 outside the foreach, over each iteration of the loop, you add the $rowTotal to the $grandTotal. Eventually when the loop ends, you have a total value of all row totals.
The benefit to this method is that it doesn't require any additional calls to the database and since you are already looping through the values to display them, accumulating them is minimal processing.
<?php if (isset($records)) : ?>
<table>
<thead>
<tr>
<th>Amount One</th>
<th>Amount Two</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php $grandTotal = 0; ?>
<?php foreach ($records as $row) : ?>
<?php
// Add field values to get row total
$rowTotal = $row->amount_one + $row->amount_two;
?>
<?php
// Add row total to grand total
$grandTotal += $rowTotal;
?>
<tr>
<td>
<?php echo $row->amount_one;?>
</td>
<td>
<?php echo $row->amount_two;?>
</td>
<td>
<?php echo $rowTotal;?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td></td>
<td></td>
<td><?php $grandTotal; ?></td>
</tr>
</tbody>
</table>
<?php else : ?>
<h3>You Have No Account</h3>
<h4>Why Not Add An Account?</h4>
<?php endif;?>

While Loop For Table PHP

I'm trying to do this in while loop PHP, number of assignments are unlimited so rowspan should also adjust itself with the number of rows, is there any proper way to do it with minimum numbers of line?
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Assignment No</th>
<th>Student Name</th>
<th>Assignment Marks</th>
<th>Overall Result</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">S1</td>
<td align="center">5</td>
<td rowspan="3" align="center">B Grade</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">S1</td>
<td align="center">8</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">S1</td>
<td align="center">7</td>
</tr>
</tbody>
</table>
Try This:
<? foreach($dataarray as $data)
{?>
<tbody>
<tr>
<td align="center"><? echo $data[0]; ?></td>
<td align="center"><? echo $data[1]; ?></td>
<td align="center"><? echo $data[2]; ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="3" align="center"><? echo $data[3]; ?></td>
<? } ?>
<? } ?>
and put if condition for last column
Assuming that data comes in a linear fashion (ie a plain one dimensional array), and that the grade is not in the array, I'd do something like this:
<?
$sizeofArray = count($data);
$rowspan = floor($sizeofArray/3);
for($arrCnt = 0; $arrCnt < $sizeofArray; $arrCnt +=3)
{?>
<tbody>
<tr>
<td align="center"><?=$data[$arrCnt]; ?></td>
<td align="center"><?=(($arrCnt+1 < $sizeofArray)?$data[$arrCnt+1]:" "); ?></td>
<td align="center"><?=(($arrCnt+2 < $sizeofArray)?$data[$arrCnt+2]:" "); ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="<?=$rowspan?>" align="center"><?=$grade; ?></td>
<? } ?>
<? } ?>
EDIT: added "padding" code, to avoid index out of bounds error
Based on your case, rowspan is different from one student to another. What you would want to do, is group data by student. The number of assignments per student is what will give you rowspan value.
While ... {
$data['student'][] = ['name' => $row -> name]; // all student inf
$data['student][]['assignments'][] = ['id' => $row -> assignement_id];//student assigments
}
Hope this help or put in the right direction to resolve your problem.
use foreach to loop
foreach ($data as $student){
$rowspan = count($student['assignments'];
foreach ($student['assigments'] as $assignment ){
// html table
}
}

Create a Counter in a ranking table

I have a table of articles that are arranged by the number of visits, where the most visited one is on top, i.e it has Rank of 1. I tried adding a Rank column in the table, but can't get it to work. The Rank column does not exist in the Database, so this should be an incremental counter that starts with 1 and ends with the number of articles listed in the table.
Here's my code, which does not work
<table class="resultsTable" width="100%">
<tr>
<th style="width:10%">Rank</th>
<th style="width:50%">Post Title</th>
<th style="width:20%">Post Type</th>
<th style="width:20%">Hits</th>
</tr>
<?php foreach($popular_articles as $article) {
//create array for most popular
?>
//set counter variable
$counter = 1;
while($row = mysql_fetch_array($result))
<tr>
echo "<td style="width:10%">".$counter."</td>";
<td style="width:50%"><?php echo $article->art_title; ?></td>
<td style="width:20%"><?php echo $article->art_type; ?></td>
<td style="width:20%"><?php if($article->art_hit_count){ echo '<b>'.$article->art_hit_count.'</b>'; }else { echo '<b>0</b>'; } ?> times.</td>
</tr>
$counter++; //increment counter by 1 on every pass
echo <?php } ?>
</table>
Increasing the counter must be done in PHP. Currently the line that does that is in the HTML:
$counter++; //increment counter by 1 on every pass
I also removed the while, since it didn't seem to do anything, as if it was accidentally pasted there.
The loop should look like this:
<?php
foreach($popular_articles as $article) {
//set counter variable
$counter = 1;
<tr>
<td style="width:10%"><?php echo $counter; ?></td>";
<td style="width:50%"><?php echo $article->art_title; ?></td>
<td style="width:20%"><?php echo $article->art_type; ?></td>
<td style="width:20%"><?php
if($article->art_hit_count){
echo '<b>'.$article->art_hit_count.'</b>';
}else {
echo '<b>0</b>';
} ?> times.</td>
</tr>
<?php
$counter++; //increment counter by 1 on every pass
} ?>
Alternatively, you can display the counter and update it in one go:
<td style="width:10%"><?php echo $counter++; ?></td>";
...
<?php
// $counter++; // You won't be needing this anymore.
} ?>
I think you missed to initialize your counter before the loop (in the commentaries, I can see a $counter = 1, but it's trully executed.
And at the end, you try to increment a variable but you're not in a PHP script, so it can not work. Try my corrected version to see if it work.
<table class="resultsTable" width="100%">
<tr>
<th style="width:10%">Rank</th>
<th style="width:50%">Post Title</th>
<th style="width:20%">Post Type</th>
<th style="width:20%">Hits</th>
</tr>
<?php
$counter = 1;
foreach($popular_articles as $article) {
//create array for most popular
?>
<tr>
<td style="width:10%"> <?php echo $counter; ?></td>;
<td style="width:50%"> <?php echo $article->art_title; ?></td>
<td style="width:20%"> <?php echo $article->art_type; ?></td>
<td style="width:20%">
<?php if($article->art_hit_count){
echo '<b>'.$article->art_hit_count.'</b>';
} else {
echo '<b>0</b>';
} ?> times. </td>
</tr>
<?php
$counter++; //increment counter by 1 on every pass
} //the end of FOR loop ?>
</table>
EDIT
If you want to make a link for the art_title. Just replace this line :
<td style="width:50%"> <?php echo $article->art_title; ?></td>
By this one :
<td style="width:50%"> <?php echo $article->art_title; ?></td>

Categories