I have managed to create two arrays from database, one for table_headers as follows
$tb_headers=array('010','011','012','013','014','015','016');
And also generated other arrays of table _data from database as follows
$tb_data=array(
array('011','013','014','015'),
array('010','012','014','015','016'),
array('010','011','013','016'),
array('010','011','012','013','014','015','016')
);
How do I generate table of this format??
th 010 | 011 | 012| 013| 014 | 015 | 016
row1 - | 011 | - | 013| 014 | 015 | -
row2 010 | - | 012 | - | 014 | 015 | 016
row3 010 | 011 | - | 013| - | - | 016
row4 010 | 011 | 012 | 013| 014 | 015 | 016
I have tried to write this script which is not working as expected
<table style="width:50%;" border="1">
<tr>
<?php
foreach ($tb_headers as $key => $value) {
echo '<th>'.$value.'</th>';
}
echo '</tr>';
foreach ($tb_headers as $key => $data) {
echo '<tr>';
if(!empty($tb_data[$key])&& $tb_data[$key]==$data ){
echo '<td>'.$tb_data[$key].'</td>';
}else{
echo '<td>-</td>';
}
echo '</tr>';
}
?>
</table>
Try this, which uses the php in_array() function.
<table>
<tr>
<?php
// header
foreach ($tb_headers as $value) echo '<th>' . $value . '</th>';
?>
</tr>
<?php
// content
foreach ($tb_data as $line => $values) {
echo '<tr>';
foreach ($tb_headers as $header) {
echo '<td>';
if (in_array($header, $values)) echo $header;
else echo '-';
echo '</td>';
}
echo '</tr>';
}
?>
</table>
Related
I have a string that contains multiple lines. how can I convert it into tables where in each td value is every word of each line.
example of the string.
Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T
Eth1/2 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T
expected table outcome:
Eth1/1 | VPC_PEER_KEEPALIVE | connected | routed | full | 1000 | 1000base-T
Eth1/2 | VPC_PEER_KEEPALIVE | connected | routed | full | 1000 | 1000base-T
<?php
$str = "Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T
Eth1/2 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T";
// echo $str;
$v1 = explode(PHP_EOL, $str);
foreach ($v1 as $key => $value) {
$v2 = explode(" ", $value);
echo "<tr>";
foreach ($v2 as $key2 => $value2) {
echo "<td>" .$value2 ."</td>";
}
echo "</tr>";
}
?>
This JS code will work-
var string = "Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T"; //Whatever your string is
string = string.split(" ");
var table = "<table id='table' border='1px'><tr>";
for (i = 0; i < string.length; i++) {
table += "<td>" + string[i] + "</td>";
}
table += "</tr></table>";
document.write(table); //Whatever you want do with your table
This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed 4 years ago.
my table sap rfc
how can I add number in front of ID? if ID contain 3 digits it will add 000 in front of ID, if ID contain 4 digits it will add 00 in front of ID, if ID contain 5 digits it will add 0 in front of ID. For an example
ID | FEE | NAME | PERIOD
000711 | 204000 | YUDI MANDALA | 201807
000790 | 84000 | AGUS WAHYUDI | 201807
001171 | 151500 | SARJANA | 201807
my code:
if ($FI_HPs!=0)
{
$array = [];
for ($i=1;$i<=$FI_HPs;$i++)
{
$FI_HP = saprfc_table_read ($fce,"FI_HP",$i);
$id = $FI_HP['ID'];
if(isset($array[$id]))
{
$array[$id]['FEE'] += $FI_HP['FEE'];
}
else
{
$array[$id] = $FI_HP;
}
}
foreach($array as $item)
{
echo '<tr>
<td>'.$item['ID'].'</td>
<td>'.($item['FEE']*100).'</td>
<td>'.$item['NAME'].'</td>
<td>'.$item['PERIOD'].'</td>
</tr>';
}
}
you could use str_pad
foreach($array as $item)
{
echo '<tr>
<td>'.str_pad($item['ID'], 6, "0", STR_PAD_LEFT) .'</td>
<td>'.($item['FEE']*100).'</td>
<td>'.$item['NAME'].'</td>
<td>'.$item['PERIOD'].'</td>
</tr>';
}
$base = 100000 ;
'. substr($base+$item['ID'],1) .'
Hi I have a PHP array like this
$table=array();
$subject_names=array();
$subject_names[118]="English";
$subject_names[108]="Software Engeneering";
$table['Josh'][118]['int'] =55;
$table['Josh'][118]['ext'] = 10;
$table['Josh'][108]['int'] =45;
$table['Josh'][108]['ext'] = 12;
$table['Matt'][118]['int'] =45;
$table['Matt'][118]['ext'] = 12;
$table['Matt'][108]['int'] =50;
$table['Matt'][108]['ext'] = 15;
Here 118 and 108 are subject_id I am trying to format it like this
student | English | Software Engeneering |
| int. mark | ext. mark | int. mark | ext. mark |
___________________________________________________________
Josh | 55 | 10 | 45 | 12
Matt | 45 | 12 | 50 | 15
I tried
echo "Student Name\t";
foreach($subject_names as $sub_name)
{
echo "$sub_name\t";
}
echo "<br>";
foreach($table as $sname => $subjects){
echo "$sname\t";
foreach($subjects as $subject_name => $types)
{
foreach($types as $marks)
{
echo "$marks\t";
}
}
echo "<br>";
}
It working fine but if I change the position of array item of table like
$table['Josh'][118]['int'] =55;
$table['Josh'][108]['int'] =45;
$table['Josh'][118]['ext'] = 10;
$table['Josh'][108]['ext'] = 12;
It won't give a correct result. Is there anyway to assure that the result are always correct.
Thank you for your any help and suggestions
Here's a solution I wrote for your request, picking the scores using the $subject_names as control rather than the students' scoresheet (I hope you get what I mean after going through the codes)...
$table=array();
$subject_names=array();
// notice that I switched subject order, just to show that subjects control the marks displayed thereby ensuring consistent score mapping
$subject_names[108]="Software Engeneering";
$subject_names[118]="English";
// and I'm using the rearranged scores (the sample use-case you specified in the question that distorts your output)
$table['Josh'][118]['int'] =55;
$table['Josh'][108]['int'] =45;
$table['Josh'][118]['ext'] = 10;
$table['Josh'][108]['ext'] = 12;
$table['Matt'][118]['int'] =45;
$table['Matt'][118]['ext'] = 12;
$table['Matt'][108]['int'] =50;
$table['Matt'][108]['ext'] = 15;
echo "Student Name\t";
foreach($subject_names as $sub_name)
{
echo "$sub_name\t";
}
echo "\n";
// proposed solution:
foreach($table as $sname => $subjects){
echo "$sname\t";
foreach ($subject_names as $subject_id => $name) {
foreach ($subjects[$subject_id] as $mark) {
echo "$mark\t";
}
}
echo "\n";
}
Here's the output of the script above...
Student Name Software Engeneering English
Josh 45 12 55 10
Matt 50 15 45 12
Running the same data through the script provided in the question, here's the output (distorted)...
Student Name Software Engeneering English
Josh 55 10 45 12
Matt 45 12 50 15
P.S: 'Engeneering' should be 'Engineering'
I hope I've been helpful.
Cheers!
im in serach for a generic piece of code that uses and array
$arr[$key1][$key2] = $value;
output should be like this where "SUM" is not part of the array.
| 1st key2 | 2nd key2 | 3rd key2 | SUM
1st key1 | 10 | 10 | 10 | 30
2nd key1 | 10 | 10 | 10 | 30
3rd key1 | 10 | 10 | 10 | 30
SUM | 30 | 30 | 30 | 90
so i startet an output to see how far i get:
echo '<table width="100%"><thead>';
foreach($arr as $linekey => $line)
{
echo '<tr>';
echo '<th align="center">';
echo '</th>';
foreach($line as $key => $value)
{
echo '<th align="center">';
echo $key;
echo '</th>';
}
echo '<th align="center">';
echo 'SUM'; //adding the SUM column
echo '</th>';
echo '</tr>';
break;
}
echo '</thead><tbody>';
foreach($arr as $key1 => $value1)
{ echo '<tr>';
echo'<td>'.$key1.'</td>';
$sumRow = 0; //reset sumRow
foreach($arr[$key1] as $key2 => $value2)
{
echo'<td>'.round($arr[$key1][$key2],0).'</td>';
$sumRow += $arr[$key1][$key2]; //summing up rows
$sumAll += $arr[$key1][$key2]; //summing up everything
$sumCol += $arr[$key1][$key2]; //where should be this?
}
echo'<td>'.round($sumRow,0).'</td>'; //echo $sumRow
echo '</tr>';
}
echo '</tbody></table>';
this alaredy works but im not sure where to sum the columns
You should use an array $sumCol to gather columns sums:
$sumCol[$key2] += $arr[$key1][$key2];
It size should be as number of columns.
You cannot do it in one loop without an array because you loop over columns index internally, so you could gather only sumRow in one temporary variable (without array).
Then, at the end:
echo '<tr><td>SUM</td>';
foreach($sumCol as $key2 => $value2)
{
echo'<td>'.round($sumCol[$key2],0).'</td>'; //echo $sumCol
}
echo '</tr>';
echo '</tbody></table>';
The other way is to define the second loop where you iterate first over columns and at second, internally, over rows.
I have data stored in a text file (This is a requirement), which I’ve turned into an array…
Now I’m trying filter my results based on 2 variables $Start_Date & $End_Date
…then display only unique values with the count next to it.
Stats.txt
08/02/14 | Red | Wood
08/03/14 | Red | Steel
08/04/14 | Blue | Wood
08/05/14 | Red | Steel
08/05/14 | Red | Wood
08/05/14 | Red | Steel
08/05/14 | Blue | Steel
08/06/14 | Blue | Wood
What I have so far:
$logfile = "stats.txt";
$Start_Date = 08/04/14;
$End_Date = 08/05/14;
if (file_exists($logfile)) {
$handle = fopen($logfile, "r");
$log = fread($handle, filesize($logfile));
fclose($handle);
} else {
die ("The log file doesn't exist!");
}
// Seperate each logline
$log = explode("\n", trim($log));
// Seperate each part in each logline
for ($i = 0; $i < count($log); $i++) {
$log[$i] = trim($log[$i]);
$log[$i] = explode('|', $log[$i]);
}
Desired Result: (based on $Start_Date = 08/04/14 and $End_Date = 08/05/14)
<h2>Date Count:</h2>
08/04/14 - 1
08/05/14 - 4
<h2>Color Count:</h2>
Red - 3
Blue - 2
<h2>Material Count:</h2>
Wood - 2
Steel - 3
write the code like this
$log = "08/02/14 | Red | Wood
08/03/14 | Red | Steel
08/04/14 | Blue | Wood
08/05/14 | Red | Steel
08/05/14 | Red | Wood
08/05/14 | Red | Steel
08/05/14 | Blue | Steel
08/06/14 | Blue | Wood";
$log = explode("\n", trim($log));
// Seperate each part in each logline
$dateFreq = array();
$colorFreq = array();
$MaterialFreq = array();
foreach ($log as $line) {
$lineArr = explode("|", $line);
$date = trim($lineArr[0]);
$color = trim($lineArr[1]);
$material = trim($lineArr[2]);
if ($date >= '08/04/14' && $date <= '08/05/14') {
if (isset($dateFreq[$date])) {
$dateFreq[$date] = $dateFreq[$date] + 1;
} else {
$dateFreq[$date] = 1;
}
if (isset($colorFreq[$color])) {
$colorFreq[$color] = $colorFreq[$color] + 1;
} else {
$colorFreq[$color] = 1;
}
if (isset($MaterialFreq[$material])) {
$MaterialFreq[$material] = $MaterialFreq[$material] + 1;
} else {
$MaterialFreq[$material] = 1;
}
}
}
echo "<h2>Date Count:</h2>\n";
foreach ($dateFreq as $date => $freq) {
echo $date, " - ", $freq, "\n";
}
echo "<h2>Color Count:</h2>\n";
foreach ($colorFreq as $color => $freq) {
echo $color, " - ", $freq, "\n";
}
echo "<h2>Material Count:</h2>\n";
foreach ($MaterialFreq as $material => $freq) {
echo $material, " - ", $freq, "\n";
}