Jump to content

Recommended Posts

Posted

I'm porting several constant elements of a website to PHP so that I don't need to hard code them on every page..

Header, footer, sitenav, etc.

 

Typically the hard coded HTML would be:

</pre><li class="currentpage">Page1</li><br><li>Page2</li><br><li>Page3</li><br>... /d

 

with CSS defining

.currentpage {
font-weight: bold;
}

 

Is there a way to modify which page is .currentpage via the PHP?

 

I found this StackOverflow page about PHP if statements, but I've found no way to really implement them into the PHP file.

 

        if (strpos($url,\'debug-index.php\') == true) {
           echo \'</pre><li class="currentpage">Page1</li>\';<br>       } else {<br>           echo \'<li>Page1</li>\'

doesn't work, so I thought, oh, OK, well maybe I can define it further up?

 

Setting

        if (strpos($url,'debug-index.php') == true) {
           $pagelink = '</pre><li class="currentpage">Page1</li>';<br>       } else {<br>           $pagelink = '<li>Page1</li>'

and then including

        
           《?php echo $pagelink;>
       

further down.. Still no dice.

(《 ought to be < but I think EduGeek thinks I'm writing an attack if I use that and it blocks me lol)

Posted

I don't know, that doesn't seem to work for me?

 

codepen.png

 

Also wouldn't that defeat the point of making the menu part of a PHP require? I'm trying to not need to go to every page and update the code, page by page, whenever something changes.

  • 2 weeks later...
Posted (edited)

Err, so you want to apply the .currentpage class to the link to whichever page the user is currently on? Have you looked at the slug? You could create a variable, store some/all of the slug depending on how much of it you need and then use that in the if statement when applying the css class?

 

Edit: I'm sure someone with better knowledge than mine can clarify but the strpos() function returns a number if that substring exists and false if it doesn't, I know the number will be a truthy value but maybe you could try using != false instead:

 

if (strpos($url,\'debug-index.php\') != false) {

echo \'

  • Page1
  • \';

    } else {

    echo \'

  • Page1
  • \';

    }

    Edited by Al_Balone
    Posted

    I feel like maybe it's just a syntax issue more than an if statement issue

     

    I found a few different if statements that (according to StackOverflow people, anyway) should work, the trouble is actually getting them to.. well.. work?

     

    Where does the if statement go?

     

    If I put it in $Menu = '

    if (...)

    ';

    it doesn't work. Neither does $Menu = '

    ' + if (...) + ''';
    Posted (edited)
    If I put it in $Menu = '

    if (...)

    ';

    it doesn't work. Neither does $Menu = '

    ' + if (...) + ''';

     

    If ($something=="thing") {
    
    $menu = "this bit of string" . " this other bit of string";
    
    }
    

    . joins strings in PHP.

     

    You can even shortcut it with:

    $menu = "this bit of string";
    $menu .= " this other bit of string";

     

    Output: this bit of string this other bit of string

    Edited by ZeroHour
    Posted
    I feel like maybe it's just a syntax issue more than an if statement issue

     

    I found a few different if statements that (according to StackOverflow people, anyway) should work, the trouble is actually getting them to.. well.. work?

     

    Where does the if statement go?

     

    If I put it in $Menu = '

    if (...)

    ';

    it doesn't work. Neither does $Menu = '

    ' + if (...) + ''';

     

    Thats Java Syntax :)

     

    PHP uses dots echo "hello" . "world"

    Posted

    Slightly embarrassing to admit, but it has been explained to me over Discord and I was fundamentally misunderstanding how PHP worked.

     

    I didn't realise you could just put raw code into an include/require.php with snippets of <?php [...] ?> where you wanted programming. I was operating under the assumption that everything must be var'd and echo'd :doh:

     

    Every day's a learning day!

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now



    ×
    ×
    • Create New...