goocharlton Posted August 19, 2007 Share Posted August 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/ Share on other sites More sharing options...
Barand Posted August 19, 2007 Share Posted August 19, 2007 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(); Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327944 Share on other sites More sharing options...
goocharlton Posted August 19, 2007 Author Share Posted August 19, 2007 I get the following error when I use that code: Parse error: syntax error, unexpected T_CONCAT_EQUAL in /home/tx25com/public_html/news-and-press2.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327968 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 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'] Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327971 Share on other sites More sharing options...
goocharlton Posted August 19, 2007 Author Share Posted August 19, 2007 Nope that didnt work either, same error as before. I am running PHP 4.4.4 on my server, would that be the reason? Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327974 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 can you post line 6-8 of news-and-press2.php Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327975 Share on other sites More sharing options...
goocharlton Posted August 19, 2007 Author Share Posted August 19, 2007 <?php foreach ($news as $value) { str .= "<p>"; str .= "<b>" .$value['date']. "</b><br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327976 Share on other sites More sharing options...
calabiyau Posted August 19, 2007 Share Posted August 19, 2007 aren't you missing the dollar sign in front of your str var in the foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327979 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 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; } Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327980 Share on other sites More sharing options...
goocharlton Posted August 19, 2007 Author Share Posted August 19, 2007 Cheers mate, spot on. Quote Link to comment https://forums.phpfreaks.com/topic/65663-solved-variable-running-when-it-shouldnt/#findComment-327984 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.