Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.