Move result to the right position on table in PHP - php

I'm trying to show 7 results on a table (image 1) which I would like to move them to the right position.
Mon = 1 ~ Sun = 7 as we can see on the array and where there is no value I would like to have it $0. At the moment I could not figure out how to move it to the right spot on the table.
Here is my code:
$w_rep = "SELECT
`totals`.`totals_weeknumb`,
`totals`.`totals_weekday`,
`totals`.`totals_of_day`
FROM `totals`
WHERE `totals`.`totals_has_users_id` = :fromID";
$w_rep_stmt = $DB->prepare($w_rep);
$w_rep_stmt->execute(array(':fromID' => $fromID));
$w_rep_stmt->setFetchMode(PDO::FETCH_ASSOC);
$totals_of_day = array();
foreach ($w_rep_stmt as $report) {
//$tdate = new DateTime($report['totals_date']);
//$totals_date = $tdate->format('d-m-Y');
$t_wnumber = (int)$report['totals_weeknumb'];
$t_wday = (int)$report['totals_weekday'];
$totals_of_day[$t_wnumber][$t_wday] = (float)$report['totals_of_day'];
//$sum_of_week[$t_wnumber] = array_sum(array_column($totals_of_day, ''));
} // close foreach
var_dump(array_keys($totals_of_day));
var_dump($totals_of_day);
foreach ($totals_of_day as $w_n => $y) {
?>
<tr>
<th><?php echo $w_n; ?></th>
<td>More Details</td>
<?php
foreach ($y as $w_day => $saved) {
echo '<td>'. $curr.number_format($saved, 2) .'</td>';
} // close foreach
?>
<td><?php //echo $curr.$sum_of_week[]; ?></td>
</tr>
<?php
} // close 1st foreach
I tried so many options :(

Basically you need to iterate the days of the week and use the index in your array
foreach (range(1, 7) as $day) {
echo (isset($y[$day]) ? $y[$day] : '-');
}
Example:
<?php
$totals_of_day = [
7 => [3 => 150.8, 4 => 523.88, 5 => 95.32, 7 => 10.8],
8 => [1 => 13.78, 2 => 107.33, 4 => 8.49, 5 => 125.67],
9 => [1 => 71.3, 2 => 49.68, 6 => 95, 7 => 100.57],
10 => [1 => 18.34, 2 => 44.9, 3 => 55.7, 4 => 15.58]
];
?>
<table border="1" style="border-collapse: collapse;">
<tr>
<th>Week</th>
<th>Items</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
<th>Sun</th>
<th>Total Savings</th>
</tr>
<?php foreach ($totals_of_day as $w_n => $y) { ?>
<tr>
<th><?php echo $w_n; ?></th>
<td>More Details</td>
<?php foreach (range(1, 7) as $day) {
echo '<td>'. (isset($y[$day]) ? number_format($y[$day], 2) : '-') .'</td>';
} ?>
<td><?php echo number_format(array_sum($y), 2); ?></td>
</tr>
<?php } ?>
</table>

Related

only display first column?

This code shows every info from an XML. I want to only display row number one (first from/to, symbol temperature etc)
<?php
$url = ('https://www.yr.no/sted/Norge/oslo/oslo/oslo/varsel_time_for_time.xml');
$feed = simplexml_load_file($url) or die('Can not connect to server');
$result = array();
foreach ($feed->forecast->tabular->time as $content) {
array_push($result, [ "from" => (string)$content['from'],
"to" => (string)$content['to'],
'symbol' => (string)$content->symbol['name'],
'symbol-icon' => (string)$content->symbol['var'],
'temperature' => (string)$content->temperature['value'],
'windDirection' => (string)$content->windDirection['name'],
'windSpeed' => (string)$content->windSpeed['mps'],
'windType' => (string)$content->windSpeed['name'],
]);
}
?>
<button class="collapsible"><div class="tbl-content">
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<?php foreach ($result as $value) { ?>
<tr>
<td>Oslo</td>
<td><?php echo date("j. M", strtotime($value['from'])); ?> kl.<?php echo date("G", strtotime($value['from'])); ?></td>
<td><img src="http://yr.github.io/weather-symbols/png/100/<?php echo $value['symbol-icon'];?>.png" /></td>
<td><?php echo $value['temperature'] ?> °C</td>
<td><?php echo $value['windType'] ?>, <?php echo $value['windSpeed'] ?> m/s fra <?php echo $value['windDirection'] ?></td>
<td>Longtherm</td>
<td>Hour</td>
</tr>
<?php } ?>
</tbody>
</table>
</div></button>
quite unexperienced, any help is greatly appreciated
Just one of the rows? Remove the foreach! Just use $result[0] in place of $value. :-)
A defined number of rows? Use a for loop:
for ($x = 0; $x < $limit; $x++) {
$value = $result[$x];
// etc
}

