Advanced Custom Fields - display label and value only if value entered - php

i currently have the following:
<?php
$field_name = "text_field";
$field = get_field_object($field_name);
if( isset($field['value'] ): ?>
<table class="">
<tbody>
<tr class="">
<th><?php echo $field['label']; ?></th>
<td><?php echo $field['value']; ?></td>
</tr>
</tbody>
</table>
<?php endif; ?>
my goal is to make the entire table row collapse and not display if there is no value entered.
clearly a novice. thanks for taking a look.

As per ACF documentation, field[‘value’] will always be set.
Instead do if (!empty($field['value']) or just if ($field['value']).
Thus it should look like this:
<?php
$field_name = "text_field";
$field = get_field_object($field_name);
?>
<table>
<tbody>
<?php
if ($field['value']): ?>
<tr>
<th><?php echo $field['label']; ?></th>
<td><?php echo $field['value']; ?></td>
</tr>
<?php endif; ?>
</tbody>
</table>

Related

How to print out two queries in one table?

I have two queries in a form. I print out the results like this:
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I want to present each result in a different column. But the result from the second query ("Column4") seems to be presented in a second table (??). It's not echoed next to the other columns, but below:
Current Output
How can I fix this issue?
You have to change
<?php endforeach; ?> </tr>
<tr> <?php foreach ($result2 as $row2) : ?>
into
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
You have begun another row ('tr' tag closed and re-opened).
Try this fix...
I have removed the things that are causing it to echo on next line
Now this will work...
<table>
<thead>
<tr>
<th>Heading1</th>
<th>Heading2</th>
<th>Heading3</th>
<th>Heading4</th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row1) : ?>
<tr>
<td><?php echo escape($row1["Column1"]); ?></td>
<td><?php echo escape($row1["Column2"]); ?></td>
<td><?php echo escape($row1["Column3"]); ?></td>
<?php endforeach; ?>
<?php foreach ($result2 as $row2) : ?>
<td><?php echo escape($row2["Column4"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

PHP table. Show total before the new group

I am having a problem about showing total in groups.
Here is my scenario, I have a report grouped by area and by product.
What I already have is the row for area group.
What I want to do is to show the total qty of product per area before the row for the next group. Currently, it shows the total after every row.
Here is my code.
<?php if (isset($summaryPerArea)): ?>
<div class="col-lg-12">
<table id="" class="table table-bordered table-condensed table-striped">
<thead>
<tr>
<th>Area</th>
<th>Material</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
$prevArea = '';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php $total += $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea): ?>
<tr class="bg-info">
<?php if ($key != 0) {$total = $currentQty;} ?>
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php endif; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php if ($value['area'] == $prevArea): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php endif; ?>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php endif ?>
query:
SELECT d.SOffcNm as area,
c.ProdNm as material,
SUM(Qty) as totalQty
FROM BigEMerchandiser.dbo.tbl_Delivery_H as a
INNER JOIN BigEMerchandiser.dbo.tbl_Delivery_D as b
ON a.TransCtr = b.TransCtr
INNER JOIN BigESales.dbo.tbl_Materials as c
ON b.Material = c.ExtMatGrp
INNER JOIN BigESales.dbo.tbl_Customers as d
ON a.CustCode = d.CustCode
WHERE d.SOffc LIKE ISNULL('%' + #area + '%', d.SOffc)
AND a.DtRcv BETWEEN #DtRcvFrom AND #DtRcvTo
GROUP BY d.SOffcNm,
c.ProdNm
ORDER BY d.SOffcNm asc
current result:
Thankyou. I appreciate your help.
try to change your code according to the comments in the code below (only the foreach part)
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php if ($value['area'] != $prevArea and $prevArea): // show total when changing area from second area onward ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<?php $total = 0; // reset the total after displaying it ?>
<?php endif; ?>
<?php if ($value['area'] != $prevArea):// show area header at every area change ?>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
</tr>
<?php endif; ?>
<?php //$currentQty = $value['totalQty']; // does not needed ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php $prevArea = $value['area']; // set prevArea to the processed row ?>
<?php endforeach ?>
<?php // show the last total ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
notice that the order of the rows are repeated as follows:
--area total--
--area header--
--area item--
and followed by:
--area total--
on the first foreach, prevArea is still '' so the first condition in the --area total-- (and $prevArea) would result in false, so that the --area total-- is suppressed, but the --area header-- does not have that condition, so the --area header-- is not suppressed.
I use a different syntax of php, using if(condition){code};
If I understood your problem, I make a different solution, I controll if the next is different from the value that i use now, because if it was different you need to print.
I rewrite your code like this:
<tbody>
<?php
$prevArea = 'AnElementLikeFlag';
$total = 0;
$currentQty = 0;
?>
<?php foreach ($summaryPerArea as $key => $value): ?>
<?php $currentQty = $value['totalQty']; ?>
<?php if ($value['area'] != $prevArea && $value['area']!="AnElementLikeFlag"): ?>
<tr class="bg-success">
<td colspan="3"><?php echo $total; ?></td>
</tr>
<tr class="bg-info">
<td colspan="3"><?php echo $value['area']; ?></td>
<?php $prevArea = $value['area']; ?>
</tr>
<?php $total=0; ?>
<?php endif; ?>
<?php $total += $value['totalQty']; ?>
<tr>
<td><?php echo $value['area']; ?></td>
<td><?php echo $value['material']; ?></td>
<td><?php echo $value['totalQty']; ?></td>
</tr>
<?php endforeach ?>
</tbody>
I hope I help you and I maybe make an error.
P.S. You print the total every time, because there is a if condition that it is not necessery

using variable from array outside of foreach loop

Im trying to display variables from an array. I am using a foreach loop, however I need to display $order['campaign_name'] before the loop so that it only shows up once. How can I do this? If I change it to $orders['campaign_name'] I get an undefined index error.
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<?php foreach ($orders as $order) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $order['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
You are trying to get those value which is not exist before loop. You are directly calling VALUE.
Easily just put index value behind tha array
echo $orders[0]['campaign_name']
It will print your value.
You would need need to know which index of the array you want to show, but if you want to show the first index of the array you can use $orders[0]['component_name'].
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $orders[0]['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order) { ?>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } else { ?>
<tbody>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
Your campaign name continues to echo out because you have it inside of your foreach loop; if you want it to display only once, make sure to place it above your foreach loop.
Here's a less cluttered example to learn from:
<table>
...
<?php
if ($orders):
echo $order['campaign_name'];
foreach($orders as $order):
echo '...'; // other components
endforeach;
else:
echo $text_no_results;
endif;
?>
...
</table>

format php output in html table

I have a following code which I have written to convert the download monitor output to html table.
<div class="table-1">
<table width="100%">
<thead>
<tr>
<th>Lab Title</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php $dlm_download->the_title(); ?></td>
<td><a href="<?php $dlm_download->get_the_download_link(); ?>">
<?php $dlm_download->the_title(); ?> (<?php echo $dlm_download->get_the_download_count(); ?>)</a></td>
</tr>
</tbody>
</table>
</div>
however this gives me the output as shown below
how can I modify code so the table headers aren't repeated for every download.
Update: here's the php code
<?php global $dlm_page_addon; ?>
<div class="download_category download_group">
<!-- <h3><?php //echo $category->name; ?> <?php //if ( $category->count ) : ?>(<?php //echo $category->count; ?>)<?php //endif; ?></h3> -->
<?php while ( $downloads->have_posts() ) : $downloads->the_post(); ?>
<?php $dlm_download = new DLM_Download( get_the_ID() ); ?>
<?php $template_handler = new DLM_Template_Handler(); $template_handler->get_template_part( 'content-download', $format, $dlm_page_addon->plugin_path() . 'templates/', array( 'dlm_download' => $dlm_download ) ); ?>
<?php endwhile; ?>
</div>
As far as I understand, this code passes the array $dlm_download to the template. The template code is the one with the table and has repeating table headers. I want to have a three column table with download title, category and download link.
Replace <th></th> tags with <td></td> and use <strong>Content</strong>
<div class="table-1">
<table width="100%">
<thead>
<tr>
<td><strong>Lab Title</strong></td>
<td>Download</td>
</tr>
</thead>
<tbody>
<tr>
<td><?php $dlm_download->the_title(); ?></td>
<td><a href="<?php $dlm_download->get_the_download_link(); ?>">
<?php $dlm_download->the_title(); ?> (<?php echo $dlm_download->get_the_download_count(); ?>)</a></td>
</tr>
</tbody>
</table>
</div>

How to display a empty record error message in symfony 1.4

I am using a foreach loop to display table records. Below is my code for indexSuccess.php template
<?php $counter = 0; ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<?php $counter++; ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php if($counter==0): ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php endif; ?>
I can't seem to find a way to echo
<td colspan="5">No items found</td>
when the table is not showing any records.
If i use
<?php echo var_dump ($counter); ?>
It displays the result below
int 1
int 2
int 1
int 2
int 1
int 2
int 1
int 2
int 1
int 2
<?php if($prescription->getPrescriptionDrugs()->count() == 0): ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php else: ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
You can use the count() function witch returns you the number of rows.
<?php $rows = count($prescription->getPrescriptionDrugs()); ?>
<?php if($rows==0){ ?>
<tr>
<td colspan="5">No items found</td>
</tr>
<?php } else { ?>
<?php foreach ($prescription->getPrescriptionDrugs() as $prescriptionDrug): ?>
<tr>
<th class="start"><?php echo $prescriptionDrug->getDrug()->getDrugName() ?></th>
<td><?php echo $prescriptionDrug->getAmountPerTime() ?></td>
<td><?php echo $prescriptionDrug->getTimesPerDay() ?></td>
<td><?php echo $prescriptionDrug->getCreater() ?> (<?php echo $prescriptionDrug->getCreatedAt() ?>)</td>
</tr>
<?php endforeach; ?>
<?php } ?>

Categories