Jump to content

really easy bit of help please :)


MSUK1

Recommended Posts

ob_start();
include('file.php');
$content = ob_get_clean(); 

 

ok so this is the code i found for adding a file to a variable..

 

now how would i use it in term of this:

<?php
$id = NULL;
if(isset($_GET['id']) && $_GET['id'] == "1"){
  $id = 'Marc';
}
else if(isset($_GET['id']) && $_GET['id'] == "2"){
  $id = 'Glenn';
}
else if(isset($_GET['id']) && $_GET['id'] == "3"){
  $id = 'Nathan';
}
else if(isset($_GET['id']) && $_GET['id'] == "4"){
  $id = 'Scott';
}
?>

 

ok so the get variable checks to see whose id youve requested.

 

then when for example id = 4 i want to display an include file somewhere else on the page so i would use..

<?php echo $id; ?>

 

now insted of it being "nathan" i want it to be a file

 

i didnt really understand the first code i found

 

Link to comment
Share on other sites

I'm not sure what you are trying to do, your question is not clear, but is seems to be related to having individual included content for each id/name? Could you indicate how your $id relates to the file.php include() statement?

 

However, you should not use a series of if/else if statements to lookup values when there can be an arbitrary and changing number of values. Use something like the following to replace your if/else if code -

 

<?php
$lookup = array();
$lookup[1] = 'Marc';
$lookup[2] = 'Glenn';
$lookup[3] = 'Nathan';
$lookup[4] = 'Scott';
// add other key/value pairs as necessary

$id = isset($_GET['id']) ? (int)$_GET['id'] : NULL; // get input value (or NULL)

// lookup value in array
if(isset($lookup[$id])){
$id = $lookup[$id];
} else {
$id = NULL;
}
?>

Link to comment
Share on other sites

oh such great feedback thankyou,

 

okay sorry for the unclearness..

 

this is for a biography page, so their are only 4 bio's needed.

 

when the user clicks a picture of a member, it loads their information (which will be in say marc.php)

 

and that i thought would be easyier to write, than writing the individual bio's in the main page.

Link to comment
Share on other sites

If there's no php code in the files, you can simply use file_get_contents and then just echo the $content any where you want.

 

file_get_contents, like any function, can use a variable as the parameter when it is called. Once you get the file name in to a variable, you can do the following -

 

$filename = 'whatever.php'; // get the correct name using your id to name lookup code

$content = file_get_contents($filename);

Link to comment
Share on other sites

okayyy so :)

 

going back to my old method now..

 

i use this to get the id:

<?php
$id = NULL;
if(isset($_GET['id']) && $_GET['id'] == "marc"){
  $id = 'Marc.php';
}
else if(isset($_GET['id']) && $_GET['id'] == "glenn"){
  $id = 'Glen.php';
}
else if(isset($_GET['id']) && $_GET['id'] == "nathan"){
  $id = 'Nathan.php';
}
else if(isset($_GET['id']) && $_GET['id'] == "scott"){
  $id = 'Scott.php';
}
?>

 

then use...

$content = file_get_contents($filename);

 

 

then!

<?php echo $content ?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.