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)"
Related
This is the code for table in Wordpress.I want to add a class to the first element of the loop. How can I do that. produces series of td elements, how can I change the code so that a class is added only to the first td in tr loop.
$table = get_field( 'hosting_plan_table' );
if ( $table ) {
echo '<tbody>';
foreach ( $table['body'] as $tr ) {
echo '<tr>';
foreach ( $tr as $td ) {
echo '<td><span id="dottedunderlinet" class="'.$td['c'].'"> ';
echo $td['c'];
echo '</td>';
}
echo '</tr>';
}
You could do this
if ( $table ) {
echo '<tbody>';
foreach ( $table['body'] as $tr ) {
echo '<tr>';
$class='first';
foreach ( $tr as $td ) {
echo '<td class="'.$class.'"><span id="dottedunderlinet" class="'.$td['c'].'"> ';
echo $td['c'];
echo '</td>';
$class='';
}
echo '</tr>';
}
Have a look at this. I have set up a variable called $i. Within the foreach loop I add 1 to $i. This means that the first time the loop runs then $i is equal to zero. The !$i test is then matched and $class can be set to be the name of the class you want to apply.
$table = get_field( 'hosting_plan_table' );
$i = 0;
if ( $table ) {
echo '<tbody>';
foreach ( $table['body'] as $tr ) {
echo '<tr>';
foreach ( $tr as $td ) {
if(!$i) { $class = 'some-class'; } else { $class = ''; }
echo '<td><span id="dottedunderlinet" class="'.$class.'"> ';
echo $td['c'];
echo '</td>';
}
echo '</tr>';
$i++;
}
You can update your code like this:
$counter = 0;
foreach ( $tr as $td ) {
if($counter == 0){
echo '<td class="my-custom-class-here"><span id="dottedunderlinet" class="'.$td['c'].'"> ';
} else {
echo '<td><span id="dottedunderlinet" class="'.$td['c'].'"> ';
}
echo $td['c'];
echo '</td>';
$counter++;
}
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>';
}
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.
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>';
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.