include as string

include as string

am 08.04.2008 14:13:44 von Harris Kosmidhs

Hello,

I have a php file like the following:





I call this file from a function :
function ListAlbums($res) {
include('views/albums_view.php');
}

Before this I have opened a sql query and $res has the resultset. So my
pages renders correctlly for each of the rows on $res.

But now I want for this file not to be printed directly (as include
does) but rather store the output to a string. As I read at
http://us3.php.net/include/ I did:

function ListAlbums($res) {
ob_start();
include('views/albums_view.php');
$disp=ob_get_contents();
ob_end_clean();
return $disp;
}

But $res now seems to be empty. Why doesn't this approach work? If I
print_r($res) into my function it returns of course data.

thanks

Re: include as string

am 08.04.2008 16:10:06 von tmilewski

On Apr 8, 8:13 am, Harris Kosmidhs
wrote:
> Hello,
>
> I have a php file like the following:
>
>


>
>
>
> I call this file from a function :
> function ListAlbums($res) {
> include('views/albums_view.php');
>
> }
>
> Before this I have opened a sql query and $res has the resultset. So my
> pages renders correctlly for each of the rows on $res.
>
> But now I want for this file not to be printed directly (as include
> does) but rather store the output to a string. As I read athttp://us3.php.net/include/I did:
>
> function ListAlbums($res) {
> ob_start();
> include('views/albums_view.php');
> $disp=ob_get_contents();
> ob_end_clean();
> return $disp;
> }
>
> But $res now seems to be empty. Why doesn't this approach work? If I
> print_r($res) into my function it returns of course data.
>
> thanks

The reason it is not working is because you are not passing the $res
variable to the function.

function ListAlbums($result_set) {
ob_start();
include('views/albums_view.php');
$disp=ob_get_contents();
ob_end_clean();
return $disp;
}

You will now need to call that function:

ListAlbums($res);


That aside, why pass it if you don't seem to be using it in the
function anyway?

Re: include as string

am 08.04.2008 16:11:57 von tmilewski

On Apr 8, 10:10 am, "Tom M." wrote:
> On Apr 8, 8:13 am, Harris Kosmidhs
> wrote:
>
>
>
> > Hello,
>
> > I have a php file like the following:
> >
> >


> >
> >
>
> > I call this file from a function :
> > function ListAlbums($res) {
> > include('views/albums_view.php');
>
> > }
>
> > Before this I have opened a sql query and $res has the resultset. So my
> > pages renders correctlly for each of the rows on $res.
>
> > But now I want for this file not to be printed directly (as include
> > does) but rather store the output to a string. As I read athttp://us3.php.net/include/Idid:
>
> > function ListAlbums($res) {
> > ob_start();
> > include('views/albums_view.php');
> > $disp=ob_get_contents();
> > ob_end_clean();
> > return $disp;
> > }
>
> > But $res now seems to be empty. Why doesn't this approach work? If I
> > print_r($res) into my function it returns of course data.
>
> > thanks
>
> The reason it is not working is because you are not passing the $res
> variable to the function.
>
> function ListAlbums($result_set) {
> ob_start();
> include('views/albums_view.php');
> $disp=ob_get_contents();
> ob_end_clean();
> return $disp;
> }
>
> You will now need to call that function:
>
> ListAlbums($res);
>
> That aside, why pass it if you don't seem to be using it in the
> function anyway?

Nvm on that last question wasn't paying attention to the include.

Re: include as string

am 08.04.2008 19:08:47 von Jerry Stuckle

Harris Kosmidhs wrote:
> Hello,
>
> I have a php file like the following:
>
>


>
>
>
> I call this file from a function :
> function ListAlbums($res) {
> include('views/albums_view.php');
> }
>
> Before this I have opened a sql query and $res has the resultset. So my
> pages renders correctlly for each of the rows on $res.
>
> But now I want for this file not to be printed directly (as include
> does) but rather store the output to a string. As I read at
> http://us3.php.net/include/ I did:
>
> function ListAlbums($res) {
> ob_start();
> include('views/albums_view.php');
> $disp=ob_get_contents();
> ob_end_clean();
> return $disp;
> }
>
> But $res now seems to be empty. Why doesn't this approach work? If I
> print_r($res) into my function it returns of course data.
>
> thanks
>

I'm not sure why it shouldn't work. However, there is a better way to
do this.

First of all, the code in your included file should itself be a
function. Then, rather than outputting the html right there, put it
into a string, and return the string, i.e.

function myfunc($res, $photosdir) {
$str = '';
foreach($res as $album) {
$str .= <<


EOT;
}
return $str;
}


Now in the calling routine you have the option of echoing the string or
not, i.e.

echo $myfunc($res, $photosdir);
or

$str = $myfunc($res, $photosdir);

You can now include the file at the top of your page and call it from
where ever you need it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================