sort specific array element value

I have the following array...
Array
(
[banana] => 1
[orange] => 2
[mango] => 1
)
What I need is How do i get the exact string value to view on the following table. example the banana value to the add to 1. banana table column value.
The table below is HTML View table, Not Mysql table.
type value
-------------------
1.mango |
2.banana |
3.orange |
------------------
in short how do i search the array to get the value of each and insert value to table below.
You simply need to use a foreach.
<?php
$array = array("banana" => 1, "orange" => 2, "mango" => 1);
?>
<table>
<tr>
<th>Type</th>
<th>Value</th>
</tr>
<?php
foreach($array as $key => $val){
?>
<tr>
<td><?php echo $val; ?></td>
<td><?php echo $key; ?></td>
</tr>
<?php
}
?>
</table>
Output :
The same format as your question would be :
<?php
$array = array("banana" => 1, "orange" => 2, "mango" => 1);
?>
<table>
<tr>
<th>Type</th>
<th>Value</th>
</tr>
<?php
$i = 0;
foreach($array as $key => $val){
$i++;
?>
<tr>
<td><?php echo "$i.$key"; ?></td>
<td></td>
</tr>
<?php
}
?>
</table>
Output :
Internet,
combine foreach and switch.
http://php.net/manual/en/control-structures.foreach.php
http://php.net/manual/en/control-structures.switch.php
Use a foreach to loop through your array and then insert this in your database.
foreach( $fruits as $fruit -> $value{
...passing data to your database insert function
...i.e. $result = mysql_query("INSERT INTO kunden (type, value) VALUES ('".$fruit."', '".$value."')");
}

print html table from php array in particular way

I have a array that is dynamically generated and i am able to manipulate the data and print it in a table but I want to be able to print it differently.
This is what I have been able to do so far:
This is what I want to achieve
As you can see the array has multiple entries and for each entry I would like to create a new table and display the data like so:
There are some keys in the array I would prefer to leave out from being added to the table. the key names are: Id, CreatedDate, Incoming
Here is a short sample of what the array looks like:
[records] => Array
(
[0] => Array
(
[Id] =>
[CreatedDate] => 2016-02-18T02:24:57.000Z
[FromName] => Technical Support
[Incoming] =>
[MessageDate] => 2016-02-18T02:24:57.000Z
[Subject] => this is a test reply
[TextBody] => testt ref:_00D708cJQ._50080oYTuD:ref
[ToAddress] => outa#gmail.com
)
[1] => Array
(
[Id] =>
[CreatedDate] => 2016-02-17T13:36:52.000Z
[FromName] => Technical Support
[Incoming] => 1
[MessageDate] => 2016-02-17T13:36:08.000Z
[Subject] => MySupport Portal: Test Email via API
[TextBody] => this is a test email 4 ref:_00D708cJQ._50080oYTuD:ref
[ToAddress] => outa#gmail.com
)
Here is my current php code
$count = $response_array['size'] -1;
//print table headers
echo '<table border=1><tr>';
foreach( $response_array['records'][0] as $key => $value ) {
if($key !== 'Id') {
echo '<th>'.$key.'</th>';
}
}
echo '</tr><tr>';
//print table data
for ($i = 0; $i <= $count; $i++) {
foreach( $response_array['records'][$i] as $key => $value ) {
if($key !== 'Id') {
if($key === 'TextBody') {
echo '<td><pre>'.$value.'</pre></td>';
} else {
echo '<td>'.$value.'</td>';
}
}
}
echo '</tr><tr>';
}
echo "</tr></table>";
I know how to write the HTML but not sure how to tie in the php as im not sure how i can sort the headers to be in different part of the table.. in any case here is the html with dummy data as placeholders
<table border=1>
<tr>
<th>MessageDate</th>
<th>FromName</th>
<th>ToAddress</th>
</tr><tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr><tr>
<th colspan=3>Subject</th>
</tr><tr>
<td colspan=3>this is the subject</td>
</tr><tr>
<th colspan=3>TextBody</th>
</tr><tr>
<td colspan=3>this is the body</td>
</tr>
</table>
Okay, since you said you are able to do it for 1 line lets show you how to do it for all the line.
Just loop thourgh all the line like this.
<?php foreach( $response_array['records'] as $records): ?>
<table border=1>
<tr>
<th>MessageDate</th>
<th>FromName</th>
<th>ToAddress</th>
</tr><tr>
<td><?php echo $records['MessageDate'] ?></td>
<td><?php echo $records['FromName'] ?></td>
<td><?php echo $records['ToAddress'] ?></td>
</tr><tr>
<th colspan=3>Subject</th>
</tr><tr>
<td colspan=3><?php echo $records['Subject'] ?></td>
</tr><tr>
<th colspan=3>TextBody</th>
</tr><tr>
<td colspan=3><?php echo $records['TextBody'] ?></td>
</tr>
</table>
<?php endforeach; ?>
edit: Added php tags
Unex's answer is perfect. Here is another alternative:-
echo '<table border=1>';
foreach( $response_array['records'] as $key => $value ) {
if($key == 'MessageDate'){
echo "<tr>";
echo "<th>MessageDate</th>";
echo "<th>FromName</th>";
echo "<th>ToAddress</th>";
echo "</tr>";
echo "<tr>";
echo "<td>{$response_array['records'][$key]['MessageDate']}</td>";
echo "<td>{$response_array['records'][$key]['FromName']}</td>";
echo "<td>{$response_array['records'][$key]['ToAddress']}</td>";
echo "</tr>";
}
if($key == 'Subject'){
echo '<tr>';
echo "<th colspan='3'>Subject</th>";
echo "</tr>";
echo '<tr>';
echo "<td colspan='3'>{$response_array['records'][$key]['Subject']}</td>";
echo "</tr>";
}
if($key == 'TextBody'){
echo "<tr>";
echo "<th colspan='3'>TextBody</th>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'>{$response_array['records'][$key]['TextBody']}</td>";
echo "</tr>";
}
}
echo "</table>";

html table and populate with php with empty rows

I am trying to fill table with data.
I want to achieve something that looks like
However, my result is:
I guess this might be related to the php IF-statement.
Here is my code:
<table class="tg">
<tr>
<th class="tg-s6z2" colspan="2" rowspan="4">OPPONENT</th>
<th class="tg-s6z2" colspan="5">DIVISION</th>
</tr>
<tr>
<td class="tg-s6z2" colspan="5">TEAM</td>
</tr>
<tr>
<?php
foreach($rows as $row) {
echo "<td class='tg-031e' colspan='2'>";
echo $row["Date"];
echo "</td>";
}
?>
</tr>
<tr>
</tr>
<?php
foreach($rows as $row) {
echo"<tr>";
echo "<td class='tg-031e' colspan='2'>";
echo $row["teamName"];
echo "</td>";
if(!empty($row["Score"])){
echo"<td>";
echo$row["Score"];
echo "</td>";
}else{
echo "<td> </td>";
}
echo"</tr>";
}
?>
</table>
THE OUTPUT OF $results
Array (
[0] => Array (
[Date] => 2015-04-22
[0] => 2015-04-22
[Score] => 1:4
[1] => 1:4
[divisionID] => 2
[2] => 2
[3] => 2
[teamName] => TEAM YXZ
[4] => TEAM XYZ )
[1] => Array (
[Date] => 2015-04-15
[0] => 2015-04-15
[Score] => 2.5:2.5
[1] => 2.5:2.5
[divisionID] => 2
[2] => 2
[3] => 2
[teamName] => TEAM XYZ 'B'
[4] => TEAM XYZ 'B'
)
)
You need to loop over the dates of the column headings, checking whether the current element of $rows matches the corresponding date. First make an array of all the dates when you're creating the headings:
$dates = array();
foreach($rows as $row) {
echo "<td class='tg-031e' colspan='2'>";
echo $row["Date"];
$dates[] = $row['Date'];
echo "</td>";
}
Then when you write the rows, loop through the dates. When it matches, write the score, otherwise leave the <td> empty (there's no need to write ).
foreach($rows as $row) {
echo"<tr>";
echo "<td class='tg-031e' colspan='2'>";
echo $row["teamName"];
echo "</td>";
foreach ($dates as $date) {
echo "<td>";
if ($row['Date'] == $date && !empty($row['Score'])) {
echo $row["Score"];
}
echo "</td>";
}
echo"</tr>";
}
Your conditional statement checking $row['score'] is saying add a TD with a score or add a TD with nothing. You still need to add a TD regardless if it's blank or not or it will break the table layout.
Also, the data you get back, is it enough data for the loop to fill out the whole table? It seems to me that there is something going on with the data being returned along with the conditional statement.
The example image you gave is not corresponding with the data $rows has. The match on 2015-04-22 should have the score 1:4 right?
I've separated the logic from the HTML.
In my opinion, the way $rows is constructed is generally bad design.
With that in mind; here's what I've came up with;
<?php
$dates = array();
foreach ($rows as $row):
$dates[] = $row['Date'];
endforeach;
$matches = array();
foreach ($rows as $row):
$scores = array();
foreach ($dates as $date):
$score = '';
if (!empty($row['Score']) && $row['Date'] === $date):
$score = $row['Score'];
endif;
$scores[] = $score;
endforeach;
$matches[] = array('teamName' => $row['teamName'], 'Scores' => $scores);
endforeach;
?>
<table class="tg">
<tr>
<th class="tg-s6z2" colspan="2" rowspan="4">OPPONENT</th>
<th class="tg-s6z2" colspan="5">DIVISION</th>
</tr>
<tr>
<td class="tg-s6z2" colspan="5">TEAM</td>
</tr>
<tr>
<?php foreach ($dates as $date): ?>
<td>
<?php echo $date; ?>
</td>
<?php endforeach; ?>
</tr>
<tr>
</tr>
<?php foreach ($matches as $match): ?>
<tr>
<td class="tg-031e" colspan="2">
<?php echo $match['teamName']; ?>
</td>
<?php foreach ($match['Scores'] as $score): ?>
<td><?php echo $score; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

How to change an array into a html table in sample way?

I have a dynamic array :
Array
(
[user1] => Array
(
[012014] => 6788
[022014] => 11141
[032014] => 6143
[042014] => 936
[052014] => 936
)
[user2] => Array
(
[012014] => 9
[022014] => 25
[032014] => 37
[042014] => 17
[052014] => 16
)
)
And I want to display it like this :
Users | Months
|---------------------------------
|012014|022014|032014|042014|052014
------------------------------------------
user1 | 6788 | 11141| 6143 | 936 | 936
-------------------------------------------
user2 | 9 | 25 | 37 | 17 | 16
I can't seem to work it out! Here's what I've been trying :
echo "<table>";
foreach($month_all as $site=>$value){
echo "<tr>";
echo "<td>$site</td>";
foreach ($value as $column) {
echo "<td>$column</td>";
}
echo "</tr>";
}
echo "</table>";
Any way I can do it in a clean way ?
Use two loops: one for displaying all the key values. One for displaying all the values of the users. This version uses <th> to handle the table headers and takes care of the indentation properly because it's using the alternative foreach style.
<table>
<tr>
<th>User</th>
<th colspan="5">Months</th>
<tr>
<th></th>
<?php foreach ($month_all['user1'] as $key): ?>
<td><?= $key ?></td>
<?php endforeach ?>
</tr>
</tr>
<?php foreach ($month_all as $site => $value): ?>
<tr>
<td><?= $site ?></td>
<?php foreach ($value as $column): ?>
<td><?= $column ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</table>
Live demo
you can use like this
<table>
<tr>
<?php
foreach($Arr['user1'] as $key1=>$th)
{
?>
<th><?=$key1?></th>
<?php
}
?>
</tr>
<?php
foreach($Arr as $row)
{ ?>
<tr>
<?php
foreach($row as $column){
?>
<td><?=$column?></td>
<?php } ?>
</tr>
<?php}
?>
</table>
Clearly speaking using implode is not a good method to use.But i wanted to give you an idea that you should use colspan='.count(array_keys($month_all['user1'])).' in header to get month over all columns.
echo '<table><tr><td rowspan=2>Users</td><td colspan='.count(array_keys($month_all['user1'])).'>Months</td></tr>';
echo '<td>'.rtrim(implode(array_keys($month_all['user1']),'</td><td>'),'<td>').'';
foreach($month_all as $site=>$value){
echo "<tr>";
echo "<td>$site</td>";
foreach ($value as $column) {
echo "<td>$column</td>";
}
echo "</tr>";
}
This will give u the exact table as in your question
<?php
$array = array(
"user1" => array(
"012014" => 6788,
"022014" => 11141,
"032014" => 6143,
"042014" => 936,
"052014" => 936
) ,
"user2" => array(
"012014" => 9,
"022014" => 25,
"032014" => 37,
"042014" => 17,
"052014" => 16
)
);
print '<table border="1px solid black"><tr><td>Users</td><td colspan="5" style="text-align: center">Month</td></tr>';
print '<tr><td></td>';
foreach (reset($array) as $key => $value) {
print '<td>' . $key . '</td>';
}
print '</tr>';
foreach ($array as $key => $values) {
print '<tr>';
print '<td>' . $key . '</td>';
foreach ($values as $value) {
print '<td>' . $value . '</td>';
}
print '</tr>';
}
print '</table>';
?>
result:

Categories