0

I came across an excellent tutorial here http://coursesweb.net/php-mysql/display-data-array-mysql-html-table_t on how to output mysql into a table.

I searched for the article because I wanted to go from 1 column, to 3 columns. Everything works great, except for when I want to include a file in concatenation. I'm trying to do the following:

$html_table .= '<td>';
$html_table .= '' . include('data.php') . '';
$html_table .= '</td>';

However this doesn't work. For example if the data.php file contains some simple text such as test, I get the following on the screen:

test test test test test test test test test

1 1 1

1 1 1

1 1 1

The very strange thing is if I do a little test and instead type:

$html_table .= '<td>test</td>';

this works absolutely fine and the following is outputted:

test test test

test test test

test test test

I've looked for information about using include() within this context and I've found nothing. The reason I need to include a file is because the data I'm trying to output contain lots of text. Can anyone advise?

4
  • Include does not return the files contents, but false on error and 1 on success. Commented Feb 14, 2015 at 22:48
  • 1
    example 6 on the include manual page. Commented Feb 14, 2015 at 22:48
  • 2
    $html_table .= include('data.php'); and <?php return 'test'; in data.php. Commented Feb 14, 2015 at 22:51
  • @Dagon I was going to say ob_start and al. Commented Feb 14, 2015 at 22:52

1 Answer 1

1

Use file_get_contents instead of include if all you want to do is get the contents of a file.

data.txt:

test

In your current file:

$html_table .= '' . file_get_contents('data.txt') . '';

Note that filename was changed to data.txt from data.php to avoid confusion, as it does not contain any PHP code.

If you actually meant to run some code in data.php to return a string, you will need to wrap it up in a function and include the file somewhere before you call that function:

data.php:

<?php
function getData() {
    return "test";
}
?>

In your current file:

include('data.php');

$html_table .= '<td>';
$html_table .= '' . getData() . '';
$html_table .= '</td>';

Of course you may want to remove the empty strings concatenation.

Edit

As you said in the comments the data.php contains a mixture of HTML and PHP, you may want to make use of output buffering to get the output of data.php when run.

data.php:

This is a mixture of <b>HTML</b> and <?php echo "PHP"; ?> code

In your current file:

function getIncludeOutput($file) {
    ob_start();
    include $file;
    return ob_get_clean();
}

$html_table .= '<td>';
$html_table .= getIncludeOutput('data.php');
$html_table .= '</td>';

echo $html_table;

Output:

<td>This is a mixture of <b>HTML</b> and PHP code</td>
Sign up to request clarification or add additional context in comments.

2 Comments

i didn't make it clear that in the data.php file, I have a mixture of PHP and HTML, hence file_get_contents won't work. Your solution will work though, by having a function in the data.php file, just as long as I don't have to build the HTML into a string. Let me do some reading and see if this is possible. Thx.
Ok, for such a case I have added an example on how to use output buffering as another way of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.