php - printing keys with embedded function - php

I need help figuring out these php print (echo) statements and where to place them. I have an embedded function 'strotime' that is transforming time (column 'StartTime') to a format, but I cannot get it to print out correctly. No errors, just no changes or use of the function.
Can someone help me figure out where to place this properly in this foreach loop?
(as you can see, i placed at beginning and tried an if statment too..but no luck). Thanks for
your help.
$keys = array('Server', 'Target','Logdate','Set','StartTime', 'Length','Size','Status');
echo '<table><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($keys as $column)
//if ($column == 'StartTime') {
// echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column =='StartTime') {
echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
} elseif ($column == 'Status') {
echo '<td> Check for Errors </td>';
} else {
echo '<td> </td>';
}
//}
}
echo '</table>';

In the beginning if foreach ($data as $row){ loop, do this:
$row['StartTime'] = date("Y-m-d H:i:s",strtotime($row['StartTime']));
And then display it like any other column.

Change
if (isset($row[$column])) {
to
if (isset($row[$column]) && $column != "StartTime") {
and by the way: you're missing the </tr> tags.

Related

Add a hyperlink only to one column of a table

I have some query that extracts data from the database like this:
|17DATE00 |concat(filename, fileextension) |
|-----------|--------------------------------|
|2017-05-30 |filename00000.pdf |
|2017-03-29 |filename00002.doc |
Now for the second column, I have passed the link of the doc, so you can click on it and download it.
This is the way I extract the data
$header = array("17DATE00", "concat(filename, fileextension)");
echo "<thead><tr>";
foreach ($header as $list) {
echo '<th >' . $list . '</th>';
}
echo "</thead></tr>";
foreach ($query as $key => $value) {
foreach ( $value as $a ) {
$url = get_template_directory_uri();
echo '<td>'. htmlspecialchars($a) .' </td>';
}
echo '</tr>';
}
Doing this, both columns get hyperlinked, while I only need the second one.
Anyway to fix this?
Add a counter to you inner loop:
$counter = 0;
foreach ( $value as $a )
{
$url = get_template_directory_uri();
if ($counter == 1)
{
echo '<td>'. htmlspecialchars($a) .' </td>';
}
else
{
echo '<td>'. htmlspecialchars($a) .'</td>';
}
$counter++;
}
You should print the link only if $key == "concat(filename, fileextension)"

display exact array elements using php

I am able to display all the array elements using the code below but I would like to know how can I display several array elements only (for example 4th, 6th and 11th). Could you please help me.
$rates = $data->Rates->ExchangeRate;
if (is_array($rates) && count($rates) > 0) {
echo '<table><tr><th>ISO</th><th>Rate</th></tr>';
foreach ($rates as $rate) {
echo '<tr>';
echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td></tr>';
echo '</tr>';
}
echo '</table>';
}
Just make an array of allowable key values and everytime check if the current key is included:
$rates = $data->Rates->ExchangeRate;
$allow = array(4,6,11);
if (is_array($rates) && count($rates) > 0) {
echo '<table><tr><th>ISO</th><th>Rate</th></tr>';
foreach ($rates as $key => $rate) {
if(!in_array($key, $allow){
continue;//if not allowed, go to next iteration
}
echo '<tr>';
echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td></tr>';
echo '</tr>';
}
echo '</table>';
}
You could filter the array in any number of ways before iterating over it, or you could use a condition inside the loop
$rates = $data->Rates->ExchangeRate;
if (is_array($rates) && count($rates) > 0) {
echo '<table><tr><th>ISO</th><th>Rate</th></tr>';
$count = 0;
$show = array(4, 8, 11);
foreach ($rates as $rate) {
if ( in_array($count++, $show) ) {
echo '<tr>';
echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td>';
echo '</tr>';
}
}
echo '</table>';
}
Other answers have given solutions but i think are slower than one posted here (use an array of included indices i.e $included_indices = array(4,6,11);):
$rates = $data->Rates->ExchangeRate;
if (is_array($rates) && count($rates) > 0) {
echo '<table><tr><th>ISO</th><th>Rate</th></tr>';
foreach ($included_indices as $included_indice) {
$rate = $rates[$included_indice];
echo '<tr>';
echo '<td>' . $rate->ISO . '</td><td>' . $rate->Rate . '</td></tr>';
echo '</tr>';
}
echo '</table>';
}

How can i merge Rows of An HTML table fetched from Mysql

I have a MySQL database for a website am building, i have displayed contents of a table in the MySQL database on my page using the PHP code shown below. What i wanted to know was if there was a way to Merge rows of a column with the same value.
note: i don't want the changes to occur in the database but just on the html table displayed on the page
here is the code i used
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]])) $arr[$row[0]] = array();
$arr[$row[0]] [$row[1]] [$row[2]] [$row[3]] [$row[4]] [$row[5]] [$row[6]][] = $row[7];
}
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
echo '<td>', $subordinatearr[1], '</td>';
echo '<td>', $subordinatearr[2], '</td>';
echo '<td>', $subordinatearr[3], '</td>';
echo '<td>', $subordinatearr[4], '</td>';
echo '<td>', $subordinatearr[5], '</td>';
echo '<td>', $subordinatearr[6], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
Please change this script to
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row['Location']."</td>";
echo "<td>".$row['Vessel ']."</td>";
echo "</tr>";
}
Store the result in an array.
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]]) $arr[$row[0]] = array();
$arr[$row[0]][] = $row[1];
}
Then put the array using rowspan like as follows:
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
If data is too big, this may cause an memory error.
I did not understand this part clearly - "Merge rows of a column with the same value."
If you don't want to show duplicate records of a column, you can do it in two ways -
Option 1
Keep the value of specific column in an array. Before displaying that column check if the value exists in the array using is_array(). If exists, then skip, otherwise show and add into the array.
Option 2
While making the query, use DISTINCT(col_name) to get only the unique values.

