Connecting Tech Pros Worldwide Help | Site Map

Include Problem

  #1  
Old July 17th, 2005, 09:40 AM
Ken
Guest
 
Posts: n/a
Hello:

I'm writing my first php app and I have a problem.
Initially I put template type strings, such as HTML Headers and page
footers in a MySQL table and called them up with a php SQL query to
include them in the printed HTML, This worked great but then I learned
about includes and I tried using includes to put them into the HTML.

1. I assume that this is faster than the query method because that's
what people seem to do. Am I right about that?

2. The following string works as a query result but not as an include
file. Could someone be so kind as to explain to me why?

<?xml version="1.0" encoding="UTF-8"\x3f>

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

The parser chokes on the first line. If I remove it the include works
fine.

Thanks,

Ken


  #2  
Old July 17th, 2005, 09:40 AM
Michael Fesser
Guest
 
Posts: n/a

re: Include Problem


.oO(Ken)
[color=blue]
>I'm writing my first php app and I have a problem.
>Initially I put template type strings, such as HTML Headers and page
>footers in a MySQL table and called them up with a php SQL query to
>include them in the printed HTML, This worked great but then I learned
>about includes and I tried using includes to put them into the HTML.
>
>1. I assume that this is faster than the query method because that's
>what people seem to do. Am I right about that?[/color]

I guess so. A filesystem access should be faster than a server request.
[color=blue]
>2. The following string works as a query result but not as an include
>file. Could someone be so kind as to explain to me why?
>
><?xml version="1.0" encoding="UTF-8"\x3f>
>[...]
>
>The parser chokes on the first line. If I remove it the include works
>fine.[/color]

You have short_open_tags enabled in your php.ini, so the parser treats
the first two chars of <?xml as the beginning of new PHP block, which
causes a parse error. Three solutions:

1) Disable short_open_tags in your php.ini.
2) Use PHP and a print or echo statement to print out the XML prolog:

<?php print "<?xml version='1.0' ...?>";?>

3) Simply remove the prolog. You only need it if you want to use another
encoding than UTF-8 (used by default) and no encoding information was
given in another protocol level, for example in the HTTP response
header.

HTH
Micha
  #3  
Old July 17th, 2005, 09:40 AM
Ken
Guest
 
Posts: n/a

re: Include Problem


On Sat, 18 Sep 2004 19:56:59 +0200, Michael Fesser <netizen@gmx.net>
wrote:[color=blue]
>[color=green]
>>2. The following string works as a query result but not as an include
>>file. Could someone be so kind as to explain to me why?
>>
>><?xml version="1.0" encoding="UTF-8"\x3f>
>>[...][/color]
>
>You have short_open_tags enabled in your php.ini, so the parser treats
>the first two chars of <?xml as the beginning of new PHP block, which
>causes a parse error. Three solutions:
>
>1) Disable short_open_tags in your php.ini.
>2) Use PHP and a print or echo statement to print out the XML prolog:
>
> <?php print "<?xml version='1.0' ...?>";?>
>
>3) Simply remove the prolog. You only need it if you want to use another
> encoding than UTF-8 (used by default) and no encoding information was
> given in another protocol level, for example in the HTTP response
> header.[/color]

Michael,

Thank you very much for your reply and explanation.

Is it odd that it works as a query result?

Ken


  #4  
Old July 17th, 2005, 09:40 AM
Lüpher Cypher
Guest
 
Posts: n/a

re: Include Problem


Ken wrote:[color=blue]
> Hello:
>
> I'm writing my first php app and I have a problem.
> Initially I put template type strings, such as HTML Headers and page
> footers in a MySQL table and called them up with a php SQL query to
> include them in the printed HTML, This worked great but then I learned
> about includes and I tried using includes to put them into the HTML.[/color]

Why don't you try something like this:

class Whatever {
function Whatever() {
...
}

function output() {
echo "<!DOCTYPE ...";
echo "<html>";
output_header();
output_body();
echo "</html>";
}

function output_head() {
// output common header
...
}

function ouput_body() {
// output custom content
...
}
}

and then for every page you have

$whatever = new Whatever();
$whatever->output();

you can pass parameters to the constructor and then use them to further
customize the body output (say, maybe a page id, which determines what
include file contains the body) :)

Lüph
  #5  
Old July 17th, 2005, 09:40 AM
Michael Fesser
Guest
 
Posts: n/a

re: Include Problem


.oO(Ken)
[color=blue]
>Is it odd that it works as a query result?[/color]

No, because to print out a query result you already use a print or echo
statement, i.e. you are inside a PHP-block. No problem with that. In
your example you try to write it out directly in a HTML-block without
PHP, but PHP interprets it wrong, hence the parse error.

Micha
  #6  
Old July 17th, 2005, 09:41 AM
Ken
Guest
 
Posts: n/a

re: Include Problem


On Mon, 20 Sep 2004 01:04:55 +0200, Michael Fesser <netizen@gmx.net>
wrote:
[color=blue]
> .oO(Ken)
>[color=green]
>>Is it odd that it works as a query result?[/color]
>
>No, because to print out a query result you already use a print or echo
>statement, i.e. you are inside a PHP-block. No problem with that. In
>your example you try to write it out directly in a HTML-block without
>PHP, but PHP interprets it wrong, hence the parse error.[/color]

Micha

Please bear with me while I try to grasp this. My include statement
is inside a php block as far as I can see:

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo "Access to ITR Billing canceled";
exit;
}
else {
$userName = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];

/* Connecting, selecting database */
$link = mysql_connect("localhost", $userName, $password)
or die("Could not connect : " . mysql_error());
mysql_select_db("ITRBilling") or die("Could not select database");

/* Performing SQL querys */
$query2 = "SELECT userName FROM Users WHERE `userName` = \"$userName\"
AND `password` = \"$password\"" ;
$result2 = mysql_query($query2) or die("Query failed : " .
mysql_error());

/* HTML Header */
include("HTMLHeader.txt")
include("PageHeader.txt");

etc...

}
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
php5 and include problem Tom answers 3 June 2nd, 2008 11:36 AM
ASP file include problem Andrew Price answers 3 November 19th, 2005 04:34 PM
include problem ScOe answers 4 July 22nd, 2005 11:05 PM
Include problem Kupo answers 2 July 17th, 2005 05:42 AM
include problem meredith answers 5 July 17th, 2005 05:38 AM