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
  • 7
    You need to add break after every case Commented Aug 14, 2017 at 11:21
  • RTM. Commented Aug 14, 2017 at 11:23
  • Instead, put the break before the next case... Commented Aug 14, 2017 at 11:24
  • Suggest you go check php.net/manual/en/language.basic-syntax.phpmode.php as well, so that you don’t have to echo all this static stuff ... Commented Aug 14, 2017 at 11:32

4 Answers 4

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;
    }   
}
Sign up to request clarification or add additional context in comments.

Comments

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).

14 Comments

Hello. I have this error, when i try to use your code. syntax error, unexpected '$secs' (T_VARIABLE), expecting '{' . Here: $secs=[ .
@გიორგიმეძველია I was missing an opening curly bracket after the function arguments, my mistake. I have updated my answer.
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?
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 <nav> and </nav> tags repeated. (I'm assuming you are making multiple/iterated calls to CreateNewSection -- but <nav> should only print to screen once. Have a closer look at your source code. You probably need to pull <nav> and <ul> tags out of the function.
Yes, the reason of it is i'm calling <nav> multiple times. So <nav> tag is main tag of navigation, its setting buttons design etc. So what would i do, to just call <nav> one time and then just add sections?
|
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;
    }   
}

Comments

0

A switch statement is a form of looping, and in order to perform the loop successfully, you need to add a break statement at the end of each case statement block. The syntax looks like this

switch($param){

    case $value1: // executes if $param = $value1

        //execute your code

    break; //the break statement forces the processing out of the loop after execution

    case $value2: // executes if $param = $value2

        //execute your code

    break; //the break statement forces the processing out of the loop after execution

    default: 
        //code here executes only if the case 1 and 2 is not matched

}

With this in mind, your code should now look like 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>';
    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;
}   

}

Hope this helps you out! If you have any issue, kindly comment and lets sort it out.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.