Displaying column names and values

I want to display the column names along with the values in the PHP page.
while($get_info = mysql_fetch_row($orderdetails))
{
foreach ($get_info as $field)
{
echo "<td>" . $field . "</td>";
}
echo '</tr>';
}
This fetches just the values. How do I display the column names too?
The column names are order_id, productid, product_discount, amount, customerid, order_date.
while($get_info=mysql_fetch_array($orderdetails))
{
foreach ($get_info as $key => $val)
{
echo "<td>" .$key. ': ' . $val . "</td>";
}
echo '</tr>';
}
You are just missing the key from your foreach :
while($get_info=mysql_fetch_assoc($orderdetails))
{
foreach ($get_info as $field => $value)
{
echo "<td>" .$field.': '.$value."</td>";
}
echo '</tr>';
}
You might want to review your foreach php documentation for more information:
http://php.net/manual/en/control-structures.foreach.php
If you want each field to contain the column name, change it to mysql_fetch_array and do:
foreach($get_info as $key => $value) {
echo "<td>$key: $value</td>";
}
If you want the column names to be at the top of the table, you can either check the first row (if you're sure the table won't be empty):
$first = true;
while($get_info = mysql_fetch_assoc($orderdetails)) {
echo '<tr>';
if($first) {
$first = false;
foreach(array_keys($get_info) as $columnName) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr><tr>';
}
foreach($get_info as $field) {
echo '<td>' . $field . '</td>';
}
echo '</tr>';
}
If you're not sure that the table will have at least one element, I would use a second DESCRIBE query.
while ($get_info=mysql_fetch_assoc($orderdetails))
{
foreach ($get_info as $columnName => $field)
{
echo "<td>$columnName: $field</td>";
}
echo '</tr>';
}
Notice that I am using mysql_fetch_assoc() which fetches the row having the column names as the keys.
<?php
while($get_info=mysql_fetch_array($orderdetails))
{
foreach ($get_info as $key => $val)
{
echo "column is " .$key. 'and value is ' . $val ;
}
echo '</br>';
}
?>
A lot of people are using a key => val demo but your code will work its just that your $field is now an a key so you need to tell it what column to look at.
When echoing just go
echo "<td>" . $field->column . "</td>";
That should work.

How to print an array of links in PHP

Need some php help in figuring out how use this array I created, but first of all I'm not sure if I done this right?? Its an array with names and href links that I would like to map for given server. Pls let me know if I constructed this properly:
$server_array = array(
'server1.domain' => array(
'href' => 'https://server1.domain.com:8080'
),
'server2.domain' => array(
'href' => 'https://server2.domain.com:8080'
),
'server3.domain' => array(
'href' => 'https://server3.domain.com:9999'
...
);
I want to map the data from my key Server to one of these links. So far, I've created a table with the server names and all I want to do is map that server name to one of the above hyper links within the table.
Can someone show me how to tweak my print code to do this? Thanks.
Code to display the table with servername:
$keys = array('Server', Target','Set','Time', 'Length','Size','Status');
echo '<table id="stats_1"><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
$counter=0;
foreach ($data as $row){
$counter ++;
$class = $counter % 2 === 0 ? 'alt1' : 'alt2';
echo '<tr class="' . $class . '">';
foreach ($keys as $column){
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column == 'Status') {
echo '<td> Check Logs </td>';
} elseif ($column == 'Length') {
echo '<td> n/a </td>';
} elseif ($column == 'Size') {
echo '<td> n/a </td>';
} else {
echo '<td> </td>';
}
}
}
echo '</table>';
If I got the question correctly, I would create the array like:
$server_array = array(
'server1.domain' => 'https://server1.domain.com:8080',
'server2.domain' => 'https://server2.domain.com:8080',
...
);
And for creating the link you would have to do (assuming $row['Server'] would contain the name like 'server5.domain'):
if ($column == 'Server'){
echo '<td> ' . $row[$column] . '</td>';
}
Full code:
$keys = array('Server', 'Target','Set','Time', 'Length','Size','Status');
echo '<table id="stats_1"><tr>';
foreach ($keys as $column) {
echo '<th>' . $column . '</th>';
}
echo '</tr>';
$counter=0;
foreach ($data as $row){
$counter ++;
$class = $counter % 2 === 0 ? 'alt1' : 'alt2';
echo '<tr class="' . $class . '">';
foreach ($keys as $column){
if (isset($row[$column])){
if ($column == 'Server'){
echo '<td> ' . $row[$column] . '</td>';
} else {
echo '<td>' . $row[$column] . '</td>';
}
} elseif ($column == 'Status') {
echo '<td> Check Logs </td>';
} elseif ($column == 'Length') {
echo '<td> n/a </td>';
} elseif ($column == 'Size') {
echo '<td> n/a </td>';
} else {
echo '<td> </td>';
}
}
}
echo '</table>';

Categories