Jump to content

[SOLVED] url parameters + content


IceDragon

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/112242-solved-url-parameters-content/
Share on other sites

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";
?>

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'];

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.