Cell0518 Posted May 20, 2006 Share Posted May 20, 2006 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 fileinclude 'layout.php';// include the config fileinclude 'config.php';$p=$_GET['p'];//Get Page Contentclass 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 Quote Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/ Share on other sites More sharing options...
jeremywesselman Posted May 21, 2006 Share Posted May 21, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/#findComment-37543 Share on other sites More sharing options...
Cell0518 Posted May 21, 2006 Author Share Posted May 21, 2006 [!--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 Quote Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/#findComment-37558 Share on other sites More sharing options...
jeremywesselman Posted May 21, 2006 Share Posted May 21, 2006 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 fileinclude 'layout.php';// include the config fileinclude 'config.php';//Get Page Contentclass 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--] Quote Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/#findComment-37564 Share on other sites More sharing options...
Cell0518 Posted May 21, 2006 Author Share Posted May 21, 2006 [!--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 fileinclude 'layout.php';// include the config fileinclude 'config.php';//Get Page Contentclass 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 Quote Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/#findComment-37569 Share on other sites More sharing options...
jeremywesselman Posted May 21, 2006 Share Posted May 21, 2006 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 URLswitch($_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 Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/#findComment-37574 Share on other sites More sharing options...
Cell0518 Posted May 21, 2006 Author Share Posted May 21, 2006 [!--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 URLswitch($_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. Quote Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/#findComment-37578 Share on other sites More sharing options...
jeremywesselman Posted May 21, 2006 Share Posted May 21, 2006 You could also do this:[code]<?phpif(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--] Quote Link to comment https://forums.phpfreaks.com/topic/10076-php-class-to-open-page-from-variable/#findComment-37585 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.