...

View Full Version : noob question: php include breaks links in html...



bigj6360
12-29-2011, 07:53 PM
Hi,

This is my first post, and it deals with my first use of php include.

I am attempting to transition away from Dreamweaver and it's templating system to php. On my index page, I have included an external php file that contains only html code. It is my navigation menu. That code works fine when I use it directly in the index.html file. However, when I replace that html code on the index.html file with

<?php include("menu.php");?>

and insert that navigation code in the php file all links following the navigation menu are ignored. there is no recognition by web browsers that they exist. The page layout looks fine, the navigation menu works great (and it's links work), but all following links do not work.

I am new to using php include, and have done a good 6 hours of reading today to try to fix this, but man... it's just not coming to me.

Do I need to prefix and append <? and ?> to the external file? When I do it doesn't fix anything.

Thanks. I'll attach whatever you need to assist.
-josh

melloorr
12-29-2011, 07:54 PM
it has to be index.php

not .html

bigj6360
12-29-2011, 07:59 PM
Thanks,

Yeah, that's what I read too. But I really don't want to go through our 120 pages and rename them all *.php in order to make our php templating work.

I read somewhere (it's all a blur now) that by setting up .htaccess with the following code (which works on my hosting server) I can keep .html as the extension:


AddType application/x-httpd-php .htm .html


This is what I have done, and php is pulling my navigation menu from the external php file and serving it up through the index.html. All the rest of the html seems to come through fine as well, except <a href""> stuff.

Thanks

melloorr
12-29-2011, 08:04 PM
Thanks,

Yeah, that's what I read too. But I really don't want to go through our 120 pages and rename them all *.php in order to make our php templating work.

I read somewhere (it's all a blur now) that by setting up .htaccess with the following code (which works on my hosting server) I can keep .html as the extension:


AddType application/x-httpd-php .htm .html


This is what I have done, and php is pulling my navigation menu from the external php file and serving it up through the index.html. All the rest of the html seems to come through fine as well, except <a href""> stuff.

Thanks

in php


echo "<a href=index.html>Index<a>";

will work

without quotation marks around the address

Fou-Lu
12-29-2011, 08:10 PM
I believe that creates invalid HTML code though.
Use single quotations and double for the properties with a simple echo '<a href="index.html">Index</a>';

As for the addtype, yes that will work to parse .html as .php. Problem is that they are not really .php pages, which means if you change servers or webserver software it may not work. I would rename these pages instead. This is trivial though, as the command line will let you wildcard a match so you can rename with ren *.html *.php.

melloorr
12-29-2011, 08:19 PM
I believe that creates invalid HTML code though.
Use single quotations and double for the properties with a simple echo '<a href="index.html">Index</a>';


I have done it and it works. Admittedly it does come up as
<a href=index.php>Home</a>
On page source, but the link still works. But I guess your way is still abit safer

bigj6360
12-29-2011, 08:23 PM
I am only looking to template my header, nav menu, and footer at this time. Is it best to convert completely to PHP (and correct the resulting syntax errors that crop up) and insert html rather than just inserting the PHP I need into my html files?

I have no intention of changing web hosts or server types. I am happy on my linux server.

I'm sorry about my ignorance. Hopefully that will change sooner rather than later.

thanks again.

Fou-Lu
12-29-2011, 08:30 PM
I have done it and it works. Admittedly it does come up as
<a href=index.php>Home</a>
On page source, but the link still works. But I guess your way is still abit safer

I didn't say it wouldn't work, but I'm quite sure its invalid. There are situations that it wouldn't work, such as an implicit +/%20 added that's left to the HTML to encode. That will be especially bad in the case of additional properties. Definitely use ' or " to wrap any value in HTML.


I am only looking to template my header, nav menu, and footer at this time. Is it best to convert completely to PHP (and correct the resulting syntax errors that crop up) and insert html rather than just inserting the PHP I need into my html files?

I have no intention of changing web hosts or server types. I am happy on my linux server.

I'm sorry about my ignorance. Hopefully that will change sooner rather than later.

thanks again.

Its up to you really. .PHP pages can be used for pure HTML as well, at the cost of a preprocess. The server treats it as .php so it assumes that you have php code in it. So it will check the entire file first and parse it as php. Is it substantially slower? Not really. The pro is you can modify it to add php whenever you want without needing to worry how this file is dependent on others. I haven't written a .html file in years; only .php and .xml in regards to the web world (with PHP of course).

bigj6360
12-29-2011, 09:15 PM
Well,

I have only dealt with php in relation to mySQL. I do not have a lot of experience with it. How difficult of a job am I looking at if I change from .html to .php. Am I going to need to do a considerable amount of work to accomplish this? I am not against doing it, it's just work.

I would rather like to try and track down what is causing all links to die after the php call. That would solve all my current problems and I can work on going to PHP later as I become more fluent in it.

Do I need to add php language in the menu.php file I am including, or is that taken care of by the php call istelf?

thanks again

melloorr
12-29-2011, 09:19 PM
Can you post your code?

Fou-Lu
12-29-2011, 09:22 PM
I would rather like to try and track down what is causing all links to die after the php call. That would solve all my current problems and I can work on going to PHP later as I become more fluent in it.


Did I miss something; I didn't realize you have an error here? Dying is not normal with an include unless its declaring a function which will fail on the second inclusion without a check first (which is why include_once is used more often). Not even a bad file path will cause an include to die, as an include is not required to continue processing.

Do you mean that all the links are in .html format and you are now looking at renaming the page as .php? If so, then yep, that will be the cause for a 404 error. For this reason, you may want to use the addtype to convert the .html if you have a few hundred pages (sorry bad assumption on my part - all my links are dynamic so I don't need to concern myself with the extensions). Alternatively, .htaccess can rewrite the .html into .php if it can't find the .html.

The included file itself does not need to be PHP, it can be html, or perl or whatever else you want it to be. If its PHP, and using a filesystem wrapper (which this is above), then it will treat the PHP as parsable. If its not PHP, or not filesystem wrapped, then it will treat the results of the page as the inclusion instead of the code within the page.

So the best is to clarify what the problem is with the dying links first.

tangoforce
12-29-2011, 10:23 PM
I have done it and it works. Admittedly it does come up as
<a href=index.php>Home</a>
On page source, but the link still works. But I guess your way is still abit safer

It's invalid markup and you're basically recommending a bodge because you're inexperienced and don't know what else to recommend.

Please don't be offended but you are relatively new to PHP so if you don't know how to offer solid advice that is clean code-wise, please don't. Again I don't wish to sound rude but its vital that if you're going to contribute, you know what is good, bad, correct and wrong otherwise you may lead be leading the blind.

melloorr
12-29-2011, 10:39 PM
It's invalid markup and you're basically recommending a bodge because you're inexperienced and don't know what else to recommend.

Please don't be offended but you are relatively new to PHP so if you don't know how to offer solid advice that is clean code-wise, please don't. Again I don't wish to sound rude but its vital that if you're going to contribute, you know what is good, bad, correct and wrong otherwise you may lead be leading the blind.

I am not offended. I honestly thought that was a proper way to do it but if it is not then I learned that what I have been doing is incorrect, and I was called on it. So I am sorry for recommending a botch.

tangoforce
12-29-2011, 11:38 PM
For future ref, if you need double quotes within a double quoted string, escape them like this:



$String = "Joe said \"This is a test string\"";


The \ before the " tells php to ignore the next character.

There is more about this in one of the links in my signature but I honestly can't remember which one so take a look at them.

It's the quotes/parse error link

melloorr
12-30-2011, 12:00 AM
For future ref, if you need double quotes within a double quoted string, escape them like this:



$String = "Joe said \"This is a test string\"";


The \ before the " tells php to ignore the next character.

There is more about this in one of the links in my signature but I honestly can't remember which one so take a look at them.

It's the quotes/parse error link


Thanks, and sorry again. I wouldn't intentionally give bad information/help.

tangoforce
12-30-2011, 12:03 AM
We can all see you're trying to help but thats what I mean, until you've got a bit more experience you can't always tell if your advice is good or bad even though its well meant.

I was programming PHP for 2-3 years before I joined here and started offering advice to people and even now I make blunders!

bigj6360
12-30-2011, 12:50 AM
Thanks for the help.

I'm sorry for the confusion. I am no longer at work so I will post the code in the morning, but here is the gist of what I have.

1. An HTML file titled index.html
2. A php include (see original post)

I am not receiving an error, but wherever I place the include in my HTML file links die. It begins directly after the include within the HTML file. All links prior to the include and within the include work, and there is no visible HTML problem after the include( at least in terms of divs and list and the like).

That's all I can think of right now, but will post snippets in the morning.

Thanks

tangoforce
12-30-2011, 01:09 AM
rename index.html to index.php

Job done :thumbsup:

bigj6360
12-30-2011, 02:03 PM
Code Snippets:

Remember, The dead links immediately follow the included php (Even though it is straight html). The number of dead links change based on whether I place the include at the top, or bottom of the page. The two files are in the same directory.

I appreciate the help.



index.html (and .php [it happens regardless the extension]) snippet:



<header class="container_24">
<h1><a href="http://www.bayleaf.org"> <img src="assets/images/LOGOS/logo.png" width="325" height="117px"></a></h1>
<nav class="grid_18">
<?php include('menu.php'); ?>
</nav>

menu.php snippet:



<ul id="navmenu">
<li><a href="#" class="drop">Ministries</a>
<div class="dropdown_4columns align_right">
<div class="col_1">
<h3>Children &amp; Students</h3>
<ul>
<li><a href="/lllc/index.html">Little Lambs Learning Center</a></li>
<li><a href="/preschool/index.html">Preschool</a></li>
<li><a href="/children/index.html">Elementary</a></li>
<li><a href="/students/index.html">Middle &amp; High school</a></li>
</ul>
</div>

<div class="col_1">
<h3>Adults<br />&nbsp;</h3>
<ul>
<li><a href="/college/index.html">College &amp; Young Adults</a></li>
<li><a href="/men/index.html">Men</a></li>
<li><a href="/women/index">Women</a></li>
<li><a href="/seniors/index.html">Seniors</a></li>
</ul>
</div>

<div class="col_1">
<h3>Music<br />&nbsp;</h3>
<ul>
<li><a href="#">Children/Student Choirs</a></li>
<li><a href="#">Adult Choir</a></li>
<!--<li><a href="#">WorkAwesome</a></li>
<li><a href="#">Mac Apps</a></li>
<li><a href="#">Web Apps</a></li>-->
</ul>
</div>

<div class="col_1">
<h3>Misc<br />&nbsp;</h3>
<ul>
<li><a href="#">Missions</a></li>
<li><a href="#">Sunday School</a></li>
<li><a href="#">Prayer</a></li>
<li><a href="#">Care &amp; Support</a></li>
<li><a href="#">Activities</a></li>
</ul>
</div>
</div>
</li>
</ul>

melloorr
12-30-2011, 02:29 PM
I still don't get what the problem is. I have tested it on my local server and it appears all the links in the .php file (the only links in the code you posted) all work.

EDIT
Do you mean they do not work at all, or they just point somewhere different?

bigj6360
12-30-2011, 02:44 PM
Just figured it out. The external menu is absolutely positioned and is blocking, for some reason, all links that are beneath the tallest menu (even when it is not in use).

I noticed that one of my links is lower in the code, but higher on the layout than the php include and it works. I then placed a few more links in the page to see if it was the menu itself that was blocking the links below it. One of the three links I placed worked, and it was just below where the tallest fully extended drop down menu would go.

It appeared to me that something was funning with my include or something within the include was wrong, but, instead, it was because of the CSS related to that include file (I think). Now I've just got to find that.

Thanks for the help.

_Aerospace_Eng_
12-30-2011, 03:47 PM
This is why we always ask people to post links or their html/css to their site as it is usually a display problem when you can clearly see the include working. Instead you just posted snippets that you thought were the problem but no CSS. Either way glad you got it sorted.

bigj6360
12-30-2011, 04:06 PM
--Aerospace--
I'm not going to respond to that except to say that I am a new PHP user. It was not so clear to me.


Thanks to everyone for the pointers. I appreciate the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum