Forum Moderators: coopster

Message Too Old, No Replies

Trying To Create A php Template

I'm having problems with links

         

Wallie

10:09 pm on Mar 18, 2004 (gmt 0)

10+ Year Member



Hey guys, I just recently got involved with php and found your forum. The Top 100 spaghetti signs had me on the floor!
But anyway, I went through this online tutorial that was supposed to help me create a very simple php template, which was supposed to help me understand how to make the links and includes. Well, I can't get it working properly, the code and errors are below. When the page loads the includes won't display when the links are clicked. Thanks for any advice you guys can offer!

[ BEGIN CODE ]

<?
if ($link == page2){
$page="pages/page2.html";
}
elseif($link == page3){
$page="pages/page3.html";
}
else{
$page="pages/page1.html";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="maincss.css">
<title> index.php </title>
</head>
<body bgcolor="#999999">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="3"><h1>The Template (index.html)</h1></td>
</tr>
<tr>
<td align="center" valign="top"><a href="index.php?link=page1"> Page 1 </a></td>
<td align="center" valign="top"><a href="index.php?link=page2"> Page 2 </a></td>
<td align="center" valign="top"><a href="index.php?link=page3"> Page 3 </a></td>
</tr>
</table>
<? include($page);?>
</body>
</html>

[ END CODE ]

[ BEGIN ERRORS ]

Notice: Undefined variable: link in c:\inetpub\wwwroot\TMPhs2k8usl8o.php on line 2

Notice: Use of undefined constant page2 - assumed 'page2' in c:\inetpub\wwwroot\TMPhs2k8usl8o.php on line 2

Notice: Undefined variable: link in c:\inetpub\wwwroot\TMPhs2k8usl8o.php on line 5

Notice: Use of undefined constant page3 - assumed 'page3' in c:\inetpub\wwwroot\TMPhs2k8usl8o.php on line 5

Warning: main(pages/page1.html): failed to open stream: No such file or directory in c:\inetpub\wwwroot\TMPhs2k8usl8o.php on line 35

Warning: main(): Failed opening 'pages/page1.html' for inclusion (include_path='.;c:\php4\pear') in c:\inetpub\wwwroot\TMPhs2k8usl8o.php on line 35

[ END ERRORS ]

Regards,
Wallie, NEWBIE

mbcx9rvt

2:01 am on Mar 19, 2004 (gmt 0)

10+ Year Member



Hi and welcome. There's a few things that I notice straight away...

The sole reason for all of your erros on this page is because the first time you load this page the variables are unset. So you need to do the following:


<?
if (isSet($link)){

if($link == 'page2'){
$page="pages/page2.html";
}
elseif($link == 'page3'){
$page="pages/page3.html";
}
else{
$page="pages/page1.html";
}

}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="maincss.css">
<title> index.php </title>
</head>
<body bgcolor="#999999">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="3"><h1>The Template (index.html)</h1></td>
</tr>
<tr>
<td align="center" valign="top"><a href="index.php?link=page1"> Page 1 </a></td>
<td align="center" valign="top"><a href="index.php?link=page2"> Page 2 </a></td>
<td align="center" valign="top"><a href="index.php?link=page3"> Page 3 </a></td>
</tr>
</table>
<? include($page);?>
</body>
</html>

You should now be ready to rock and roll! HTH

mbcx9rvt

mbcx9rvt

2:03 am on Mar 19, 2004 (gmt 0)

10+ Year Member



Another couple of things to note:

It's often nicer to us <?php code here ?> instead of <? ?> - it's easier to read and I'm sure there will be some techno mumbo jumbo about some browsers liking the language defined.

Try using:

<style type="text/css" media="screen">@import "main.css";</style>

when you import your external stylesheets.

Good luck and HTH,

mbcx9rvt

Wallie

6:46 pm on Mar 19, 2004 (gmt 0)

10+ Year Member



Thanks! That solved a TON of the errors and warnings, but there is still one thing bothering me, and that still isn't working. It's saying that $page still isn't set. I tried to figure it out on my own but couldn't. Also, the include path that it has in ( ) is wrong, is there a way to control where the include function is looking for the file/variable? Thanks Again!

[ ERRORS ]

Notice: Undefined variable: page in c:\inetpub\wwwroot\index.php on line 32

Warning: main(): Failed opening '' for inclusion (include_path='.;c:\php4\pear') in c:\inetpub\wwwroot\index.php on line 32

[ CODE ]

<?
if (isSet($link)){

if($link == 'page2'){
$page="/page2.htm";
}
elseif($link == 'page3'){
$page="/page3.htm";
}
else{
$page="/page1.htm";
}

}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="maincss.css">
<title> index.php </title>
</head>
<body bgcolor="#FFFFFF">
<table width="25%" align="center" cellpadding="0" cellspacing="0" border="0">
<tr>
<td colspan="3" align="center" height="50"><font face="Verdana, Arial, Helvetica, sans-serif" size="3">The Template (index.html)</font></td>
</tr>
<tr>
<td align="center" valign="top"><a href="index.php?link=page1"> Page 1 </a></td>
<td align="center" valign="top"><a href="index.php?link=page2"> Page 2 </a></td>
<td align="center" valign="top"><a href="index.php?link=page3"> Page 3 </a></td>
</tr>
</table>
<? include($page);?>

</body>
</html>

mbcx9rvt

7:41 pm on Mar 19, 2004 (gmt 0)

10+ Year Member



Yeah, sorry my bad:

Line 32 should be:

<?php

if(isSet($page)){ include($page);
}
?>

Wallie

7:56 pm on Mar 19, 2004 (gmt 0)

10+ Year Member



mbcx9rvt, I can't thank you enough, you finally got me going with this! There are no more errors now, and the url changes the way it should when the links are clicked, but the pages aren't being displayed. Can you think of any reason why this would happen? Is there anything special I should have in the included files for them to display properly? I posted the short HTML include files below. I have used include before with much success but never with a variable in it. Any ideas what could be going on? Thanks!

[ page1.htm ]

<table>
<tr>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#000000">This Should Be Page 3</font></td>
</tr>
</table>

mbcx9rvt

3:51 am on Mar 20, 2004 (gmt 0)

10+ Year Member



Generally an include file should start with <html> or an appropriate markup beginning tag. Try with that first.

If it is not that you need to do some debugging.

Replace the include() statement with a print("test"); statement. if "test" is printed then you know your script is working up to that point. If nothing is output then you know you have an issue earlier on in your script.

Im sorry my responses are slow but I am away from my PC most of this week. I'm glad I've helped so fa.r If you like, print your whole code in your next post and I will see what I can do. Also, tell me what the response is from the test as described above.

Good luck,

mbcx9rvt

mbcx9rvt

3:54 am on Mar 20, 2004 (gmt 0)

10+ Year Member



There is also some discrepency in the first code you posted and the last one. In the first one you have links to pages/page2.html and in the second you have /page2.html

Do these files exist? If nothing is output then chances are the files don't exist as we haven't told it to do anything upon these files not existing - only for it to do something if they do exist.

Double check that you have your links correct in your $page variables! i.e. do you mean pages/page2.html or simply /page2.html

HTH

mbcx9rvt