0

So I've search far and wide to try and find an example of this but found nothing. It seems like a simple thing however I continue to get errors.

Essentially what I've got is:

<?php 
    $articleCount = 3;
    include('/newsArticles.php?id=$articleCount'); 
?>

It seems fairly self explanatory. What I need is an include that I can pass a value on into the file which I can then grab with $_GET['id'].

André
  • 343
  • 2
  • 7
  • 15
  • possible duplicate of [PHP - include a php file and also send query parameters](http://stackoverflow.com/questions/1232097/php-include-a-php-file-and-also-send-query-parameters) – Alex K. Apr 29 '14 at 15:57
  • A query string is only meaningful in the context of an HTTP request, an include is not such; its a simple read from the local file system. – Alex K. Apr 29 '14 at 15:58

4 Answers4

1

You can't add a query string (Something like ?x=yyy&vv=www) onto an include like that. Fortunately your includes have access to all the variables before them, so if $_GET['id'] is defined before you call:

include('/newsArticles.php');

Then newsArticles.php will also have access to $_GET['id'];

Clément Andraud
  • 9,103
  • 25
  • 80
  • 158
0

You don't need to pass a $_GET variable. Just set it, and reference it in your included file.

Your included file will have access to $articleCount without any additional work needed.

emsoff
  • 1,585
  • 2
  • 11
  • 16
0

Try this:

$_GET['id'] = 3;
include('/newsArticles.php'); 
AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
  • Except the `newsArticles.php` file is unlikely to be at the filesystem root; so make sure that you're using the path to your web route – Mark Baker Apr 29 '14 at 16:00
0

Since you are including a script it would seem as though you have access to the script itself. Even if you don't have access to the script you can edit the $_GET variable from within the script you showed above, like this:

<?php
    $_GET['id'] = 3; // or $_GET['id'] = $articleCount;
    include('/newsArticles.php');
?>

That's because the script newsArticles.php has the same access to the $_GET variable, unless the script was specifically created so that it extracts the variables from the URL.

Try it.