Bold the last row in table - php

How can I make the last row Bold in fpdf?
here is my code
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
$this->Ln();
}
}
Updated Code : Added conditions for the rows until reach the last row.
foreach ($data as $row) {
$cnt = 0;
if ($currentRow === $totalRows) {
$this->SetFont('Arial','B');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
} else {
$this->SetFont('Arial');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
}
$currentRow++;
$this->Ln();
}
The sample code above creates the row and column my target is to make the last row bold

Simply, you can count() rows and then check if current processed row is the last one.
$totalRows = count($data);
$currentRow = 1;
foreach ($data as $row) {
// (...)
if ($currentRow === $totalRows) {
// this is the last row
}
$currentRow++;
}
Here is the working code.
foreach ($data as $row) {
$cnt = 0;
if ($currentRow === $totalRows) {
$this->SetFont('Arial','B');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
} else {
$this->SetFont('Arial');
foreach ($row as $col) {
if($cnt < 1){
$this->Cell(9,5,$col,1,0,'C');
} elseif ($cnt < 2) {
$this->Cell(18,5,$col,1,0,'C');
} else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
}
$currentRow++;
$this->Ln();
}

Related

How can I divide the foreach result for 2 div?

Here's the thing: I have a foreach loop that adds inputs dynamically. I need it to place part of them in one div, the rest in another. The current code is the following:
$sqla=mysql_fetch_row($sql);
$x=1;
if($x<50)
{
?>
<div class="area area-1">
<?
foreach($sqla as $key=>$values){
if ($key == "0") {
continue;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($key == "50") {
break;
}
$x++;
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
?>
</div>
<?
}else{
?>
<div class="zodiak ar-1">
<?
foreach($sqla as $key=>$values){
if ($key == "50") {
continue;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
echo "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($key == "62") {
break;
}
$x++;
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
?>
</div>
<?
}
?>
The output puts it all in the first div, but none in the "zodiak ar-1" one. The target thing is everything after the 50-th key to go into that div. Hope that managed to explain the issue...
Thank you
Right now you are doing this:
$x=1;
if($x<50)
{
// your code
} else {
// your code
}
The problem is that you do a foreach INSIDE the if statement, so $x < 50 will ALWAYS be true because just before you do $x = 1.
Now in both foreach loop you do this :
foreach($sqla as $key=>$values){
if ($key == "0") {
continue;
}
// your code
}
foreach($sqla as $key=>$values){
if ($key == "50") {
continue;
}
// your code
}
So you use a var $x that you increment each turn but you have a $key that you use too to check if value is <50 or not?
So try something like this :
$new_rand_arr = array();
$open_first_div = false;
$open_second_div = false;
$html = "";
foreach($sqla as $key=>$values){
if ($key < "50") {
// You open your first div one time
if (!$open_first_div) {
$html .= "<div class=\"area area-1\">";
$open_first_div = true;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($values > 0) {
$new_rand_arr[] = $values;
}
} else {
// You close your first div and open the second div
if (!$open_second_div) {
$html .= "</div><div class=\"zodiak ar-1\">";
$open_second_div = true;
}
$icheck = ($values > 0) ? "icheck" : "";
$ichecked = ($values > 0) ? "isChecked" : "";
$html .= "<label class='label-area label-".$values['num".$x."'][$x]." ".$ichecked."'><input name='data[ar][".$x."][]' type='checkbox' value='".$x."' title='".$x."' class='".$icheck." archeck1'><span class='label-num'>".$x."</span><span class='label-check-mark'></span></label>";
if ($values > 0) {
$new_rand_arr[] = $values;
}
}
}
// After the foreach your close your div
$html .= "</div>";
// You display it
echo $html;

foreach loop breaks before reaching the limit

I've been trying to list 9 tables for each student using the following code -
Up to 8 records are getting successfully printed. But for the next value of $s, the next student gets listed.
Where am I getting it wrong?
$query="SELECT * FROM `member_db` WHERE Class='$class' ORDER BY `Member_name` ASC";
$Result = mysqli_query($conn, $query);
$s=1;
while($row=mysqli_fetch_array($Result)) {
foreach($row as $field) {
if($s==10){
$s=1; break;
} else if($s==1){
$sub="English (WS VOL-1)";
} else if($s==2){
$sub="English (WS VOL-2)";
} else if($s==3){
$sub="English (WS VOL-3)";
} else if($s==4){
$sub="Maths (WS VOL-1)";
} else if($s==5){
$sub="Maths (WS VOL-2)";
} else if($s==6){
$sub="Maths (WS VOL-3)";
} else if($s==7){
$sub="Science (WS VOL-1)";
} else if($s==8){
$sub="Science (WS VOL-2)";
} else if($s==9){
$sub="Science (WS VOL-3)";
}
echo $table;
$s++;
}
}
try this : $i++ should be after end inner loop.
<?php
$query = "SELECT * FROM `member_db` WHERE Class='$class' ORDER BY `Member_name` ASC";
$Result = mysqli_query($conn, $query);
$s = 1;
while ($row = mysqli_fetch_array($Result)) {
foreach ($row as $field) {
if ($s == 10) {
$s = 1;
break;
}
if ($s == 1) {
$sub = "English (WS VOL-1)";
} elseif ($s == 2) {
$sub = "English (WS VOL-2)";
} elseif ($s == 3) {
$sub = "English (WS VOL-3)";
} elseif ($s == 4) {
$sub = "Maths (WS VOL-1)";
} elseif ($s == 5) {
$sub = "Maths (WS VOL-2)";
} elseif ($s == 6) {
$sub = "Maths (WS VOL-3)";
} elseif ($s == 7) {
$sub = "Science (WS VOL-1)";
} elseif ($s == 8) {
$sub = "Science (WS VOL-2)";
} elseif ($s == 9) {
$sub = "Science (WS VOL-3)";
}
echo $table;
}
$s++; //this is only change
}

How to align right all columns except the 1st 2?

I have a table that has 27 columns and I am using fpdf to create pdf file.
I wonder how can I make all the columns align right except the 1st 2?
Here is my code.
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
foreach ($row as $col)
#$this->Cell(18,5,$col,1,'R');
$this->Cell(18,5, $col, 1, 0);
$this->Ln();
}
}
}
Update Code (Working)
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 2){
$this->Cell(18,5,$col,1);
}
else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
$this->Ln();
}
}
}
You should check col value on each row,
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 2){
$this->Cell(18,5,$col,1,'R');
}
else {
$this->Cell(18,5, $col, 1, 0);
}
$cnt++;
}
$this->Ln();
}
}
I also found extra "}" in your function.
Updated code based on post above
#Create the table
function BasicTable($header,$data) {
#Create the header.
foreach ($header as $col)
$this->Cell(18,5,$col,1);
$this->Ln();
#Get the data
foreach ($data as $row) {
$cnt = 0;
foreach ($row as $col) {
if($cnt < 2){
$this->Cell(18,5,$col,1);
}
else {
$this->Cell(18,5, $col, 1, 0,'R');
}
$cnt++;
}
$this->Ln();
}
}
}

