php two dimensional array with sums to table - php

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.

Related

How can I convert multi text string to table

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

Create html table from arrays

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>

For loop array in php

Hello Guys how can i output this array using for loop?
This code is working but it is not for loop.
$a=array('Pol','Peter');
$b=array('3.2','2.79');
$c=array('1','1.4');
$values = array($a,$b,$c);
echo '<table border="1"><tr>';
echo '<td>'.$values[0][0].'</td>';
echo '<td>'.$values[1][0].'</td>';
echo '<td>'.$values[2][0].'</td>>';
echo '<tr><tr>';
echo '<td>'.$values[0][1].'</td>';
echo '<td>'.$values[1][1].'</td>';
echo '<td>'.$values[2][1].'</td>';
echo '</tr></table>';
Output:
--------------------------------
| Pol | 3.2 | 2.79 |
| Peter | 1 | 1.4 |
--------------------------------
Help me how loop this output.
Thanks in advance
You're looking for a for loop inside of a for loop.
The <table> tags should come outside of the outer loop, and the <tr> tags should come inside of the outer loop, but outside of the inner loop. Naturally, the <td> tags should come inside both.
This would look something like:
echo '<table border="1">';
for ($j = 0; $j < $c; $j++) {
echo '<tr>';
for ($i = 0; $i < count($values); $i++) {
echo '<td>'.$values[$i][$j].'</td>';
}
echo '<tr>';
}
echo '</table>';
Hope this helps! :)

how to print query mysql in php like this

I am newbie in php.
I have one table in mysql (example)
id id_pro name
1 2 budi
2 1 adi
3 1 steve
4 1 jono
5 2 galang
I would like to print like this
id_pro name
1 adi, steve, jono
2 budi, galang
I have made,but look like this. (Please help me)
id_pro name
1 adi
1 steve
1 jono
2 budi
2 galang
<?php
$a = mysql_connect("localhost","root","admin");
mysql_select_db("aa");
$result = mysql_query("select * from member");
echo '<table>
<tr><th>id_pro</th><th>name</th></tr>';
while($data = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td> '. $data["id_pro"] .'</td>';
echo '<td> '. $data["name"] .'</td>';
echo '</tr>';
}
?>
<?php
$result = array();
$data = array(array("id_pro"=>1,"name"=>"adi"),array("id_pro"=>2,"name"=>"budi"),array("id_pro"=>1,"name"=>"steve"));
foreach($data as $value){
$result[$value["id_pro"]][] = $value["name"];
}
foreach($result as $key => $value){
echo $key . "----" . implode("," , $value);
echo "\n";
}
?>
or you can change your sql like this :
select id_pro,group_concat(name) from member group by id_pro;

Custom Table using SQL query and php

I am trying to create a table from a sql query but I need to only have 3 results per table row.
I have a list of about 47 names so I have no problem printing them on a new line but how would I go about creating a table where the while loop would print a table row then print 3 table data cells with the query then create a new row for the next 3 values?
Exa:
result_1 | result_2 | result_3
result_4 | result_5 | result_6
result_7 | result_8 | result_9
Current While Loop:
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td><input type='checkbox'> ".$row['name']."</td>";
echo "<td><input type='checkbox'> ".$row['name']."</td>";
echo "<td><input type='checkbox'> ".$row['name']."</td>";
echo "</tr>";
}
Database Structure:
id | name
1 | result_1
2 | result_2
3 | result_3
4 | result_4
Thanks in advance!
You can use the modulus operator (%) to check if you're ready for a new line.
$number_of_names = count($names);
$number_of_columns = 3; //you can change this at any point
echo "<table><tr>";
for($i=0;$ i<$number_of_names; $i++){
echo "<td>" . $names[$i] . "</td>";
if ($i % $number_of_columns == ($number_of_columns - 1) && $i<$number_of_names-1){
echo "</tr><tr>";
}
}
echo "</tr></table>";
Try looping through the results using a for loop, then compare the remainder of dividing the iterator by 3, using the modulus operator.
echo "<tr>";
for($i = 0; $i<mysql_num_rows($result); $i++) {
if($i%3==0) {
echo "</tr><tr>";
}
$row = mysql_fetch_assoc($result)
echo "<td><input type='checkbox'> ".$row['name']."</td>";
}
echo "</tr>";

Categories