Dustin013 Posted November 12, 2008 Share Posted November 12, 2008 I am trying to include a script in my index.php file that will display some records... However, I am trying to include a file with a variable attached to the include like so... include("include/somefile.php?action=display&title=test"); After a bit of Googling and reading I found out that it quite doesn't work like that, so I was curious if it was possible to include a php file with the variables attached using another method that doesn't involve ajax... Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/ Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 No, it doesn't work like that. But included files are treated as if they are actual code in the parent file, so you can use any variables freely between the two: main.php : <?php $message = "Testing!"; include('test.php'); test.php : <?php echo $message; Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-688918 Share on other sites More sharing options...
Dustin013 Posted November 12, 2008 Author Share Posted November 12, 2008 What about for instance I have the following... This is a link on the title bar. <a href="index.php?showitem=a>List all articles starting with A</a> in index.php $showitem = $_GET['showitem']; // Grab the value of showitem if ($showitem == ''){ // if no showitem value show default listings include ("default.php"); } else{ // If the value of $showitem is not blank include ("include/default.php?action=".$showitem."); // include default.php?action=a } Not sure if the formatting is completely accurate above, but is something like this possible? So once more, the user clicks the A link, it reloads index.php and where it would normally just include default.php it now loads up up default.php?action=a because the user loaded up index.php?showitem=1 Make any sense? Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-688936 Share on other sites More sharing options...
blueman378 Posted November 13, 2008 Share Posted November 13, 2008 did you read his reply? herws an example you have index.php <?php $a =1; $b =2; echo "$a; include("index2.php"); ?> index2.php is $c = $a+$b; echo $c; this would make your script acctually look like <?php $a =1; $b =2; echo "$a; $c = $a+$b; echo $c; ?> you are not acctually executing a page when you inclue it its more of a copy and paste sort of thing/ get it? Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-689101 Share on other sites More sharing options...
nitation Posted November 13, 2008 Share Posted November 13, 2008 I think your explanation is going further than his capability. In a nutshell, IT IS NOT POSSIBLE TO ACHIEVE WHAT YOU ARE TRYING. The major reason why you would wanna have an included file is because you need the file to appear on your page more than once. This would work <?php include_once 'myfolder/ourfiles.php'; ?> This wouldn't <?php include_once 'myfolder/ourfiles.php?profileID=13'; ?> My 2 cent Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-689156 Share on other sites More sharing options...
blueman378 Posted November 13, 2008 Share Posted November 13, 2008 I think your explanation is going further than his capability. lol Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-689212 Share on other sites More sharing options...
nitation Posted November 13, 2008 Share Posted November 13, 2008 Blue man was that funny? Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-689291 Share on other sites More sharing options...
premiso Posted November 13, 2008 Share Posted November 13, 2008 I am trying to include a script in my index.php file that will display some records... However, I am trying to include a file with a variable attached to the include like so... include("include/somefile.php?action=display&title=test"); After a bit of Googling and reading I found out that it quite doesn't work like that, so I was curious if it was possible to include a php file with the variables attached using another method that doesn't involve ajax... No, that doesnt work. If you wish to include a file with "get" data you can do this: <?php $_GET['action'] = "display"; $_GET['title'] = "test"; include("include/somefile.php"); ?> You just have to manually define the get data you want to put in. Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-689300 Share on other sites More sharing options...
Mchl Posted November 13, 2008 Share Posted November 13, 2008 Can't you just say that $_GET[] array is available in all included files? As this is apparently what OP tries to do. Move $_GET variable from main script to included script. Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-689304 Share on other sites More sharing options...
premiso Posted November 13, 2008 Share Posted November 13, 2008 Can't you just say that $_GET[] array is available in all included files? As this is apparently what OP tries to do. Move $_GET variable from main script to included script. Agree with you 100%, I just figured he wanted to define his own $_GET. But you bring up a valid point, if the get is coming from the page that is including the somefile.php the get data will pass thru just fine without doing what I did above. Thanks Mchl for bringing that up. I just figured he would have already known that =) Quote Link to comment https://forums.phpfreaks.com/topic/132492-use-an-include-with-variables-is-this-possible/#findComment-689308 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.