Jump to content

[SOLVED] Variable running when it shouldn't


goocharlton

Recommended Posts

I have my site 'news-and-press.php' page with this code:

<?php 
$page_name = 'News & Press';
$page_ID = 'newsandpress';
$heading = 'News & Press';
$content = include("news-and-press2.php");
include ('template.php');
?>

 

When I run 'news-and-press.php' the code runs through the list of variables then it runs the include ('template.php'); (this is the page that runs the site template, it asks for the variables above).

 

My problem is when 'news-and-press.php' is run the $content variables' output displays before the include ('template.php'); is displayed but it is meant to run the include ('template.php'); and then call $content into the template.

 

The 'news-and-press2.php' file has the following code...

<?php 
function GetNews()
{
include("news_items.php"); 
	foreach ($news as $value) {
		echo "<p>";
		echo "<b>" .$value[date]. "</b><br>";
		echo $value[content];
		echo "<p>";
	}
}

echo GetNews()
?>

 

What am I doing wrong?

 

Preview can be found http://www.tx25.com/news-and-press.php

Link to comment
Share on other sites

echo GetNews();

 

That is going to echo the result returned by the function. Problem is the function doesn't return anything.

 

Try

<?php
function GetNews()
{
include("news_items.php");
        $str = ''; 
	foreach ($news as $value) {
		str .=  "<p>";
		str .= "<b>" .$value[date]. "</b><br>";
		str .= $value[content];
		str .=" <p>";
	}
        return $str;
}
?>

 

instead of

 

$content = include("news-and-press2.php");

 

try

 

include("news-and-press2.php");
$content = GetNews();

Link to comment
Share on other sites

change to

<?php
function GetNews()
{
include("news_items.php");
        $str = ''; 
	foreach ($news as $value) {
		str .=  "<p>";
		str .= "<b>" .$value['date']. "</b><br>";
		str .= $value['content'];
		str .=" <p>";
	}
        return $str;
}
?>

 

Note

$value[date] to $value['date']
$value[content] to $value['content']

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.