user502019
user502019

Reputation: 49

Having multiple scripts within one php

I am trying to find out how to have multiple scripts within one php file, and run Certain scripts only when a certain link is clicked on (or some other input from a user). For example, the default output Of profile.php is the user's "wall", but when they click "info" on the profile.php page (the page reloads because I'm not using JavaScript and that's okay for now) the main content area would now display the user's info (as printed by a script within the original profile.php file) how is this done?

Upvotes: 2

Views: 4387

Answers (4)

Shamsher
Shamsher

Reputation: 1761

You can use require_once("navigation.php")

On every top of the page where navigation.php includes your Home,Wall,Profile links...

Upvotes: 1

Martin.
Martin.

Reputation: 10529

You should learn something about predefined variables $_GET, $_POST and for the later use, $_COOKIE

<?php
if ($_GET['clicked'] == "info") {
    //show user's info
}
else{
    //show another info
}
?>
<a href="?clicked=info">Click to see info</a>

Upvotes: 2

Sergio Moura
Sergio Moura

Reputation: 4942

That's fairly easy. Using only one php file, you can have many "actions" that displays some stuff depending on what you need. Take a look of this simple source code (save it as "file.php" or use the name you like and edit the "a" tags. I've included the name of the file as some browsers do not work correctly when the link starts with "?".

<h3>Sections</h3>
<a href="file.php?action=home">Home</a>
<a href="file.php?action=wall">Wall</a>
<a href="file.php?action=profile">profile</a>
<?php

if (!isset($_GET['action']) {
  $action = "home";
}
else {
  $action = $_GET['action'];
}

switch($action) {
  case 'wall':
    display_wall();
    break;
  case 'profile':
    display_profile();
    break;
  case 'home':
  default:
    display_home();
    break;
}

function display_wall() {
  echo("Wall");
  // Do something
}

function display_profile() {
  echo("Profile");
  // Do something
}

function display_home() {
  echo("Home");
  // Do something
}

Tweak that code a bit to fill your needs.

Upvotes: 3

I'm no PHP-developer. But I think the most basic way is using querystring and includes...

So your front-facing file is profile.php, but you also have info.php and wall.php which you include dynamically based on the query-string...

Upvotes: 0

Related Questions