How to Use HTML inside PHP (Show only if value exists) - php

I have the following code on my ecommerce product description tabs so that I can enter in all the information one field at a time without having to preformat my product descriptions in Excel with HTML.
<?php echo the_field('color');?><br>
<?php echo the_field('product_info');?><br>
<?php echo the_field('product_size');?><br>
<?php echo the_field('product_size2');?><br>
<?php echo the_field('product_size3');?><br>
Unfortunately this isn't helpful because the tags outside of the PHP will leave blank lines in cases when no value exists for these fields. How can I incorporate the tags inside the PHP so that they're used only when a value exists?
I'm clearly new to PHP (and coding in general), so I really appreciate anyone's help. Thank you!

Try:
if(get_field('field_name') != "")
{
echo '<p>' . get_field('field_name') . '</p>';
}
Should work for you

Can you try this:
<?php echo empty(trim(the_field('color'))) ? '' : the_field('color') . '<br>'; ?>
And repeat the same pattern for every field.

Without knowing what your the_field function does and how "heavy" it is, this should avoid unnecessary duplication of the calls to both test and output the result.
<?php if($color = the_field('color')): ?>
<?php echo $color; ?><br>
<?php endif; ?>
<?php if($product_info = the_field('product_info')): ?>
<?php echo $product_info; ?><br>
<?php endif; ?>
<?php if($product_size = the_field('product_size')): ?>
<?php echo $product_size; ?><br>
<?php endif; ?>
<?php if($product_size2 = the_field('product_size2')): ?>
<?php echo $product_size2; ?><br>
<?php endif; ?>
<?php if($product_size3 = the_field('product_size3')): ?>
<?php echo $product_size3; ?><br>
<?php endif; ?>

Edit: Use this:
<?php echo (the_field('color') != '') ? the_field('color') . '<br>' : ''; ?>
<?php echo (the_field('product_info') != '') ? the_field('product_info') . '<br>' : ''; ?>
<?php echo (the_field('product_size') != '') ? the_field('product_size') . '<br>' : ''; ?>
<?php echo (the_field('product_size2') != '') ? the_field('product_size2') . '<br>' : ''; ?>
<?php echo (the_field('product_size3') != '') ? the_field('product_size3') . '<br>' : ''; ?>

Related

How to select the end of a dynamically-generated array and hide part of its content?

everyone!
My website has a list of office contacts being dynamically generated via PHP (partial snippet below). As you can see, each list result/child has a name, address and, on a following div, a google maps frame.
What I'd like to achieve is to call the latest result of the array and have that one NOT TO display the google maps div.
I think I can probably call it with the "end()" command, but I'm not being able to wrap my head around the right way to proceed... Can anyone point me in the right direction, please?
Thank you!
<header id="standardheader">
<div class="wrap">
<nav class="standardsubnav">
<ul class="standardsubnav_list vanilla"><!--
<?php include_partial( 'contact/citynav', array('class' => 'standardsubnav') ); ?>
--></ul>
</nav>
<h1 class="standardpage_title"><?php echo $current->getHeadline(); ?></h1>
</div>
</header>
<div class="wrap">
<section class="">
<ul class="list4 vanilla">
<?php $contacts = $current->getChildren(); ?>
<?php foreach($contacts as $contact): $settings = $contact->getSettings(); ?>
<li class="item4">
<a name="<?php echo $settings['city']; ?>"></a>
<div class="link4">
<div class="link4_inner">
<h2 class="title4"><?php echo $settings['city']; ?></h2><!--
--><address class="address4">
<?php echo $settings['name']; ?><br />
<?php if(isset($settings['address1']) && $settings['address1'] != "") echo $settings['address1'] . '<br />'; ?>
<?php if(isset($settings['address2']) && $settings['address2'] != "") echo $settings['address2'] . '<br />'; ?>
<?php if(isset($settings['address3']) && $settings['address3'] != "") echo $settings['address3'] . '<br />'; ?>
<?php if(isset($settings['address4']) && $settings['address4'] != "") echo $settings['address4'] . '<br />'; ?>
<?php if(isset($settings['phone']) && $settings['phone'] != ""): ?>t. <?php echo $settings['phone']; ?><br /><?php endif; ?>
<?php if(isset($settings['fax']) && $settings['fax'] != ""): ?>f. <?php echo $settings['fax']; ?><br /><?php endif; ?>
<?php if(isset($settings['email']) && $settings['email'] != ""): ?>e. <?php echo $settings['email']; ?><?php endif; ?>
</address>
</div><!--
--><div class="link4_inner">
<?php
$mapurl = (!empty($settings['mapurl'])) ? $settings['mapurl'] : 'https://www.google.com/maps/#' . $settings['latitude'] . ',' . $settings['longitude'] . ',' . $settings['zoom'] . 'z'; ?>
<div class="gmap4" data-gmap="<?php echo htmlspecialchars( '{"lat":"'.$settings['latitude'].'","lng":"'.$settings['longitude'].'", "zoom":"'.$settings['zoom'].'", "mapurl":"'.$mapurl.'"}' ); ?>">
<noscript><?php echo __('Please enable javascript to see the map.'); ?></noscript>
<?php end($contacts)
Count the $contacts array. Then in the foreach loop check if the element beeing currently in the loop is equals the items in the array, if it's equal to the items, it's the last element, then exit the foreach:
$items = count($contacts );
$i = 0;
foreach($contacts as $contact) {
if(++$i === $items ) {
continue;
}
}

Codeigniter function "from_label()" is giving error

