3

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.

Matt
  • 77
  • 7
  • 1
    That's impossible, given the code you've posted. There is **no way** to "screw up" an `if` statement such that both branches are evaluated. It's **fundamentally** impossible for PHP to do this, there is no mistake you can possibly make that would produce this behaviour. You're misinterpreting your error. – user229044 Oct 04 '13 at 15:37
  • 2
    Hm it seems you are using Smarty or something like it. In this case you should use their if else statement instead clean php – bksi Oct 04 '13 at 15:38
  • I know is fundamentally impossible which is why it is confusing me so much. It must be something to do with pmWiki. – Matt Oct 04 '13 at 15:54

4 Answers4

1

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

bksi
  • 1,606
  • 1
  • 23
  • 45
  • Hi, It does not seem to work. I am getting the following in the web browser: http://imgur.com/Pbrz0wc – Matt Oct 04 '13 at 15:53
  • Does the template engine 'TinyButStrong' mean anything? – Matt Oct 04 '13 at 15:56
  • It may be not smarty. I can't tell anything until you don't give the rest of the code (the controller for example) – bksi Oct 04 '13 at 16:07
  • I tried it with the pmWiki syntax as above however it is still placing the if statement onto the web page and showing it in the html when inspecting element in browser. Is there something stopping it from executing it? I have placed the code in the DIV tag as above, I have also tried placing this into a seperate PHP file and including this however the include seems to be commented out when inspecting in browser? I have pasted the main pmwiki.php into pastebin if this helps: http://pastebin.com/aU1Q548z – Matt Oct 07 '13 at 08:37
  • 1
    I have managed to work it out, conditional statements can only be used on the Wiki pages not in the templates. I had to create a 'Site.AllGroupHeader' and then set the $GroupHeaderFmt variable. http://www.pmwiki.org/wiki/Cookbook/AllGroupHeader Thanks all for the help. – Matt Oct 07 '13 at 15:57
-1

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>
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
-1

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

Fabio
  • 791
  • 1
  • 7
  • 27
-1
<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>
user2320601
  • 162
  • 2