Forum

Thread tagged as: Question

PHP help: echo if exists

I have the following heading code on my results page for a filterable form.

<h1 class="smallh1">Search results:
<span class="pink"><?php echo perch_get('type'); ?></span> 
<span class="pink"><?php echo perch_get('location'); ?></span>
</h1>

What code would I use If the user does not select anything in the 'type' select box. Ideally I would want it to echo "Any type in Location name"

How can I make it echo the words Any Type if nothing is selected .

Any help is appreciated.

James Tedder

James Tedder 0 points

  • 5 years ago
Simon Clay

Simon Clay 127 points

Hi, I think you could use something like:

<h1 class="smallh1">Search results:

    <?php if (perch_get('type')) { ?>
        <span class="pink"><?php echo perch_get('type'); ?></span> 

    <?php } elseif (perch_get('location')) { ?>
        <span class="pink"><?php echo perch_get('location'); ?></span> 

    <?php } else {
        <span class="pink">Any Type</span>
    } ?>
</h1>

Thanks Simon,

This throws up the following error though

Parse error: syntax error, unexpected '<' in /home/design/public_html/eoyd/results.php on line 19
Simon Clay

Simon Clay 127 points

Hi, I missed a php tag. I think this should do it:

<h1 class="smallh1">Search results:  

    <?php if (perch_get('type')) { ?> 
        <span class="pink"><?php echo perch_get('type'); ?></span>   

    <?php } elseif (perch_get('location')) { ?> 
        <span class="pink"><?php echo perch_get('location'); ?></span>   

    <?php } else { ?> 
        <span class="pink">Any Type</span> 
    <?php } ?> 
</h1>

Perfect, thanks Simons.

This is the code I have ended up using based on your initial code:

<h1 class="smallh1">Search results:   

          <?php if (perch_get('type')) { ?>  
               <span class="pink"><?php echo perch_get('type'); ?></span>

               <?php } else { ?>  
               <span class="pink">All Businesses</span>  <?php } ?>

               <?php if (perch_get('location')) { ?>  
               in <span class="pink"><?php echo perch_get('location'); ?></span>

               <?php } else { ?>  
               in <span class="pink">All locations</span>  <?php } ?>  

               </h1>