Jump to content

php function to "print" the whole navbar

Go to solution Solved by ¨TrisT¨,

Done it this way:

 

Current page that is being executed is in need of a navigation bar - calls the functionFile.php by: 

include("relativeDirectory/functionFile.php");

The function inside functionFile.php (among many other functions, just so you don't think I did this with the sole purpose of showing the navigation bar) prints out the page that creates the navbar(in this case navbarPage.php) to the page where it is executed. It also has an argument that says which one of the buttons on the navbar is selected.

function printnbar($selected){  include 'navbarPage';}

and in the navbarPage.php page I did something like:

<body><style style type = "text/css" scoped>   .buttondefault{     unselected button stuff}   .buttonselected{     selected button stuff}</style><?php$sel1 = $sel2 = $sel3 = "class='buttondefault'";switch ($selected) {    case "NavButton1":        $sel1 = "class='buttonselected'";        break;    case "NavButton2":        $sel2 = "class='buttonselected'";        break;    case "NavButton3":        $sel3 = "class='buttonselected'";        break;}?><divs,dropdowns,whatever>  <button <?php echo $sel1; ?>>YourButton1</button>  <button <?php echo $sel2; ?>>YourButton2</button>  <button <?php echo $sel3; ?>>YourButton3</button></divs,dropdowns,whatever></body>

And there it is! I can print out a navigation bar in any page with the button I want selected just by typing:

printnbar("ButtonOnTheNavbar");
 
I didn't copy paste this so it might have some errors.

Hope this may help someone in the future! And thanks to everyone who helped me achieve this.

 

Tell me if you have a better way!

Hey!

Say I have lots of pages, with repeated code and such, but I want to make stuff easier to change in the future, so, I make this other page that is all functions, so when something gets repetitive between pages, I simply include that page and use the functions. Now don't "say", I actually do it. This time, I coded myself a shiny navbar, with the css and html, but I have got to put it in basically every other page on the website, so, I thought why not do the same thing!

 

But my question is: Should I get php to echo every one of those ever-growing now 40 lines(I know I can stuff it all in one)? As much as it may seem a good idea, I still have something in the back of my mind asking me how retarded I am for even thinking about doing that. And I really don't feel like copy pasting all that over and over again. I would also greatly appreciate any other idea or way of taking care of this matter.

 

Thanks in advance!

Link to comment
Share on other sites

Link to post
Share on other sites

echo file.php I think

or get_template_part( 'file' );

Thats that. If you need to get in touch chances are you can find someone that knows me that can get in touch.

Link to comment
Share on other sites

Link to post
Share on other sites

include 'file.php';

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Also the file.php @Hazy125 mentioned does not have to be one long echo statement. You can write the actual html markup and either include that as is, if your content is static for a nav bar I'd assume most of it would be or with <?= "hello"; ?> little php echo statements in there :)

Link to comment
Share on other sites

Link to post
Share on other sites

Done it this way:

 

Current page that is being executed is in need of a navigation bar - calls the functionFile.php by: 

include("relativeDirectory/functionFile.php");

The function inside functionFile.php (among many other functions, just so you don't think I did this with the sole purpose of showing the navigation bar) prints out the page that creates the navbar(in this case navbarPage.php) to the page where it is executed. It also has an argument that says which one of the buttons on the navbar is selected.

function printnbar($selected){  include 'navbarPage';}

and in the navbarPage.php page I did something like:

<body><style style type = "text/css" scoped>   .buttondefault{     unselected button stuff}   .buttonselected{     selected button stuff}</style><?php$sel1 = $sel2 = $sel3 = "class='buttondefault'";switch ($selected) {    case "NavButton1":        $sel1 = "class='buttonselected'";        break;    case "NavButton2":        $sel2 = "class='buttonselected'";        break;    case "NavButton3":        $sel3 = "class='buttonselected'";        break;}?><divs,dropdowns,whatever>  <button <?php echo $sel1; ?>>YourButton1</button>  <button <?php echo $sel2; ?>>YourButton2</button>  <button <?php echo $sel3; ?>>YourButton3</button></divs,dropdowns,whatever></body>

And there it is! I can print out a navigation bar in any page with the button I want selected just by typing:

printnbar("ButtonOnTheNavbar");
 
I didn't copy paste this so it might have some errors.

Hope this may help someone in the future! And thanks to everyone who helped me achieve this.

 

Tell me if you have a better way!

Link to comment
Share on other sites

Link to post
Share on other sites

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

×