_Bat_ Posted February 1, 2007 Posted February 1, 2007 I've come across a problem over the past couple of days because of several scripts I want to use. These scripts unfortunately involve placing code in the head section of the page, yet they are only required on one page each. Joomla it appears only allows you to edit the head for every single webpage on the site. This is very limiting so I am sure there must be a way to add code to the head of just one page only? Does anyone know of a way of doing this, or a work around? Thanks James
Irazmus Posted February 1, 2007 Posted February 1, 2007 You could always add a conditional statement in the head of the template file. if ($_GET['option'] == 'com_contact') { ?> } ?> I use this to imbed Google Maps on the contact page without having to call an external script on every page. It may not be the most elegant solution, but it works and puts very little extra strain on the server.
_Bat_ Posted February 1, 2007 Author Posted February 1, 2007 It may not be the most elegant solution, but it works and puts very little extra strain on the server. I considered doing this, but I have about 16 or 17 pages that each need a seperate piece of code in the head. Would this still be considered to not be putting much extra strain on the server? Thats rather a lot of code when multipled by 17
_Bat_ Posted February 1, 2007 Author Posted February 1, 2007 I'll just clarify what I am trying to do. I am doing image rollovers on these 16/17 pages, all with different images. This means different code in the head for each page, mainly for the preload of the images. I don't think there is any way of avoiding putting the code into the head.
webman Posted February 1, 2007 Posted February 1, 2007 Each page you want to include as a separate file? $option = $_GET['option']; $file = "includes/$option.php"; if(file_exists($file)){ include($file); }
_Bat_ Posted February 1, 2007 Author Posted February 1, 2007 Each page you want to include as a separate file? $option = $_GET['option']; $file = "includes/$option.php"; if(file_exists($file)){ include($file); } I'm a little bit lost :?
webman Posted February 1, 2007 Posted February 1, 2007 Sorry, my mistake... didn't know exactly how the Joomla URLs worked :oops: Ok, each page has an ID, so you get the ID from the URL and then look for a file in includes/$id.php - where you make a single file for each page (id) you want that has the information you want to display. The PHP includes the right file for the page. $id = $_GET['id']; $file = "includes/$id.php"; if(file_exists($file)){ include($file); }
Irazmus Posted February 1, 2007 Posted February 1, 2007 I considered doing this, but I have about 16 or 17 pages that each need a seperate piece of code in the head. Would this still be considered to not be putting much extra strain on the server? Thats rather a lot of code when multipled by 17 The extra strain would come more from the 16 extra IF statements rather than the quantity of code, but you could get around that by using a SWITCH statement which would be more efficient. However Webman's approach is probably better, not simply for the server but also for you when you have to modify it later.
_Bat_ Posted February 1, 2007 Author Posted February 1, 2007 Sorry, my mistake... didn't know exactly how the Joomla URLs worked :oops: Ok, each page has an ID, so you get the ID from the URL and then look for a file in includes/$id.php - where you make a single file for each page (id) you want that has the information you want to display. The PHP includes the right file for the page. $id = $_GET['id']; $file = "includes/$id.php"; if(file_exists($file)){ include($file); } Okay I understand that now. Just one more question and I know its really simple but I was never any good with PHP. There is also an onload function in the body tag, so on the pages that use the image rollovers, the body tag would have to be modified. I thought the easiest way of doing this is to do an ELSE statement at the end of your code, i.e. IF file exists, include it, ELSE insert "". That way, I could include the modified body tag in the $id.php for the pages that need it. I would do this myself, but as I say, I was never any good with PHP, i'm trying to learn it though. Is it possible you could show me how? Thanks for all your help, both of you. James
webman Posted February 1, 2007 Posted February 1, 2007 Put customised body tags in the individual files: inclues/45.php (example) $body = ''; echo '(other stuff for this page)'; ?> And once it's included: if(isset($body)){ echo $body; } else { echo ''; } If the $body variable exists (set in your included files) then just echo it out; otherwise just print a bog-standard tag without any onload things.
_Bat_ Posted February 1, 2007 Author Posted February 1, 2007 Okay I think I did what you said, I now get the following PHP error: Parse error: parse error, unexpected T_STRING in /var/www/html/headincludes/12.php on line 2 Line two is: $body = ''; Any ideas?
webman Posted February 1, 2007 Posted February 1, 2007 You will need to escape all the single quotes in the string with a backslash. $body = '';
_Bat_ Posted February 1, 2007 Author Posted February 1, 2007 You will need to escape all the single quotes in the string with a backslash. $body = ''; That worked . Thank you ever so much, I'll let you get on with your own job now, haha.
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