Lelio Faieta
Lelio Faieta

Reputation: 6684

include file passing a parameter

I am using ob_* functions to build some content that will be then passed to an HTML template and printed in pdf format with MPDF.

I need to include some contents coming from another php file. The file I need to include will run a query and echo some data in a table based on a id parameter. Normally in my application I use ajax to load this file in the right place but ob_* functions output the data before AJAX can do its stuffs (see my other question on this point) so I am looking for a workaround. The best idea I had is to use php include to do the trick. The code in my mind would look like:

foreach($listaz as $l){
    include "bp/businessplan.php?id=$l";
}

Obviously this doesn't work (I am aware of how include works as stated in this SO question) since I get this error:

Warning: include(bp/businessplan.php?id=AZ000000213): failed to open stream: No such file or directory in /var/www/jdev/creditmanager/sofferenze/soff.php on line 1000

$listaz will contain the list of parameters to pass to the file and the array is like the following:

array(
    [0]=>AZ000000213
)

(in this case just one item) So the question is: How do I get to include the content of the businessplan.php file in the main php file as many times as the values in the array printing it in a specific place in the main php file (appending it into a div one after the other)?

Upvotes: 1

Views: 87

Answers (1)

stepozer
stepozer

Reputation: 1191

  1. Try add absolute path to your file with __DIR__ constant (http://php.net/manual/en/language.constants.predefined.php) something like this:

    include __DIR__."/bp/businessplan.php?id=$l";

it must solve your path issue.

  1. And if you want get content many times you can try use return instead ob_* functions. Please see my example below:

file test.php

<?php
$r = '';
for ($i=0;$i<10;$i++) {
  $_GET['id'] = $i;
  $r .= include 'businessplan.php';
}

echo $r;

file businessplan.php

<?php
return 'your html here'.$_GET['id'].PHP_EOL;

And when I run test.php it show me this lines:

your html here0
your html here1
your html here2
your html here3
your html here4
your html here5
your html here6
your html here7
your html here8
your html here9

I hope it help

EDIT 1

  1. file not found issues - try remove ?id=$l from file name and pass it to $_GET array as I suggest above.

Upvotes: 1

Related Questions