php - include files by ID on same page -
is there way this?
including file:
<?php $_get["id"]; case "fruits": include 'fruits.php'; ?> fruits.php:
<?php $id = 'fruits'; echo 'hello fruits'; ?> i want include files id specified in included file. help.
your code incomplete, here's attempt @ solution problem.
<?php // id parameter , change standard form // (standard form lower case no leading or trailing spaces) $fileid = strtolower(trim($_get['id'])); // check file id , load relevant file switch( $fileid ){ case 'fruits': require('fruits.php'); break; case 'something_else': require('something_else.php'); break; /* ... other test cases... */ default: // unknown file requested echo 'an error has occurred. unknown file requested.'; } ?> alternatively, if have long list of possible files, recommend following:
<?php // id parameter , change standard form // (standard form lower case no leading or trailing spaces) $fileid = strtolower(trim($_get['id'])); // array of possible options: $fileoptions = array('fruits', 'something_else', 'file1', 'file2' /* ... etc... */); // check if fileid valid if(in_array($fileid, $fileoptions, true)){ // fileid valid option $fullfilename = $fileid . '.php'; require($fullfilename); }else{ // invalid file option echo 'an error has occurred. unknown file requested.'; } ?> switch statements lot of cases can long , can reduce readability. hence, second solution uses array , in_array function reduce code length. allows see/manage files permitted.
Comments
Post a Comment