Garacesh Posted April 25, 2024 Posted April 25, 2024 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)
Garacesh Posted April 25, 2024 Author Posted April 25, 2024 I don't know, that doesn't seem to work for me? 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.
adamsund Posted April 25, 2024 Posted April 25, 2024 That code was to show you what you need to add, it won't work on it's own. You could try something like this but otherwise you will have to make some coding changes for what you want to achieve I think. Get only filename from url in php without any variable values which exist in the url - Stack Overflow
ZeroHour Posted April 25, 2024 Posted April 25, 2024 (《 ought to be < but I think EduGeek thinks I'm writing an attack if I use that and it blocks me lol) Looking into this.
ZeroHour Posted April 25, 2024 Posted April 25, 2024 I use things like: https://onlinephp.io/ for a quick test as well. (Hopefully the block should be fixed now)
Al_Balone Posted May 7, 2024 Posted May 7, 2024 (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 May 7, 2024 by Al_Balone
Garacesh Posted May 9, 2024 Author Posted May 9, 2024 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 (...) + ''';
ZeroHour Posted May 9, 2024 Posted May 9, 2024 (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 May 9, 2024 by ZeroHour
supportman Posted May 10, 2024 Posted May 10, 2024 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"
Garacesh Posted May 10, 2024 Author Posted May 10, 2024 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 Every day's a learning day!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now