If else statement returning both instead of one?

214 Views Asked by At

This is my first PHP if statement so bear with me if I have made a silly mistake!

I am running pmWiki and have a two variables for the Group names. $Group is the group name without spaces (EasyCatalog for example) and $Groupspace is the group name with spaces (Easy Catalog for example).

I want to check if $Groupspaced == "Easy Catalog", if true then return the $Group variable, else return $Groupspaced

This is my code:

            <p class="grouptitle">
                <?php if ($Groupspaced == "Easy Catalog") : ?>
                    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Group}</a>
                <?php else : ?>
                    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Groupspaced}</a>
                <?php endif; ?>
            </p>

The problem I am having is that it is returning both links not one.

4

There are 4 best solutions below

5
bksi On BEST ANSWER

It seems you are using Smarty: use its syntax for if else:

{if $Groupspaced eq 'Easy Catalog'}
    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Group}</a>
{else}
    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Groupspaced}</a>
{/if}

More you can read at http://www.smarty.net/docsv2/en/language.function.if.tpl

I see that this is not a smarty: Here is pmWiki if else syntax:

(:if cond param:) body (:else:) body (:ifend:)

In your case the code should be:

(:if equal "{$Groupspaced}" "Easy Catalog":)
     <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Group}</a>
(:else:)
    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Group}</a>
(:ifend:)

I got this from here: http://www.pmwiki.org/wiki/Cookbook/ConditionalMarkupSamples

0
Mehdi Karamosly On

Try this to make sure that your theory is wrong, then fix your issues:

<p class="grouptitle">
    <?php 
    $Groupspaced = "Easy Catalog2";
    if ($Groupspaced == "Easy Catalog") : ?>
    <a href='blablabla1' class="pagegroup">Group 1</a>
    <?php else : ?>
    <a href='blablabla2' class="pagegroup">Group spaced 2</a>
    <?php endif; ?>
</p>

Now since you are using smarty template this is the correct smarty if/else syntax :

<p class="grouptitle">

    {if $Groupspaced eq "Easy Catalog"}
    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">Group 1</a>
    {else}
    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">Group spaced 2</a>
    {/if}
</p>
1
Fabio On

Your code seems right to me, but i personally don't like to use if statements like that, you may want to give a try on this:

 <p class="grouptitle">
                <?php if ($Groupspaced == "Easy Catalog") { ?>
                    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Group}</a>
                <?php } else { ?>
                    <a href='{$ScriptUrl}/{$Group}' class="pagegroup">{$Groupspaced}</a>
                <?php } ?>
            </p>

Hope it work's

0
user2320601 On
<p class="grouptitle">
                <?php 
                    if ($Groupspaced == "Easy Catalog")
                    {
                        echo " <a href='{$ScriptUrl}/{$Group}' class='pagegroup'>{$Group}</a>";
                    }
                    else{
                        echo " <a href='{$ScriptUrl}/{$Group}' class='pagegroup'>{$Groupspaced}</a>";
                    }
                   ?>
</p>