Jump to content


Photo

PHP Class to open page from variable


  • Please log in to reply
7 replies to this topic

#1 Cell0518

Cell0518
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts
  • LocationUSA / Japan (US Navy)

Posted 20 May 2006 - 07:31 PM

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]

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

?>

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
Chris

#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 21 May 2006 - 01:55 AM

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

#3 Cell0518

Cell0518
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts
  • LocationUSA / Japan (US Navy)

Posted 21 May 2006 - 02:47 AM

[!--quoteo(post=375620:date=May 21 2006, 10:55 AM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ May 21 2006, 10:55 AM) View Post[/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:
var $p;
var $p_title;
Is that correct?

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

Thanks,
CL
Chris

#4 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 21 May 2006 - 03:09 AM

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

<?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();

?>

That shoud do it for you.

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

#5 Cell0518

Cell0518
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts
  • LocationUSA / Japan (US Navy)

Posted 21 May 2006 - 03:46 AM

[!--quoteo(post=375642:date=May 21 2006, 12:09 PM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ May 21 2006, 12:09 PM) View Post[/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.

<?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();

?>
[/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'];
Another Question...
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
Chris

#6 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 21 May 2006 - 04:07 AM

There is an easier way to do this without classes.

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

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

This should make it a lot easier.

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

#7 Cell0518

Cell0518
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts
  • LocationUSA / Japan (US Navy)

Posted 21 May 2006 - 04:19 AM

[!--quoteo(post=375652:date=May 21 2006, 01:07 PM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ May 21 2006, 01:07 PM) View Post[/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.

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

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.

Chris

#8 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 21 May 2006 - 04:53 AM

You could also do this:

<?php

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

?>

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users