PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Thu Oct 16, 2003 10:41 am
I'm not quite sure what you want to achieve but if you do not want to use templates maybe something like
Code: Select all
<?php // main.php
$pageHandlerToInclude = 'myNewshandler.php';
include($pageHandlerToInclude);
// <- something else here if you want to ->
if (function_exists('headerFunc'))
headerFunc();
if (function_exists('bodyFunc'))
bodyFunc();
if (function_exists('footerFunc'))
footerFunc();
?>Code: Select all
<?php // myNewshandler.php
function headerFunc()
{
?>
<html>
<head>
<title>created at <?php echo date('F j, Y, g:i a'); ?></title>
</head>
<body>
<?php
}
function bodyFunc()
{
?>
the current time is: <?php echo date('H:i:s'); ?>
<?php
}
function footerFunc()
{
?>
</body>
</html>
<?php
}
?>tested not even by compiler
will be sufficient
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Thu Oct 16, 2003 10:48 am
volka wrote: I'm not quite sure what you want to achieve but if you do not want to use templates maybe something like
no, i dont want to use templates, becuase ive nearly got exactly the same code for bakgrounds, headers etc...
i think im gonna try out the include('start.php?id=content') stuff
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Thu Oct 16, 2003 11:21 am
how can i include a file with $_GET paramters?
ive tested
Code: Select all
<?php include('start.php?w=vars') ?>
But that doesnt work.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Thu Oct 16, 2003 12:50 pm
renamed the topic to "Content management"
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Thu Oct 16, 2003 1:22 pm
you can't. But the included script has access to all variables as the calling code has.
Code: Select all
<html><!-- main.php -->
<head>
<title>test</title>
</head>
<body>
<?php
if(isset($_POST['submit'])) {
?>
<fieldset><legend>processing</legend>
<pre><?php print_r($_POST); ?></pre>
<?php
$pageParams = array(
'datetime'=>date('F j, Y, g:i a'),
'randnum'=>rand(0,99)
);
include('display.php');
?>
</fieldset>
<?php
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="options[]" multiple="multiple">
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
</select>
<input type="submit" name="submit" />
</form>
</body>
</html>Code: Select all
<!-- display.php -->
<table border="1">
<tr>
<th colspan="2"><?php echo $pageParams['datetime']; ?></th>
</tr>
<?php
foreach($_POST as $key=>$value)
{
echo ' <tr><td>', $key, '</td><td>';
print_r($value);
echo '</td></tr>';
}
?>
</table>
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Thu Oct 16, 2003 2:11 pm
hmmm, ok, so then i have to use templates
or, maybe i could use some sort of variable to tell the included page to print the content?
Site.php:
Code: Select all
<?php
//Include variables
$print_content = "No"
include('start.php')
//Include content
$print_content = "Yes"
include('start.php')
?>
Start.php:
Code: Select all
<?php
If($print_content == "No") {
//Then include vars...
} elseif($print_content == "Yes") {
//Then include content...
?>
Will this work?
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Thu Oct 16, 2003 2:34 pm
after adding some semicolons: probably
but do not define functions within this file, you will get "error: function already declared ..." errors otherwise (because of the second include)
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Thu Oct 16, 2003 2:55 pm
volka wrote: after adding some semicolons: probably
but do not define functions within this file, you will get "error: function already declared ..." errors otherwise (because of the second include)
i always forget the semicolons
ok, no functions
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Thu Oct 16, 2003 3:05 pm
yep, works, final output:
start.php:
Code: Select all
<?php If($print_content == "No") { ?>
<?php
//HEADER
$cheader = "Welcome to v-site!";
$cheader2 = "This site contains downloads, a file uploader, free scripts and tutorials.";
?>
<?php } elseif($print_content <> "No") { ?>
<?php include('nphp/news.txt') ?>
<?php } ?>
?>
Site.php:
Code: Select all
<?php
//Include variables
$print_content = "No";
//Print it
$ext = ".php";
if (isset($_GET['id'])) {
$id = "".addslashes($_GET['id'])."".$ext."";
if (file_exists($id)) {
include("$id");
}
else {
include("start.php");
}
}
else {
include("start.php");
}
?>
?>
<?php
//Include content
$print_content = "Yes";
//Print it
$ext = ".php";
if (isset($_GET['id'])) {
$id = "".addslashes($_GET['id'])."".$ext."";
if (file_exists($id)) {
include("$id");
}
else {
include("start.php");
}
}
else {
include("start.php");
}
?>