July 21, 201115 yr Using php includes to include different sidebars for different pages etc. Now I have run in to a problem. I would like to use the same sidebar for, say, 10 pages of the site, BUT each page needs to have a certain area of the sidebar that is different. How would I do this? I know I could just create 10 sidebars but sure there is a 'proper' way to do it lol. If/else? Unsure how this works though.
July 21, 201115 yr cant you just include the defaults then do something conditional based on url: require_once($_SERVER["DOCUMENT_ROOT"] . "/toolbar/survey.inc"); require_once($_SERVER["DOCUMENT_ROOT"] . "/toolbar/calendar.inc"); require_once($_SERVER["DOCUMENT_ROOT"] . "/toolbar/news.inc"); if($_SERVER["REQUEST_URI"] == "http://mysite.com/test.html"){ require_once($_SERVER["DOCUMENT_ROOT"] . "/toolbar/test.inc"); } obviously that would probably be better done in a switch if you have 10 of them. Not sure if there is any better way of doing this but i'm no expert!
July 21, 201115 yr Right i'm sure their are better ways to do this but depending on the project size i would do this... Add.. <?php $thisPage="PAGE NAME"; ?> at the very top of each page (before any header includes or anything else, change 'PAGE NAME' to something that will identify the page such as 'home'. You can then target this with an if/else statement. such as... <?php if ($thisPage=="PAGE NAME") ?> <div> Sidebar stuff for home page etc </div> <?php ; ?> but like i said, their is probably a much better method for this. I usually use this for targeting the current page in menu's etc. You can also then use this variable for other things such as inserting in to links, title tags etc. Edited July 21, 201115 yr by MikeChipshop
July 21, 201115 yr Author Thanks guys. Mike, your example, I cannot get it working :\. My include files are html extensions, would this be the issue?
July 21, 201115 yr Thanks guys. Mike, your example, I cannot get it working :\. My include files are html extensions, would this be the issue? Yeah. They'd have to be .php extensions to run the containing PHP scripts. Just change the extension.
July 21, 201115 yr Erm, not 100% sure. But thing to do is, put all the html inside sidebar.php, then at the top of your sidebar.php file put ?> then at the bottom put <?php I think it may be trying to just insert the html while still inside php tags. Then use include('sidebar.php'); EDIT: Ah there we go ^^ Edited July 21, 201115 yr by Alias
July 22, 201115 yr Author Hmmmphh!!! I have, at present, two pages I am trying to implement this on. I have used php to give these pages names, see below:- <?php $thisPage="LBC"; ?> <?php $thisPage="CECIL"; ?> Now in my sidebar.php file I have included the following:- <?php if ($thisPage=="LBC") ?> example code <?php ; ?> <?php if ($thisPage=="CECIL") ?> example code <?php ; ?> BUT, why do both sets of info appear on both pages??
August 3, 201115 yr Author Right i'm sure their are better ways to do this but depending on the project size i would do this... Add.. <?php $thisPage="PAGE NAME"; ?> at the very top of each page (before any header includes or anything else, change 'PAGE NAME' to something that will identify the page such as 'home'. You can then target this with an if/else statement. such as... <?php if ($thisPage=="PAGE NAME") ?> <div> Sidebar stuff for home page etc </div> <?php ; ?> but like i said, their is probably a much better method for this. I usually use this for targeting the current page in menu's etc. You can also then use this variable for other things such as inserting in to links, title tags etc. Is it possible to assign two pages id's to the above? I have tried ... <?php if ($thisPage=="PAGE NAME") > <?php if ($thisPage=="OTHER PAGE NAME") ?> , <?php if ($thisPage=="PAGE NAME, OTHER PAGE NAME") ?> Edited August 3, 201115 yr by roothost
August 4, 201115 yr Is it possible to assign two pages id's to the above? I have tried ... <?php if ($thisPage=="PAGE NAME") > <?php if ($thisPage=="OTHER PAGE NAME") ?> , <?php if ($thisPage=="PAGE NAME, OTHER PAGE NAME") ?> although i am still learning php i guess that would be <?php if ( $thisPage=="PAGE NAME" || "OTHER PAGE NAME" ) { // stuff goes here } ?> if i am wrong please correct me anyone Edited August 4, 201115 yr by sash_oo7
August 4, 201115 yr Author Nope, thats not working boys <?php if ( $thisPage=="PORTFOLIO" || "BIBBYS" ) { <img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio"> } ?>
August 4, 201115 yr This may be of some help: http://www.php.net/manual/en/function.pathinfo.php It can be very handy to do this sort of thing dynamically (to an extent) providing you are using a file and directory structure and not a single page with dynamic content (i.e. index.php?page=about) although you can still handle this in a similar way using $_GET['page'] for example. You can set the file name by checking that you are in a directory, get the directory name and check that you are in index.php (/home/user/public_html/about_me/index.php). With pathinfo you can get the directory path (/home/user/public_html/about_me/) then expload at the /, get the last array value and thats the current directory. The use pathinfo to get the filename to check your in index.php (or any other file you want). This is also great for breadcrumb systems.
August 4, 201115 yr Nope, thats not working boys <?php if ( $thisPage=="PORTFOLIO" || "BIBBYS" ) { <img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio"> } ?> it should be: <?php if ( $thisPage == "PORTFOLIO" || $thisPage == "BIBBYS" ) { echo '<img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio">'; } ?> a worthy addition is to check that $thisPage is set and if not then add a fallback: <?php if(isset($thisPage)){ if ($thisPage == "PORTFOLIO" || $thisPage == "BIBBYS" ) { echo '<img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio">'; }elseif($thisPage == "RAWR"){ echo 'other content'; } }else{ echo 'default output, $thisPage is not set.'; } ?>
August 4, 201115 yr Author it should be: <?php if ( $thisPage == "PORTFOLIO" || $thisPage == "BIBBYS" ) { echo '<img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio">'; } ?> a worthy addition is to check that $thisPage is set and if not then add a fallback: <?php if(isset($thisPage)){ if ($thisPage == "PORTFOLIO" || $thisPage == "BIBBYS" ) { echo '<img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio">'; }elseif($thisPage == "RAWR"){ echo 'other content'; } }else{ echo 'default output, $thisPage is not set.'; } ?> The above works on the first use of that include but not for the second...
August 4, 201115 yr Author I'm going back to bed......................maybe I might wake up with some common sense!
August 4, 201115 yr Author Yeah its all working now Rob, kind of helps if you actually 'ask' the page to include it doesn't it lool
August 16, 201115 yr Author Hmmmm, would there be a way to reverse this? So it hides/doesn't show specific content on a set page?
August 16, 201115 yr Hmmmm, would there be a way to reverse this? So it hides/doesn't show specific content on a set page? if you dont want to show that message on a certain page just put another check around that message as an if statement, just like the one its in
August 16, 201115 yr Author if you dont want to show that message on a certain page just put another check around that message as an if statement, just like the one its in Not entirely sure I'm following you...
August 16, 201115 yr Not entirely sure I'm following you... <?php if(isset($thisPage)){ if ($thisPage == "PORTFOLIO" || $thisPage == "BIBBYS" ) { echo '<img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio">'; }elseif($thisPage == "RAWR"){ echo 'other content'; } }else{ if ($thisPage == "thiscertainpage") { echo 'default output, $thisPage is not set.'; } } ?> i dont know if that code originally worked but you see what i mean? i think thats what you meant lol if not explain further
August 16, 201115 yr ahh no, i take that back, that wont work I dont quite get what you mean now i come to think of it, i didnt realise all of that was in another if statement already Edited August 16, 201115 yr by web-itec
August 16, 201115 yr Author <?php if(isset($thisPage)){ if ($thisPage == "PORTFOLIO" || $thisPage == "BIBBYS" ) { echo '<img src="http://www.roothost.co/assets/img/logos-banners/portfolio-topper.gif" alt="roothost web design portfolio" title="roothost web design portfolio">'; }elseif($thisPage == "RAWR"){ echo 'other content'; } }else{ if ($thisPage == "thiscertainpage") { echo 'default output, $thisPage is not set.'; } } ?> i dont know if that code originally worked but you see what i mean? i think thats what you meant lol if not explain further Sort of lol Let's say for instance I have an include file that gets included on every page, the contents are <div id="content-a">blah de blah</div> <div id="content-b">blah de blah</div> Now, both those div's will display on any page that I insert the include, now let's say I have a page where, for some reason, I only want #content-b displayed, how would I cancel out #content-a? Hope that explains it:)
August 16, 201115 yr Anybody have a suggestion? if ($thisPage != "thisCertainPageYoudDontWantTheIncludeToShowOn") { include("file.php"); } so if i put the page was home and i changed thisCertainPageYoudDontWantTheIncludeToShowOn to home it wouldnt show that included file Edited August 16, 201115 yr by web-itec
August 16, 201115 yr Author if ($thisPage != "thisCertainPageYoudDontWantTheIncludeToShowOn") { include("file.php"); } so if i put the page was home and i changed thisCertainPageYoudDontWantTheIncludeToShowOn to home it wouldnt show that included file Ok that works if I just wish to exclude it from one page, but when I try to exclude from multiple pages it just sticks it back in again. Here is how I tried it:- <?php if ($thisPage !="SERVICES" || $thisPage !="WEB DESIGN") { ?> <div class="dropdown"><h3>random info here</h3> <?php ; } ?> Edited August 16, 201115 yr by roothost
August 16, 201115 yr Ok that works if I just wish to exclude it from one page, but when I try to exclude from multiple pages it just sticks it back in again. Here is how I tried it:- <?php if ($thisPage !="SERVICES" || $thisPage !="WEB DESIGN") { ?> <div class="dropdown"><h3>random info here</h3> <?php ; } ?> <?php if(($thisPage !="SERVICES") || ($thisPage !="WEB DESIGN")) { echo '<div class="dropdown"><h3>random info here</h3></div>'; } ?> if you have 2 totally seperate statements you need to put them in there own brackets and then in extra brackets to show the OR of the 2 statements that should work Edited August 16, 201115 yr by web-itec
August 16, 201115 yr Author Grrr, that's not working lol Here is the complete part I am trying to exclude from certain pages <?php if(($thisPage !="SERVICES") || ($thisPage !="WEB DESIGN")) { echo '<div class="dropdown"><h3><a href="#" title="some of the services i offer">Some Of The Services I Offer</a></h3></div><!-- /.dropdown --> <div class="dropdown_container"> <div class="block"> <div class="dropdown-services"> <div class="dropdown-services-image"><img src="http://www.roothost.co/assets/img/dropdown-services/web-design.gif" alt="web design" title="web design"></div><!-- /.example-services-image --> <div class="dropdown-services-title"><h3>web design</h3></div><!-- /.dropdown-services-title --> <div class="dropdown-services-desc"> <p><a href="http://www.roothost.co/website-design/" title="professional web design">Professional web design</a> services in the Lewes, East Sussex area.</p> <p><abbr title="world wide web consortium">W3C</abbr> standards compliant, <strong>cross-browser websites</strong> created using the latest website design techniques giving your site maximum search engine value.</p> </div><!-- /.dropdown-services-desc --> </div><!-- /.dropdown-services --> <div class="dropdown-services-middle"> <div class="dropdown-services-image"><img src="http://www.roothost.co/assets/img/dropdown-services/ecommerce.gif" alt="ecommerce web design" title="ecommerce website design"></div><!-- /.dropdown-services-image --> <div class="dropdown-services-title"><h3>ecommerce websites</h3></div><!-- /.dropdown-services-title --> <div class="dropdown-services-desc"> <p>Development of <a href="http://www.roothost.co/ecommerce-web-design/" title="ecommerce websites">ecommerce websites</a> and online shops in the Lewes, East Sussex area.</p> <p>With a tailor made <strong>ecommerce website</strong> you are in the best position to sell your products and services to the world! Compatible with all major payment gateways.</p> </div><!-- /.dropdown-services-desc --> </div><!-- /.dropdown-services-middle --> <div class="dropdown-services"> <div class="dropdown-services-image"><img src="http://www.roothost.co/assets/img/dropdown-services/wordpress.gif" alt="wordpress web design" title="wordpress web design"></div><!-- /.dropdown-services-image --> <div class="dropdown-services-title"><h3>wordpress web design</h3></div><!-- /.dropdown-services-title --> <div class="dropdown-services-desc"><p><a href="http://www.roothost.co/wordpress-web-design/" title="wordpress web design">WordPress web design</a> services in the Lewes, East Sussex area.</p> <p>Creative, search engine friendly, custom-built <strong>WordPress themes developed</strong> as a powerful content management system (<abbr title="content management system">CMS</abbr>) and blogging platform.</p> </div><!-- /.dropdown-services-desc --> </div><!-- /.dropdown-services --> </div><!-- /.block --> </div><!-- /.dropdown_container --> <div class="dropdown-base"></div><!-- /.dropdown-base -->'; } ?> <div class="line-break-no-top-margin"></div><!-- /.line-break --> Both the pages called have this at the very top <?php $thisPage="SERVICES"; ?> or <?php $thisPage="WEB DESIGN"; ?>
August 16, 201115 yr Grrr, that's not working lol Here is the complete part I am trying to exclude from certain pages <?php if(($thisPage !="SERVICES") || ($thisPage !="WEB DESIGN")) { echo '<div class="dropdown"><h3><a href="#" title="some of the services i offer">Some Of The Services I Offer</a></h3></div><!-- /.dropdown --> <div class="dropdown_container"> <div class="block"> <div class="dropdown-services"> <div class="dropdown-services-image"><img src="http://www.roothost.co/assets/img/dropdown-services/web-design.gif" alt="web design" title="web design"></div><!-- /.example-services-image --> <div class="dropdown-services-title"><h3>web design</h3></div><!-- /.dropdown-services-title --> <div class="dropdown-services-desc"> <p><a href="http://www.roothost.co/website-design/" title="professional web design">Professional web design</a> services in the Lewes, East Sussex area.</p> <p><abbr title="world wide web consortium">W3C</abbr> standards compliant, <strong>cross-browser websites</strong> created using the latest website design techniques giving your site maximum search engine value.</p> </div><!-- /.dropdown-services-desc --> </div><!-- /.dropdown-services --> <div class="dropdown-services-middle"> <div class="dropdown-services-image"><img src="http://www.roothost.co/assets/img/dropdown-services/ecommerce.gif" alt="ecommerce web design" title="ecommerce website design"></div><!-- /.dropdown-services-image --> <div class="dropdown-services-title"><h3>ecommerce websites</h3></div><!-- /.dropdown-services-title --> <div class="dropdown-services-desc"> <p>Development of <a href="http://www.roothost.co/ecommerce-web-design/" title="ecommerce websites">ecommerce websites</a> and online shops in the Lewes, East Sussex area.</p> <p>With a tailor made <strong>ecommerce website</strong> you are in the best position to sell your products and services to the world! Compatible with all major payment gateways.</p> </div><!-- /.dropdown-services-desc --> </div><!-- /.dropdown-services-middle --> <div class="dropdown-services"> <div class="dropdown-services-image"><img src="http://www.roothost.co/assets/img/dropdown-services/wordpress.gif" alt="wordpress web design" title="wordpress web design"></div><!-- /.dropdown-services-image --> <div class="dropdown-services-title"><h3>wordpress web design</h3></div><!-- /.dropdown-services-title --> <div class="dropdown-services-desc"><p><a href="http://www.roothost.co/wordpress-web-design/" title="wordpress web design">WordPress web design</a> services in the Lewes, East Sussex area.</p> <p>Creative, search engine friendly, custom-built <strong>WordPress themes developed</strong> as a powerful content management system (<abbr title="content management system">CMS</abbr>) and blogging platform.</p> </div><!-- /.dropdown-services-desc --> </div><!-- /.dropdown-services --> </div><!-- /.block --> </div><!-- /.dropdown_container --> <div class="dropdown-base"></div><!-- /.dropdown-base -->'; } ?> <div class="line-break-no-top-margin"></div><!-- /.line-break --> Both the pages called have this at the very top <?php $thisPage="SERVICES"; ?> or <?php $thisPage="WEB DESIGN"; ?> My bad, need to use the && not ||
Create an account or sign in to comment