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
https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/
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;
}
?>

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.

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);

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

 

 

Archived

This topic is now archived and is closed to further replies.

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