In PHPExcel i merger three cell and having foreach loop. Result next value not printed after merge

$periodOne=array(array('SNO','Appraisal','TOT','AVG','CLS TOT','CLS AVG','DIFF'));
$rowID = 7;
foreach($periodOne as $rowArray) {
$columnID = 'A';
foreach($rowArray as $columnValue) {
$this->setActiveSheetIndex(0)->mergeCells('B7:D7');
$this->getActiveSheet()->setCellValue($columnID.$rowID,$columnValue);
$columnID++;
}
$rowID++;
}
$periodOne=array(array('SNO','Appraisal','TOT','AVG','CLS TOT','CLS AVG','DIFF'));
$rowID = 7;
foreach($periodOne as $rowArray) {
$columnID = 'A';
foreach($rowArray as $columnValue) {
if($columnID.$rowID == 'C7')
{
$columnID = 'E';
$this->getActiveSheet()->setCellValue($columnID.$rowID,$columnValue);
}
else
{
$this->getActiveSheet()->setCellValue($columnID.$rowID,$columnValue);
}
$columnID++;
}
$rowID++;
}

How to insert coordinates of a table into an array in PHP

I am making the game battleships in PHP and I need to make it so that the computer randomly chooses 5 coordinates(x, y), and they have to be in a straight line (column or row). How do I make it so the coordinates are in a line and how do I connect the coordinates to the table cells?
Here's what I have so far:
<?php
session_start();
$i=0;
$start=1;
$st=65;
$l=0;
$polje=array();
$x=(mt_rand(0, 10));
$y=(mt_rand(0, 10));
for($q=0; $q<5; $q++){
$polje[$l]=array($x, $y);
}
echo "<table id='table'>\n";
while ($i<11){
$ascii=chr($st);
echo "<tr>";
for($k=0; $k<11; $k++, $start++){
if($k==0){
echo "<td>$i</td>";
}
else if($i==0){
echo "<td class='td2'>$ascii</td>";
$ascii++;
}
else{
echo "<td class='td1'> </td>";
}
}
echo "</tr>";
$i++;
}
?>
This is what the table looks like:
I like this kind of challenge.
Of course, there is a lot of ways, better ways to do this, with OOP, but you can get the point.
Read the comments through the code and if you have any doubts feel free to ask.
I did some treatment to avoid ships overlapping each other, and random deployment.
const BOARD_SIZE = 10; //10x10 board
const SUBMARINE = 1;
const DESTROYER = 2;
const BATTLESHIP = 3;
const AIRCRAFTCARRIER = 4;
$ships_available = [];
//populate with some data based on game rule
array_push($ships_available, SUBMARINE);
array_push($ships_available, SUBMARINE);
array_push($ships_available, DESTROYER);
array_push($ships_available, DESTROYER);
array_push($ships_available, DESTROYER);
array_push($ships_available, BATTLESHIP);
array_push($ships_available, BATTLESHIP);
array_push($ships_available, AIRCRAFTCARRIER);
$board = [];
while(count($ships_available) > 0) {
deployShip($board, $ships_available);
}
$lastColumnLetter = chr(ord('A')+ BOARD_SIZE-1);
//print table
echo "<table border='1' align='center'>";
echo "<tr><td></td><td>".implode('</td><td>',range(1, BOARD_SIZE))."</td></tr>";
for($i = 'A'; $i <= $lastColumnLetter; $i++) {
echo "<tr><td>".$i."</td>";
for($j=0; $j < BOARD_SIZE; $j++) {
echo "<td align='center'>";
echo $board[$i][$j] ?: ' ';
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
exit;
function array_merge_custom($array1, $array2) {
foreach($array2 as $key => $value) {
foreach($value as $k => $v) {
$array1[$key][$k] = $v;
}
}
return $array1;
}
function deployShip(&$board, &$ships_available) {
$randomShipKey = array_rand($ships_available);
$ship = $ships_available[$randomShipKey];
$beginCoordinates = getRandomCoordinates();
$coordinates = getShipCoordinates($board, $beginCoordinates, $ship);
if(!$coordinates) {
return false;
}
unset($ships_available[$randomShipKey]);
$board = array_merge_custom($board,$coordinates);
}
function getRowNumberByLetter($letter) {
return array_search($letter, range('A','Z'));
}
function getRowLetterByNumber($number) {
$letters = range('A','Z');
return $letters[$number];
}
function getRandomCoordinates() {
return ['row' => chr(mt_rand(ord('A'), (ord('A') + BOARD_SIZE-1))), 'col' => mt_rand(0,BOARD_SIZE -1)];
}
function getShipCoordinates($board, $beginCoordinates, $ship) {
if(isset($board[$beginCoordinates['row']][$beginCoordinates['col']])) {
return false; //anchor position already taken
}
if($ship == 1) {
$return[$beginCoordinates['row']][$beginCoordinates['col']] = 1;
return $return;
}
$shipArraySize = $ship -1;
$directions = ['left', 'right', 'up', 'down'];
$tries = 10;
while($tries > 0) {
$tries--;
$direction = $directions[array_rand($directions)];
$return = [];
switch($direction) {
case 'left':
if(($beginCoordinates['col'] - $shipArraySize) < 0) { //check if can go left
break;
}
for($colBegin = ($beginCoordinates['col'] - $shipArraySize), $colEnd = $beginCoordinates['col']; $colBegin <= $colEnd; $colBegin++) {
if(!empty($board[$beginCoordinates['row']][$colBegin])) {
break 2;
} else {
$return[$beginCoordinates['row']][$colBegin] = $ship;
}
}
return $return;
case 'right':
if(($beginCoordinates['col'] + $shipArraySize) > BOARD_SIZE -1) { //check if can go right
break;
}
for($colBegin = $beginCoordinates['col'], $colEnd = ($beginCoordinates['col'] + $shipArraySize); $colBegin <= $colEnd; $colBegin++) {
if(!empty($board[$beginCoordinates['row']][$colEnd])) {
break 2;
} else {
$return[$beginCoordinates['row']][$colBegin] = $ship;
}
}
return $return;
case 'up':
if((getRowNumberByLetter($beginCoordinates['row']) - $shipArraySize) < 0) { //check if can go up
break;
}
for($rowBegin = (getRowNumberByLetter($beginCoordinates['row']) - $shipArraySize), $rowEnd = getRowNumberByLetter($beginCoordinates['row']); $rowBegin <= $rowEnd; $rowBegin++) {
if(!empty($board[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']])) {
break 2;
} else {
$return[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']] = $ship;
}
}
return $return;
case 'down':
if((getRowNumberByLetter($beginCoordinates['row']) + $shipArraySize) > BOARD_SIZE -1) { //check if can go down
break;
}
for($rowBegin = getRowNumberByLetter($beginCoordinates['row']), $rowEnd = (getRowNumberByLetter($beginCoordinates['row']) + $shipArraySize); $rowBegin <= $rowEnd; $rowBegin++) {
if(!empty($board[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']])) {
break 2;
} else {
$return[getRowLetterByNumber($rowBegin)][$beginCoordinates['col']] = $ship;
}
}
return $return;
}
}
return false;
}

Categories