1

Today i finished creating my markup, so i wanted to move it in the php. So i have navigator.

<nav id="cd-vertical-nav">
<ul>
    <li>
        <a href="#destinations" data-number="1" class="">
            <span class="cd-dot"></span>
            <span class="cd-label">Choose Destinations</span>
        </a>
    </li>
    <li>
        <a href="#activities" data-number="2" class="">
            <span class="cd-dot"></span>
            <span class="cd-label">Activities</span>
        </a>
    </li>
    <li>
        <a href="#accommodation" data-number="3" class="">
            <span class="cd-dot"></span>
            <span class="cd-label">Accommodation</span>
        </a>
    </li>
    <li>
        <a href="#transportation" data-number="4" class="is-selected">
            <span class="cd-dot"></span>
            <span class="cd-label">Transportation</span>
        </a>
    </li>
    <li>
        <a href="#maspindzei" data-number="5" class="">
            <span class="cd-dot"></span>
            <span class="cd-label">Choose your Host</span>
        </a>
    </li>
    <li>
        <a href="#contactinfo" data-number="6" class="">
            <span class="cd-dot"></span>
            <span class="cd-label">Contact Information</span>
        </a>
    </li>
</ul>

So i need to make new function which will work like this.

CreateNewSection($secid);

I tried this code for this.

function CreateNewSection($secid)
{
    switch($secid)
    {
        case 1:
        {
            echo '<nav id="cd-vertical-nav">
        <ul>
            <li>
                <a href="#destinations" data-number="1" class="">
                    <span class="cd-dot"></span>
                    <span class="cd-label">Choose Destinations</span>
                </a>
            </li></ul></nav>';  
        }
        case 2:
        {
            echo '<nav id="cd-vertical-nav"><li><a href="#activities" data-number="2" class=""><span class="cd-dot"></span><span class="cd-label">Activities</span></a></li></li></ul></nav>';
        }
    }   
}

Problem is that, when i call ID 1, its still calling id 2 too. Its my first time, when i'm converting markup to functions like this.

4 Answers4

2

Here is how switch-case is supposed to be, every case has a break and then there is default at the end:

switch($secid)
{
    case 1:
    // code here....
    break;

    case 2:
    // code here....
    break;

    default:
    // code here....
}  

So your code shall be updated as below:

function CreateNewSection($secid)
{
    switch($secid)
    {
        case 1:
        echo '<nav id="cd-vertical-nav">
<ul>
<li>
<a href="#destinations" data-number="1" class="">
<span class="cd-dot"></span>
<span class="cd-label">Choose Destinations</span>
</a>
</li></ul></nav>';  
        break;

        case 2:
        echo '<nav id="cd-vertical-nav"><li><a href="#activities" data-number="2" class=""><span class="cd-dot"></span><span class="cd-label">Activities</span></a></li></li></ul></nav>';
        break;
    }   
}
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
1

The break is missing at the end of the switch case which is causing it to fall through.

function CreateNewSection($secid)
{
    switch($secid)
    {
        case 1:
            echo '<nav id="cd-vertical-nav">
        <ul>
            <li>
                <a href="#destinations" data-number="1" class="">
                    <span class="cd-dot"></span>
                    <span class="cd-label">Choose Destinations</span>
                </a>
            </li></ul></nav>';  
            break;
        case 2:
            echo '<nav id="cd-vertical-nav"><li><a href="#activities" data-number="2" class=""><span class="cd-dot"></span><span class="cd-label">Activities</span></a></li></li></ul></nav>';
            break;
    }   
}
Dan Hastings
  • 3,241
  • 7
  • 34
  • 71
1

My strong recommendation is to abandon the idea of a verbose switch-case block. There is a cleaner way...

Much more DRY and easier to manage will be to store the dynamic portions of your html in an array, then simply inject those values into the otherwise static html. You will thank yourself later for going this route.

