Best way to write this?

Hi all, I’ve written a script, I have three conditional statements all with the exact same echo values. I’m not keen on having THREE of the same echos as it’s becoming a little short of a pain every time I want to change something…I have to do it three times.

I have tried putting all of the echos into a variable and echo the variable but doesn’t seem to want to echo the variables inside. Any help to rewriting this would be appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php include('tpl_includes/defines.php'); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title>The Blog | <?php echo BUSINESS_NAME." - ".BUSINESS_SLOGAN ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="Joe Blogs" />
	<meta name="copyright" content="test 2010 - <?php echo date('Y'); ?>" />
	<meta name="email" content="hello@test.com" />
	<meta name="robots" content="all" />
	<meta name="distribution" content="global" />
	<meta name="rating" content="general" />
	<link href="http://www.test.com/blog/css/stylesheet.css" rel="stylesheet" type="text/css" />
	</head>
<body>
<div id="wrapper">
<?php include('tpl_includes/header.php');?>
<div class="postMaster">
<?php 
$url = "http://www.test.com/blog/";
$check = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if ($url==$check) {
echo '
<div class="containerAbout">
<img src="http://www.test.com/blog/images/the-team.jpg" width="266" height="190" class="img container" alt="Meet the team!" />
<div class="container" style="width: 244px; font-size: 13px; margin-top: 15px;"><span class="pinkColor" style="font-size: 20px;">So what\\'s it all about?</span><br/>It\\'s all about us, wanting to let our customers know what\\'s new, what\\'s in, what\\'s cool and what\\'s not. Keep updated with our latest developments along with news, special offers and latest brands that we\\'re carrying. Oh, and thanks for dropping by...!</div>
</div>';
}
?>
<?php 
$voice = $_GET['voice'];
$tag = $_GET['tag'];

include('tpl_includes/connection.php');
	if (isset($voice)){
		$query = mysql_query("SELECT * FROM posts WHERE user='$voice' ORDER BY `id` DESC");
		while ($row = mysql_fetch_array($query)){
		echo '<div class="postWrap">';
		echo '<div class="postTitle"><a href="article/'.$row['article_url'].'">'.$row['article_title'].'</a></div>';
		echo '<div class="postInfo fontGen">Posted on '.$row['date'].' at '.$row['time'].' by '.$row['user'].'</div>';
		echo '<div class="postContent">'.$row['article'].'</div>';
		echo '<div class="postTags fontGen"><span class="bold" style="color: #00acf1;">tags</span> '.$row['article_tags'].'</div>';
		echo '</div>';
		}
		}
	elseif (isset($tag)){
		$query = mysql_query("SELECT * FROM posts WHERE article_tags LIKE '%$tag%' ORDER BY `id` DESC");
		while ($row = mysql_fetch_array($query)){
		echo '<div class="postWrap">';
		echo '<div class="postTitle"><a href="article/'.$row['article_url'].'">'.$row['article_title'].'</a></div>';
		echo '<div class="postInfo fontGen">Posted on '.$row['date'].' at '.$row['time'].' by '.$row['user'].'</div>';
		echo '<div class="postContent">'.$row['article'].'</div>';
		echo '<div class="postTags fontGen"><span class="bold" style="color: #00acf1;">tags</span> '.$row['article_tags'].'</div>';
		echo '</div>';
		}
		}
	else {
		$query = mysql_query("SELECT * FROM posts ORDER BY `id` DESC");
		while ($row = mysql_fetch_array($query)){
		echo '<div class="postWrap">';
		echo '<div class="postTitle"><a href="article/'.$row['article_url'].'">'.$row['article_title'].'</a></div>';
		echo '<div class="postInfo fontGen">Posted on '.$row['date'].' at '.$row['time'].' by '.$row['user'].'</div>';
		echo '<div class="postContent">'.substr($row['article'],0,400).'...</div>';
		echo '<div class="postTags fontGen"><span class="bold" style="color: #00acf1;">tags</span> '.$row['article_tags'].'</div>';
		echo '</div>';
		}
	}
?>
</div>
<!-- BOF Filters -->
<div id="filters">
<div id="filterTags">
<div class="fontGen voicesBox"><a href="http://www.test.com/blog/default/shobrook/">shobrook</a>, <a href="default.php?tag=amet">amet</a></div>
</div>
<div id="filterVoices">
<div class="fontGen voicesBox"><a href="http://www.test.com/blog/default/kriss">Show only posts by Kriss</a><br/> <a href="http://www.test.com/blog/default/gina">Show only posts by Gina</a></div>
</div>
<div id="recentPosts">
<div class="fontGen recentPostsContain">
<?php
$related = mysql_query("SELECT * FROM posts ORDER BY `posts`.`date` DESC LIMIT 5");
	while ($res = mysql_fetch_array($related)) {
	echo '<div class="relHold">';
	echo '<b>'.$res['article_title'].'</b><br/>';
	echo '<span style="font-size: 9px;">Posted '.$res['date'].'</span>';
	echo '</div>';
	}
?>
</div>
</div>
</div>
<!-- EOF Filters -->
<?php
include('tpl_includes/right_ads.php');
include('tpl_includes/footer.php');
?>
</div>
</body>
</html>

Why not put the echo statement into a function and call the function from each of the three places.

Hi, thanks for your message. I’ve created a function but it doesn’t to want to echo the variables within it. Any ideas what I can do?

function displayRows($row){
	echo '<div class="postWrap">';
	echo '<div class="postTitle"><a href="article/'.$row['article_url'].'">'.$row['article_title'].'</a></div>';
	echo '<div class="postInfo fontGen">Posted on '.$row['date'].' at '.$row['time'].' by '.$row['user'].'</div>';
	echo '<div class="postContent">'.$row['article'].'</div>';
	echo '<div class="postTags fontGen"><span class="bold" style="color: #00acf1;">tags</span> '.$row['article_tags'].'<div>';
	echo '</div>';
}

Further to my previous post, all sorted now, I just need to put a global in for the variables inside the echo’s. Thanks for your help dude, it’s appreciated :smiley: