[Solved] Content management

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

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

how can i include a file with $_GET paramters?
ive tested

Code: Select all

<?php include('start.php?w=vars') ?>
But that doesnt work.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

renamed the topic to "Content management"
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

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)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

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 ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

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");
}
?>
Post Reply