Jump to content

PHP Class to open page from variable


Cell0518

Recommended Posts

Hi,

I'm trying to make a class in PHP4 to do:

[a href=\"http://www.domain.com/?p=pagename\" target=\"_blank\"]http://www.domain.com/?p=pagename[/a]
[Pagename displayed]

[code]<?php

// include the layout file

include 'layout.php';

// include the config file

include 'config.php';

$p=$_GET['p'];

//Get Page Content

class get_page_content {

var $p;

var $p_title;

function get_page_content() {

if ($p == "") {$p = "main"; }

$this->p = "$p";

$this->p_title = "ChrisLoveOnline : ".strtoupper($p);}

function disp_incl($this->p,$this->p_title){

    myheader("$this->p_title");

    include "./include/$this->p.inc";

    }

}

$p_content = new get_page_content();

$p_content->disp_incl($p);

?>[/code]

Parse error: parse error, expecting `')'' in 'file' on line 15. From what I can tell, it's complaining about the , between variables in the function.

I'm new to classes.

I'm also looking at how I can show an error page if the page isn't found. $p = pagename and $p.inc is include file.

Thanks,
Chris
Link to comment
Share on other sites

If you have already declared the variables you wish to use within the class, you do not need to pass them to the method inside the class. You can access them like you are, with $this. But if you are using a method in the class that requires variables to be passed to it, that is when you declare them with the method.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
Link to comment
Share on other sites

[!--quoteo(post=375620:date=May 21 2006, 10:55 AM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ May 21 2006, 10:55 AM) [snapback]375620[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If you have already declared the variables you wish to use within the class, you do not need to pass them to the method inside the class. You can access them like you are, with $this. But if you are using a method in the class that requires variables to be passed to it, that is when you declare them with the method.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
[/quote]
If I'm understanding you, I don't need this:
[code]
var $p;
var $p_title;
[/code]
Is that correct?

I'm still getting an error on line 15 (as mentioned above)

Thanks,
CL
Link to comment
Share on other sites

There are many different ways that you could go about this. Here is one way to work how you have it set up.

[code]<?php

// include the layout file
include 'layout.php';

// include the config file
include 'config.php';

//Get Page Content

class get_page_content {

var $p;
var $p_title;

function get_page_content($p) {

$this->p = $p;

if ($this->p == "") {$this->p = "main"; }

$this->p_title = "ChrisLoveOnline : ".strtoupper($this->p);}

function disp_incl(){

    myheader("$this->p_title");

    include "./include/$this->p.inc";

    }

}

$p_content = new get_page_content($_GET['p']);

$p_content->disp_incl();

?>[/code]

That shoud do it for you.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
Link to comment
Share on other sites

[!--quoteo(post=375642:date=May 21 2006, 12:09 PM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ May 21 2006, 12:09 PM) [snapback]375642[/snapback][/div][div class=\'quotemain\'][!--quotec--]
There are many different ways that you could go about this. Here is one way to work how you have it set up.

[code]<?php

// include the layout file
include 'layout.php';

// include the config file
include 'config.php';

//Get Page Content

class get_page_content {

var $p;
var $p_title;

function get_page_content() {

$this->p = $_REQUEST['p'];

if ($this->p == "") {$this->p = "main"; }

$this->p_title = "ChrisLoveOnline : ".ucfirst($this->p);}

function disp_incl(){

    myheader("$this->p_title");

    include "./include/$this->p.inc";

    }

}

$p_content = new get_page_content();

$p_content->disp_incl();

?>[/code]
[/quote]

Well, It works, to a degree. To get it to totally work, I had to remove $_REQUEST['p'] from new get_page_contents and changed $this->p = $p to $_REQUEST['p'];
[b]Another Question...[/b]
How to check to see if $_REQUEST['p'].inc exists, and if not, change $p to something like error (change unavailable/bad .inc file to error.inc)?
Thanks,
Chris
Link to comment
Share on other sites

There is an easier way to do this without classes.

You can use a switch statement which makes it a whole lot easier.

[code]<?php
//use a switch statement to find out which page is in the URL
switch($_GET['p'])
{
     case 'about':
     {
      //this is where you include the file you want to display.
      include('about.inc');
     }
     break;
    
    default:
     {
          include('main.inc');
     }
     break;
}
?>[/code]

This should make it a lot easier.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
Link to comment
Share on other sites

[!--quoteo(post=375652:date=May 21 2006, 01:07 PM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ May 21 2006, 01:07 PM) [snapback]375652[/snapback][/div][div class=\'quotemain\'][!--quotec--]
There is an easier way to do this without classes.

You can use a switch statement which makes it a whole lot easier.

[code]<?php
//use a switch statement to find out which page is in the URL
switch($_GET['p'])
{
     case 'about':
     {
      //this is where you include the file you want to display.
      include('about.inc');
     }
     break;
    
    default:
     {
          include('main.inc');
     }
     break;
}
?>[/code]

This should make it a lot easier.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
[/quote]

Question is though, I would have to make a new case for every page I have, albeit it's not that many, I was looking for it being a little more dynamic, in the case I added more pages in the future.
Link to comment
Share on other sites

You could also do this:

[code]<?php

if(isset($_GET['p']))
{
     include($_GET['p'] . '.inc');
     $pageTitle = $_GET['p'];
}

?>[/code]

This is dynamic, but you'll need to secure it more.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
Link to comment
Share on other sites

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.