2

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?

3
  • 1
    Do you want to clone Facebook? Commented Nov 16, 2011 at 13:16
  • Have you read about 'include' or 'require' ? Commented Nov 16, 2011 at 13:18
  • @gustav it seems he's looking for a condition Commented Nov 16, 2011 at 13:21

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you explain what is happening here: if (!isset($_GET['action']) { $action = "home"; } else { $action = $_GET['action']; }
To prevent errors, I check if the user is passing a parameter to the "action" variable. if you access ..../file.php the "$_GET['action']" will not be set, so, it defaults to the "home" action. If the user accesses ..../file.php?action=wall the "$_GET['action']" variable will contain 'wall', and thus, the $action variable will be set accordingly.
2

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>

1 Comment

The above code will help you to run certain script on specific action on the same page
1

You can use require_once("navigation.php")

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.