4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 Looking for PHP link+variable modifier script
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
Zealot Zachary
Junior WebHelper
Junior WebHelper


Joined: 28 Dec 2004
Posts: 4

PostPosted: Tue Dec 28, 2004 10:57 pm (19 years, 3 months ago) Reply with QuoteBack to Top

Hello! as wel as new to the site, I'm also new to php. Riht now while trying to expand my knowledge im trying to create a website built on php using only one page, index.php. The over all idea would be that that would be the layout and the content would be a text or php file that would loaded by include="&content.txt" and the &content should be modified when clicking on a link, which I have no idea how to code, please help me!

edit:nevermind, i figured it out the code i used was

for the link
Code:
<a href="index.php?content=drawing">


for the content
Code:
<?php
if ($content == "drawing")
{include('drawing.txt');}
else
{include('index.txt');}
?>


for anyone that cares

edit2: is there anyway to do this without constantly updating the site, as in variable read by include
OfflineView User's ProfileFind all posts by Zealot ZacharySend Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Wed Dec 29, 2004 11:58 am (19 years, 3 months ago) Reply with QuoteBack to Top

This should do it:
include($content . ".txt");

although it would probably be an idea to check whether such a file actually exists incase the query string is accidently altered/missing etc. Give this a go...

Code:
$filename = (isset($_GET['content'])) ? $_GET['content'] : "index";

$include_file = $filename.".txt";

if(file_exists($include_file))
{
   include($include_file);
} else {
   echo "Sorry, there is no page of that name.");   
}


Assuming I didn't make any mistakes this should do the following:

Line 1 is an IF statement that checks if the $_GET['content'] variable has been set. If it hasn't the $filename variable is set to 'index', if it has $filename is given the contents of the $_GET['content'] variable. This just lets us set the default for when there is no query string (i.e. the homepage)

next we join (or concatenate) the filename to the .txt extension this is done with the fullstop.

Then the main if statement checks that the file exists, if it does it is included. If it doesn't an error message is displayed. You could just include the index at this point instead.
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Zealot Zachary
Junior WebHelper
Junior WebHelper


Joined: 28 Dec 2004
Posts: 4

PostPosted: Fri Dec 31, 2004 8:17 am (19 years, 3 months ago) Reply with QuoteBack to Top

thank you very much for your knowledgable and explanational reply!
OfflineView User's ProfileFind all posts by Zealot ZacharySend Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Fri Dec 31, 2004 10:23 am (19 years, 3 months ago) Reply with QuoteBack to Top

No probs, did it work OK?
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Zealot Zachary
Junior WebHelper
Junior WebHelper


Joined: 28 Dec 2004
Posts: 4

PostPosted: Sat Jan 01, 2005 11:23 am (19 years, 3 months ago) Reply with QuoteBack to Top

unlike those newbs who just copy and paste, I actually tend to try and udnerstand what info im receiving, and after i found your lil coding mistake, it worked like a charm
Code:
echo "Sorry, there is no page of that name."<!-- SCRIPT ERROR!!!!!!--!>)<!--END SCRIPT ERROR OF TERROR--!>;
OfflineView User's ProfileFind all posts by Zealot ZacharySend Personal Message
adam
Forum Moderator & Developer



Joined: 26 Jul 2002
Posts: 704
Location: UK

PostPosted: Sat Jan 01, 2005 4:40 pm (19 years, 3 months ago) Reply with QuoteBack to Top

As well as telling the user the file wasn't found, it is probably a good idea to set the HTTP status to 404:
Code:
header('HTTP/1.0 404 File Not Found');

________________________________
It's turtles all the way down...
OfflineView User's ProfileFind all posts by adamSend Personal MessageVisit Poster's Website
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Sat Jan 01, 2005 6:46 pm (19 years, 3 months ago) Reply with QuoteBack to Top

Zealot Zachary wrote:
unlike those newbs who just copy and paste, I actually tend to try and udnerstand what info im receiving, and after i found your lil coding mistake, it worked like a charm
Code:
echo "Sorry, there is no page of that name."<!-- SCRIPT ERROR!!!!!!--!>)<!--END SCRIPT ERROR OF TERROR--!>;


ahh, thats because I pasted from my own site, where I have a function to display the error message... I guess I missed that when I reformatted into an echo Very Happy
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.213379 seconds :: 18 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme