How to print an array of links in PHP - 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>';

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>';
}

Highlight every other row of a table in a foreach loop and highlight columns depending on value of row

I had a really long description written for my problem, but I've progressed and have managed to get it partially working... I basically want every other row in a table to be a different color - and to highlight certain rows if they meet a condition that I set.
foreach ($data as $row) {
$style = null;
while (($values = fgetcsv($handle, 0, '|')) !== false) {
$comment_lines = $values[6];
$priority = $values[7];
$time_worked = $values[11];
$var_X = strpos($priority, '1');
$var_C1 = strpos($comment_lines, 'CCB');
$var_C2 = strpos($comment_lines, 'CEB');
$var_U = empty($time_worked);
}
if (empty($values[0]) && count($values) === 1) {
continue;
}
if (strlen($var_X)) {
echo '<tr class="gradeX">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_C1 !== false) {
echo '<tr class="gradeC">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_C2 !== false) {
echo '<tr class="gradeC">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_U !== false) {
echo '<tr class="gradeU">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($odd) {
$odd = !$odd;
echo '<tr class="even gradeA">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else {
$odd = !$odd;
echo '<tr class="odd gradeA">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
}
echo '</tr>';
}
Right now, only the 'every other' is working. I can't get the gradeX, gradeC and gradeU to show anything...
EDIT:
This is the working code, cheers.
while (($values = fgetcsv($handle, 0, '|')) !== false) {
$style = null;
$comment_lines = $values[6];
$priority = $values[7];
$time_worked = $values[11];
$var_X = strpos($priority, '1');
$var_C1 = strpos($comment_lines, 'CCB');
$var_C2 = strpos($comment_lines, 'CEB');
$var_U = empty($time_worked);
if (empty($values[0]) && count($values) === 1) {
continue;
}
if ($var_X !==false) {
echo '<tr class="gradeX">';
} elseif ($var_C1 !== false) {
echo '<tr class="gradeC">';
} elseif ($var_C2 !== false) {
echo '<tr class="gradeC">';
} elseif ($var_U !== false) {
echo '<tr class="gradeU">';
} else {
//nothing
}
foreach ($values as $index => $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
In your loop you need to add a counter to identify whether you're currently on an 'even' or an 'odd' iteration, and then add either class 'odd' or 'even' into the as you output it.
There's a related question already on here that covers that here: php: how to add odd/even loop in array
The other parts should be fairly easy by using if statements to check if the relevant data exists or not.
For example, strlen will tell you whether a variable has length or not (to put out 'gradeX'), and strpos will allow you to check if a varibale contains a string (for 'gradeC').
If you can target IE later than IE8 and forget about IE8, you can use CSS3:
tr:nth-child(odd) {
background-color: #EEEEEE;
}
tr:nth-child(even) {
background-color: #FFFFFF;
}

php foreachloop with html tables

I have a foreach loop for 'n' number of cubicles.
I want to display 4 cubicles in each row and reaminig in next row.
How to limit 4 cubicles in each row within foreach loop.
Right now below code display all cubicles in one row
print '<table border="2">';
print '<tr>';
foreach($Cubicle as $cubicle )
{
print '<td>';
if($nodeStatus == '0'){
printf('%s ', $nodeId,$insert);
}
elseif($nodeStatus == '1'){
printf('%s ', $nodeId,$insert);
}
else{
printf('%s ', $nodeId,$insert);
}
print '</td>';
}
Use array_chunk PHP Manual to obtain the 4 values per each row:
echo '<table border="2">';
foreach(array_chunk($Cubicle, 4) as $row )
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>', $col /* your column formatting */, '</td>';
}
echo '</tr>';
}
echo '</table>';
print '<table border="2">';
print '<tr>';
foreach($Cubicle as $num => $cubicle )
{
if ($num%4 == 0)
{
print '</tr><tr>';
}
print '<td>';
...
This should do the trick, and is flexible:
function printTable($cubicles, $items_per_row) {
print '<table border="2">';
while($row = array_splice($cubicles, 0, $items_per_row)) {
print '<tr>';
printRow($row, $items_per_row);
print '</tr>';
}
print '</table>';
}
function printRow($cubicles, $items_per_row) {
for($i=0; $i<$items_per_row; $i++) {
print '<td>';
print (isset($cubicles[$i]) ? $cubicles[$i] : ' ');
print '</td>';
}
}
printTable($Cubicle, 4);
print '<table border="2">';
print '<tr>';
$rowNum = 0;
foreach($Cubicle as $cubicle){
$rowNum++;
if($rowNum % 4 == 0){ echo '<tr>'; }
print '<td>';
if($nodeStatus == '0'){
printf('%s ', $nodeId,$insert);
}
elseif($nodeStatus == '1'){
printf('%s ', $nodeId,$insert);
}
else{
printf('%s ', $nodeId,$insert);
}
print '</td>';
if($rowNum % 4 == 0){ echo '</tr>'; }
}
try this.
print '<table border="2">';
$s=0;
foreach($Cubicle as $cubicle )
{
if($s == 0){
echo $open_tr = '<tr>';
}else if($s % ceil(count($Cubicle)/4) == 0){
echo $open_ul = '</tr><tr>';
}else{
echo $open_ul = '';
}
print '<td>';
if($nodeStatus == '0'){
printf('%s ', $nodeId,$insert);
}
elseif($nodeStatus == '1'){
printf('%s ', $nodeId,$insert);
}
else{
printf('%s ', $nodeId,$insert);
}
print '</td>';
if($s == (count($Cubicle) - 1)){
echo '</tr>';
$s++;
}
}
print '<table border="2">';
print '<tr>';
$cellIndex = 0;
foreach($Cubicle as $cubicle )
{
if ((++$cellIndex % 4) == 0) {
print '</tr><tr>';
}
print '<td>';
...
The basic technique consist on using a numeric counter to keep track of the column and the modulus operator to keep it in within the column range. Also, since it's an HTML table you may also want to fill missing cells so the display looks good.
Here's an example:
<?php
define('NUM_COLUMNS', 4);
$cubicle = array('A', 'B', 'C', 'D', 'E', 'F');
if( empty($cubicle) ){
echo '<p>No cubicles found.</p>';
}else{
echo '<table>' . PHP_EOL;
$column = 0;
foreach($cubicle as $cubicle_name){
if( $column==0 ){
echo '<tr>';
}
echo '<td>' . htmlspecialchars($cubicle_name) . '</td>';
if( $column==NUM_COLUMNS-1 ){
echo '</tr>' . PHP_EOL;
}
$column = ($column+1) % NUM_COLUMNS;
}
// Fill gaps
if( $column>0 ){
while( $column<NUM_COLUMNS ){
echo '<td>—</td>';
$column++;
}
echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;
}

php - printing keys with embedded function

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.

Categories