function CreateNewSection($secid,$class=''){
    $secs=[
        1=>['href'=>'#destinations','label'=>'Choose Destinations'],
        2=>['href'=>'#activities','label'=>'Activities'],
        3=>['href'=>'#accommodation','label'=>'Accommodation'],
        4=>['href'=>'#transportation','label'=>'Transportation'],
        5=>['href'=>'#maspindzei','label'=>'Choose your Host'],
        6=>['href'=>'#contactinfo','label'=>'Contact Information']
    ];

    if(!isset($secs[$secid])){
        // echo some default behavior or error message
    }else{
        $sec=$secs[$secid]; // merely to shorten the variable
        echo "<li>";
            echo "<a href=\"{$sec['href']}\" data-number=\"{$secid}\" class=\"{$class}\">";
                echo "<span class=\"cd-dot\"></span>";
                echo "<span class=\"cd-label\">{$sec['label']}</span>";
            echo "</a>";
        echo "</li>";
    }
}
echo "<nav id=\"cd-vertical-nav\">";
    echo "<ul>";
        $selected=4;
        for($x=1; $x<7; ++$x){
            CreateNewSection($x,($x==$selected?'is-selected':''));
        }
    echo "</ul>";
echo "</nav>";

*note I have {} curly-bracketed all variables in the echo (even though the string variables don't require it) so that the variables stand out in your text editor (or they could if your editor is setup to do so).

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Hello. I have this error, when i try to use your code. syntax error, unexpected '$secs' (T_VARIABLE), expecting '{' . Here: $secs=[ . – გიორგი მეძველია Aug 14 '17 at 12:39
  • @გიორგიმეძველია I was missing an opening curly bracket after the function arguments, my mistake. I have updated my answer. – mickmackusa Aug 14 '17 at 12:42
  • I have one more issue while using this. Its not setting buttons vertically, id 2 is next to the id 1. How can i fix this issue? – გიორგი მეძველია Aug 14 '17 at 12:54
  • I am afraid that is beyond the scope of this question. I haven't fooled around with any of the attributes. My suspicion, though, is that you **don't** want to have the `` tags repeated. (I'm assuming you are making multiple/iterated calls to `CreateNewSection` -- but ` – mickmackusa Aug 14 '17 at 12:58
  • Yes, the reason of it is i'm calling – გიორგი მეძველია Aug 14 '17 at 13:02
  • The way the output is tabbed is a little sloppy but it should be proof enough of how it should look and feel. http://sandbox.onlinephpfunctions.com/code/e8a9885f305132d7a4c2336e5653d0a4760b5f08 – mickmackusa Aug 14 '17 at 13:07
  • @გიორგიმეძველია I've updated my code. Let me know if anything is unclear. – mickmackusa Aug 14 '17 at 13:15
  • The reason, why i want to make, function CreateNewSection is, that when you choose Destination, then you will see Activities and etc. With this logic, my TODO was: create function which will adds Sections with CreateNewSection(secid), but issue was that i was calling – გიორგი მეძველია Aug 14 '17 at 13:17
  • Okay, then the switch-case approach was going to fail you as well. Now I see that wish to create a relationship between the elements in `$secs`. The good news is my array structure will also allow you to nominate `parent->child` relationships. You can add new elements (in each subarray; after `label` element) that declare all children that should also be displayed. I cannot help you with this part because I don't know the relationships. I assure you, my method will be very helpful in your continued development. Have a try and let me know if you get stuck (i'm going to bed soon). – mickmackusa Aug 14 '17 at 13:22
  • So here is my item which user have to click – გიორგი მეძველია Aug 14 '17 at 13:27
  • http://sandbox.onlinephpfunctions.com/code/02a34d4a9acc1f4fb085130b27f586bf64e9c8c5 When user clicks ` CHOOSE DESTINATION` New section would be added. So how to make your code useful with my code? – გიორგი მეძველია Aug 14 '17 at 13:33
  • I have know idea what effect you are hoping to get when CHOOSE DESTINATION is clicked. How does this relate to `CreateNewSection()`? You are losing me, and I don't have much more time this evening. – mickmackusa Aug 14 '17 at 13:36
  • Yes, the reason of CreateNewSection was, for this. When user clicks on BUTTON, that would be reason of calling CreateNewSection. I think you understand. But i dont got. How to use your demo with this. – გიორგი მეძველია Aug 14 '17 at 13:42
  • Originally, this page was discussing server-side output. You are now thinking in terms of client-side (javascript) actions. The most that php can do to support you in this is to generate all possible Sections and have most of the `
  • `'s "hidden" until a javascript trigger changes their class/style. Please take some time to consider how this will work for you. This is now way, way outside the scope of the original question.
  • – mickmackusa Aug 14 '17 at 13:47