Jump to content

Use an include with variables... is this possible?


Recommended Posts

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

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?

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?

 

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

 

 

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.

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

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.