MSUK1 Posted November 23, 2010 Share Posted November 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/ Share on other sites More sharing options...
pengu Posted November 23, 2010 Share Posted November 23, 2010 <?PHP $file = $_GET['id']; switch ($file) { case 1: include '1.php'; break; case 2: include '2.php'; break; case default: include 'default.php'; break; } ?> Something like that? Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138707 Share on other sites More sharing options...
Hybride Posted November 23, 2010 Share Posted November 23, 2010 ob_start(); include('file.php'); $content = ob_get_clean(); ok so this is the code i found for adding a file to a variable.. No, that's including starting a session buffer (ob_start()), including a file, and cleaning out the buffer for $content. Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138710 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138711 Share on other sites More sharing options...
MSUK1 Posted November 23, 2010 Author Share Posted November 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138715 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 Is there actually php code in the 'marc.php' type files or is it just some text content that you want to place into the HTML on your page? And, is there a reason you are not using a database for this? Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138723 Share on other sites More sharing options...
MSUK1 Posted November 23, 2010 Author Share Posted November 23, 2010 not using a database because its only 4 members whose data isnt going to change, could use one however good suggestion! and, no its just html markup for p and h1 tags Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138740 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2010 Share Posted November 23, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138749 Share on other sites More sharing options...
MSUK1 Posted November 23, 2010 Author Share Posted November 23, 2010 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/219630-really-easy-bit-of-help-please/#findComment-1138759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.