View Single Post
Old 04-29-2005, 02:39 PM   #12 (permalink)
Silvy
paranoid
 
Silvy's Avatar
 
Location: The Netherlands
NOTE: READ THIS FIRST
I wrote this response to Arch13's previous post.
I thought he was asking 2 questions:
- Should I include files dynamically?
- Is it possible for included files to use variables set in the URL?
This response tries to tackle those questions. But re-reading his post, I wasn't sure he was asking for this
So that's why I posted follow-up questions (my previous post).
But in an effort to speed things along (and in case he indeed did ask for this reponse), I will now post the original reply I wrote.

See this as more of an informational post, rather than continuing the discussion.

End note


I see your problem. (EDIT: I didn't )
Though using global variables should work on included pages.

BUT..... why would you?

What does the included page do? If the included page does the same thing everytime (i.e. display an image that the first script can guess itself), why would you include it, and not just build it in.

IMHO including files can have the following reasons:
- clearer code (large included libraries for example)
- seperate authors. No need to include other's code because it makes debugging difficult
- often-changing files (if the included files change often, or are perhaps changed by someone else)
- code in the included files is not always needed. (as seems to be your case. You include files dynamically).

Now the last point is only valid if the different included files (say brewery.php, stable.php, garage.php etc) vary in code or this difference in functionality is expected in the future
If the code is predictable (as your quoted second file seems to be), why not put it into your main index.php?

Your suggestion:
Quote:
Originally Posted by Arch13
Or would using your suggested page on the index.php page, and simply creating an anchor for id2 in any included page be more effeciant?
would then be the way to go.

If, however, you still want / need to variably include files, and hence need to pass the $_REQUEST['id2'] parameter to the include file, you should not need to do much.
On my installation this works out of the box. I just created 2 files (ugly code, but it works )
Save them in their suggested names, and try it out. One displays the value for 'id' (taken from the URL) and includes the second file. The second file prints the value of id2 which is also taken from the URL. In essence this proves that passing the values you need, works.

First file:
(include_test.php)
PHP Code:
<?php
echo '<pre>';
echo 
"include test \n";

echo 
'$_REQUEST[id] equals: '.$_REQUEST['id']." \n";
echo 
"Including file: \n";
include (
'included.php');
echo 
"passed including files \n";
?>
Second file:
(included.php)
PHP Code:
<?php
echo 'Inluded file thinks $_REQUEST[id2] equals: '.$_REQUEST['id2']."\n";
?>
Now if you call "include_test.php?id=foo&id2=bar" you should see this:
Code:
include test 
$_REQUEST[id] equals: foo 
Including file: 
Inluded file thinks $_REQUEST[id2] equals: bar
passed including files
I hope this clears things up a little in either case. I guess it's obvious I'm not sure why you dynamically include files, so I tried to help by giving that "reasons for including" list above.
Anyhow, unless your setup blocks it, passing URL variables to included files works automatically. (And so should so called 'global variables', so if you need to create a variable, you can "pass" it to included files as well. In fact, that's how my scripts usually load a bunch of settings, it includes a settings file which creates a whole bunch of global variables.)

Note: indeed I forgot two semicolons in my previous code, so it didn't parse. The above code was copy/pasted from the working files so they should work
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. "
- Murphy MacManus (Boondock Saints)
Silvy is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360