IceDragon Posted June 27, 2008 Share Posted June 27, 2008 This is probably really basic stuff but i dunno how to do it. Here's what id like to know. i have a page /test.php and all i have on that page are 2 links.. 1 link is /test.php?id=1 and i want to add some things there but i dunno how.. same with 2nd link (/test.php?id=2) i know u cant just type in something in notepad and save it as test.php?id=1 cus it wont let u.. so WHERE do i add the content i want in those 2 links? Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/ Share on other sites More sharing options...
pugboy Posted June 27, 2008 Share Posted June 27, 2008 <?php $id = $_GET["$id"]; if($id=="1"){ echo "Your content"; } else { echo "Content 2"; } ?> I bet there is a better way to do it, but this is what I usually do Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/#findComment-576287 Share on other sites More sharing options...
.josh Posted June 27, 2008 Share Posted June 27, 2008 well, ideally your main page will be a controller, and you would store each piece of content as separate files. It would look something like this: content1.html <html> <head></head> <body> BLAHBLAHBLAHBLAH MORE BLAH THIS IS CONTENT 1 </body> </html> content2.html <html> <head></head> <body> BLEHBLEHBLEHBLEH MORE BLEH THIS IS CONTENT 2 </body> </html> main.php <html> <head></head> <body> <a href = 'main.php?id=content1'>Content 1</a> <a href = 'main.php?id=content2'>Content 2</a> </body> </html> <?php // array of allowed content files $allowed = array('content1','content2'); // if there is an id in the url... if ($_GET['id']) { // if id is allowed... if (in_array($_GET['id'], $allowed) { // assign page to $id $id = $_GET['id']; // if id not allowed... } else { // assign a default of first one in array $id = $allowed[0]; } // end if..else // if no id in url exists... } else { // assign a default of first one array $id = $allowed[0]; } // end if..else include "$id.html"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/#findComment-576301 Share on other sites More sharing options...
IceDragon Posted June 27, 2008 Author Share Posted June 27, 2008 i tried both. neither worked. the 1st one would work if i didnt have <div> and other elements inside the echo.. the 2nd one.. there seems to be some errors or something. When i uploaded it i got Parse error: parse error, unexpected '{' in HIDDEN on line 25 line 25 = if (in_array($_GET['id'], $allowed) { and when i remove the curly tag { i get T_VARIABLE error on line 27 line 27 = $id = $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/#findComment-576342 Share on other sites More sharing options...
.josh Posted June 27, 2008 Share Posted June 27, 2008 sorry, missed the closing ')' if (in_array($_GET['id'], $allowed)) { Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/#findComment-576357 Share on other sites More sharing options...
IceDragon Posted June 28, 2008 Author Share Posted June 28, 2008 i have another small problem Now when i open /test.php it automatically displays the content of /test.php?id=content1 before i actually click the link to test.php?id=content1 how do i fix this? tnx in advance. Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/#findComment-576672 Share on other sites More sharing options...
.josh Posted June 28, 2008 Share Posted June 28, 2008 Assuming that you are using my script example, did you read the comments in it? The comments tell you where it defaults to content1 if there is no ...?id= (like on page load). You can change that to default to something else or remove the else altogether. Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/#findComment-576679 Share on other sites More sharing options...
IceDragon Posted June 28, 2008 Author Share Posted June 28, 2008 ah. ya i got it now Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/#findComment-576797 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.