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>
Related
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.
Here is my code to display table:
<table id="ws_table" class="ws_table">
<thead>
<tr class="bg-light">
<th>WS Code</th>
<th>WS Name</th>
</tr>
</thead>
<tbody class="reportWs_table">
<?php
if (is_array($workschedules)) {
//$i=0;
foreach ($workschedules as $workschedulesingle) {
$cnt++;
?>
<tr>
<td class="">
<?php echo $workschedulesingle['workScheduleCode']; ?>
</td>
<td class="">
<?php echo $workschedulesingle['workScheduleName']; ?>
</td>
</tr>
<?php
}
} else {
}
?>
</tbody>
</table>
My problem is its a long table. So I want to split into two. How can I split the table vertically? i.e. When the entries exceeds 20 it will goes to next table. How can I create it?
• Set Index $i=0
• Loop every row
• Check if the index start on 0
• Check if Index reach 20/limit
• If reach 20/limit reset Index to 0 again.
<?php
//INIT INDEX $i
$i = 0;
if(is_array($workschedules)){
// FOR EACH ROW
foreach($workschedules as $workschedulesingle){
//EVERY TIME THE INDEX START PRINT THE TABLE
if($i==0){
?>
<table id="ws_table" class="ws_table">
<thead>
<tr class="bg-light">
<th>WS Code</th>
<th>WS Name</th>
</tr>
</thead>
<tbody class="reportWs_table">
<?php } ?>
<tr>
<td class=""><?php echo $workschedulesingle['workScheduleCode'];?></td>
<td class=""><?php echo $workschedulesingle['workScheduleName'];?></td>
</tr>
<?php
//IF THE INDEX REACH 20/LIMIT, PRINT CLOSING TABLE TAGS
if($i==20){ ?>
</tbody>
</table>
<?php
}
//IF THE INDEX EXCEED 20/LIMIT, RESET $i TO 0 AND START AGAIN
$i++;
if($i>20){
$i=0;
}
}
}else{
}
?>
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
}
}
Needed Display
Here my code and display
<table width="100%" cellspacing="0" cellpadding="0" summary="" id="box-table-a">
<thead>
<tr>
<th width="" scope="col"><strong>Item Description</strong></th>
<th scope="col" style="text-align:center;"><strong>Quantity</strong></th>
<th width="350" scope="col"><strong>Supplier Name</strong></th>
<th scope="col" style="text-align:center;"><strong>Unit Price Rs.</strong></th>
<th scope="col" style="text-align:center;"><strong>VAT Price Rs.</strong></th>
<th width="100" scope="col"><strong>Total Price Rs.</strong></th>
</tr>
</thead>
<tbody>
<?php
$get_data =mysql_query("SELECT supplier_add_quotaion_form.quotaion_request_id,supplier_add_quotaion_form.supplier_id,supplier_add_quotaion_form.supplier_add_quotaion_id,supplier_add_quotaion_request_item.* FROM supplier_add_quotaion_request_item,supplier_add_quotaion_form
WHERE supplier_add_quotaion_form.supplier_add_quotaion_id=supplier_add_quotaion_request_item.supplier_add_quotaion_id AND supplier_add_quotaion_form.quotaion_request_id='$id' ORDER BY quotation_item_id");
while($row = mysql_fetch_array($get_data)){
$quotaion_request_id = $row['quotaion_request_id'];
$supplier_add_quotaion_id = $row['supplier_add_quotaion_id'];
$supplier_id = $row['supplier_id'];
$net_item_value = $row['net_item_value'];
$vat_item_value = $row['vat_item_value'];
$total_value = $row['total_value'];
$quotation_item_id = $row['quotation_item_id'];
$get_count = mysql_query("SELECT quotation_item_id FROM supplier_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
$count = mysql_num_rows($get_count);
$get_quantity = mysql_query("SELECT quantity_required,item_description FROM clerk_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
while($rowB = mysql_fetch_array($get_quantity)){
$quantity_required = $rowB['quantity_required'];
$item_description = $rowB['item_description'];
}
?>
<tr>
<td><?php echo $item_description; ?></td>
<td align="center"><?php echo $quantity_required; ?></td>
<td><?php echo $supplier_id; ?></td>
<td align="center"><?php echo $net_item_value; ?></td>
<td align="center"><?php echo $vat_item_value; ?></td>
<td align="center"><?php echo $total_value; ?></td>
</tr>
<?php
}
?>
</tbody>
When Duplicate description coming need to rowspan them or what ever method need to show display as first image..i try it get count and then rowspan according to it.but unable to get need display,don't know how to delete extra cell
Duplicate row count not fixed,only example show first image,it can be range 1-7 duplicate for one description(for one item 7 suppliers able to bids)
supplier_add_quotaion_form table structure
supplier_add_quotaion_request_item table structure
clerk_add_quotaion_request_item structure
You can do this in one pass,try this
<?php
$get_data =mysql_query("...");
$last_quotaion_request_id = -1;
while($row = mysql_fetch_array($get_data)){
$quotaion_request_id = $row['quotaion_request_id'];
$supplier_add_quotaion_id = $row['supplier_add_quotaion_id'];
$supplier_id = $row['supplier_id'];
$net_item_value = $row['net_item_value'];
$vat_item_value = $row['vat_item_value'];
$total_value = $row['total_value'];
$quotation_item_id = $row['quotation_item_id'];
$get_count = mysql_query("SELECT quotation_item_id FROM supplier_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
$count = mysql_num_rows($get_count);
$get_quantity = mysql_query("SELECT quantity_required,item_description FROM clerk_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
while($rowB = mysql_fetch_array($get_quantity)){
$quantity_required = $rowB['quantity_required'];
$item_description = $rowB['item_description'];
}
?>
<tr>
<?php if($last_quotaion_request_id != $quotaion_request_id){ ?>
<td rowspan="<?php echo $count; ?>"><?php echo $item_description; ?></td>
<td rowspan="<?php echo $count; ?>" align="center"><?php echo $quantity_required; ?></td>
<?php } ?>
<td><?php echo $supplier_id; ?></td>
<td align="center"><?php echo $net_item_value; ?></td>
<td align="center"><?php echo $vat_item_value; ?></td>
<td align="center"><?php echo $total_value; ?></td>
</tr>
<?php
$last_quotaion_request_id = $quotaion_request_id;
}
?>
It'll probably be FAR easier to slurp your query results into a structured array, from which you can then easily retrieve the necessary counts to do your rowspans:
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[$row['item_description']][] = $row;
}
echo "start your table here";
foreach($data as $description => $items) {
echo "<tr><td rowspan=" . count($items) . ">";
foreach($items as $item) {
output item data here
}
}
This won't work as is, but should give you an idea of how to go about it.
you need to change the structure of table you are using. as per my guessing I'm giving a suggestion. keep the item description in a table and the quotation details in another table. and while fetching the data you can fetch using group by sql command.
your item table may be like this:
1. id
2. desc
3. clicks
and quotation table may be like this
1. id
2. item_id
3. supplier
4. quantity
5. vat
6. price
etc.
I tried to allow the last td in this code to be beside the previous td, but I can't and the td is printed in a new line. How to allow them to be beside each other,the problem is that the 1st 5 td are in a foreach loop and the last td is not follow this foreach as it's value is a function and not a key or a value in the foreach.
<?php foreach($downloads as $dl) { ?>
<tr id="this">
<td ><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td>
<a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>">
<?=$dl['title']?>
</a>
</td>
<td>
<center>
<?=$dl['sname']?>
</center>
</td>
<td align="center"><?=$dl['views']?></td>
</tr>
<?php } ?>
<td align="center"><?=$core->use_love(); ?></td>
The function of the last td
public function use_love(){
$sql=mysql_query("select * from wcddl_downloads ORDER BY id DESC LIMIT ".$this->pg.",".$this->limit."");
while($row=mysql_fetch_array($sql))
{
$down_id=$row['id'];
$love=$row['love'];
?>
<div class="box" align="center">
<a href="#" class="love" id="<?php echo $down_id; ?>">
<span class="on_img" align="left"> <?php echo $love; ?> </span>
</a>
</div>
<?
}
}
The last <td> (the one outside the foreach loop) is on a new line because it's outside the last <tr> tag. One way to solve this is to always close the </tr> tag after the last <td>, like this:
<?php
$first_time = True;
foreach($downloads as $dl) {
// If this is the first time through the loop, don't echo a </tr> tag:
if ($first_time) {
$first_time = False;
} else {
echo "</tr>";
}
// Now print the new row, but don't close it yet:
?>
<tr id="this">
<td><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td>
<td><center><?=$dl['sname']?></center></td>
<td align="center"><?=$dl['views']?></td>
<?php
}
?>
<td align="center"><?=$core->use_love(); ?></td>
</tr>
This will always put the last <td> in the final row.
UPDATE: I just saw you added a diagram. This answer now makes no sense as the text description given earlier has nothing to do with what you want to achieve in the diagram.
I submit the modification to Steve Nay's answer as you need the same number of TD in all your TR. When you don't have the same count, you need to use colspan to achieve it. I've added a counter to check if it's the last time you loop. Here it goes:
<?php
$downloads_count = count($downloads);
$counter = 0;
foreach($downloads as $dl) :
$counter++;
// If this is the first time through the loop, don't echo a </tr> tag:
if ($counter > 1) {
echo "</tr>";
}
// Now print the new row, but don't close it yet:
?>
<tr id="this">
<td><img src="images/<?=$dl['type']?>.png"/></td>
<td id="no3"><?=$dl['type']?></td>
<td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td>
<td><center><?=$dl['sname']?></center></td>
<td align="center"<?php if ($downloads_count != $counter) echo ' colspan="2"'; ?>><?=$dl['views']?></td>
<?php endforeach; ?>
<td align="center"><?=$core->use_love(); ?></td>
</tr>