I sent you guys a screenshot of error, the form is working properly the only issue is in "from_label()" if I comment it the input field is working properly without any error
<?php $attributes = array('id' => 'login_form','class' => 'form_horizontal'); ?>
<?php echo form_open($login_user, $attributes); ?>
<div class="from-group">
<?php echo from_label(); ?>
<?php echo from_input(); ?>
</div>
<?php echo form_close(); ?>
change
<?php echo from_label(); ?>
^^
<?php echo from_input(); ?>
^^
to
<?php echo form_label(); ?>
<?php echo form_input(); ?>
Reference
form_label([$label_text = ''[, $id = ''[, $attributes =
array()]]])
form_input([$data = ''[, $value = ''[, $extra = '']]])
FYI:
form_label() need the first argument as run smoothly. echo
form_label('What is your Name'); (even with form-input)
Even your class from-group should be form-group

PHP if function joomla no work

Hello everyone and thank you in advance that I will answer. I have this problem, I want to see the second page where I find a link that takes me home page blog if in other categories.
I developed this little code:
<div class="componentheading<?php echo $this->params->get('pageclass_sfx')?>">
<?php $link_address = "http://testjoomla0315.altervista.org/index.php?option=com_k2&view=itemlist&layout=category&Itemid=53" ?>
<?php $homeblog = "Blog" ?>
<?php $doc = JFactory::getDocument(); ?>
<?php $page_title = $doc->getTitle(); ?>
<?php echo $page_title; ?>
<?php if ($page_title != $homeblog) ?>
<?php echo "<a href='".$link_address."'>Home Blog</a>"; ?>
<?php endif; ?>
<?php echo $this->escape($this->params->get('page_title')); ?>
Unfortunately although not error the result is the same, or as if what I wrote was invisible. In homepage obviously sees the home page link (and that is the rule I)
Your code is missing a colon at the end of if statement.
Change
if ($page_title != $homeblog)
To
if ($page_title != $homeblog):
Also dont use <?php and ?> every line. You can have the code like this
<div class="componentheading<?php echo $this->params->get('pageclass_sfx')?>">
<?php
$link_address = "http://testjoomla0315.altervista.org/index.php?option=com_k2&view=itemlist&layout=category&Itemid=53";
$homeblog = "Blog";
$doc = JFactory::getDocument();
$page_title = $doc->getTitle();
echo $page_title;
if ($page_title != $homeblog):
echo "<a href='".$link_address."'>Home Blog</a>";
endif;
echo $this->escape($this->params->get('page_title'));
?>

how to make navigation items display the correct active class with php

I am building a CMS and I have problems with my navigation. I want to add the class active to the active item in the navigation. This would work fine if I had a page for every item, but I have other sections and links
Here is my code:
<div id="main-menu">
<?php foreach ($items as $item) : ?>
<?php $active = ''; ?>
<?php if (isset($page)) : ?>
<?php ($item->pageId === $page->id) ? $active = 'active' : $active = ''; ?>
<?php endif; ?>
<?php if ($item->link == '') : ?>
<?php echo $item->label; ?>
<?php else : ?>
<?php echo $item->label; ?>
<?php endif; ?>
<?php endforeach; ?>
When I do not have a page, then I give the link the value provided to point to the specific section. How can I make them all to work fine?
You don't need that many <?php ?> tags! Anyway, your problem seem to be an incorrect usage of the ternary operator. Try this:
<?php
foreach ($items as $item) :
$active = '';
if (isset($page)) :
$active = ($item->pageId === $page->id) ? 'active' : '';
endif;
if ($item->link == '') :
?>
<?php echo $item->label; ?>
<?php else : ?>
<?php echo $item->label; ?>
<?php
endif;
endforeach;
?>
Here's how to do it with only one pair of PHP tags:
<?php
foreach($items as $item)
{
$active = ($item->pageId == $page->id) ? 'active' : '';
if(!$item->link)
{
echo '' . $item->label . '';
}
else
{
echo '' . $item->label . '';
}
}
?>

PHP keeping $variable outside scope of if statement

I've picked up a project from another developer and I'm trying to finish and clean up a bit. This part of the code renders products from a database after checking for a language cookie:
<?php if (get_cookie('spanish')) : ?>
<?php if ($product['details_spanish'] != '') : ?>
<?php $details = explode(':',$product['details_spanish']); ?>
<h3>Especificaciones</h3>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Especificaciones</h3>
<?php endif ?>
<?php endif; ?>
<?php else : ?>
<?php if ($product['details'] != '') : ?>
<?php $details = explode(':',$product['details']); ?>
<h3>Specifications</h3>
<?php endif; ?>
<?php endif; ?>
<ul>
<?php if ($details) : ?>
<?php foreach ($details as $detail) : ?>
<?php $detail = split(',',$detail); ?>
<?php if ($detail[0] != '' && $detail[1] != '') : ?>
<li>
<strong><?=ucwords($detail[0])?></strong> : <?=$detail[1]?>
</li>
<?php endif; ?>
<?php $i++; ?>
<?php endforeach; ?>
<?php endif; ?>
Sorry, I know this is hard to read due to inconsistency and bad coding (not my project initially. What it's doing is checking for a spanish cookie on the users machine. If it finds it, it pulls the values from the spanish column of the table instead of the regular. It falls back on the english value if there is no spanish value in the table. Also, it creates a $details variable that contains one of the values of the database, and splits it where the : is.
The problem I'm having is on some products, it gives me an error saying it can't find the $details variable, I'm assuming it is because the variable doesn't live outside the scope of the first if statement?
I'm a php noob really but even I know this isn't good practice. is there a better way to clean this up and have my $detail variable available?
Quick workaround is to pre-initialize that variable at the very top:
<?php $details = ''; ?>

Categories