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
https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/
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();

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']

try this

 

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.