Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Efficient PHP; how do I stop repeating this section of code?

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
0
0
GB
Well I have a working PHP page, but I'm annoyed because I'm repeating the exact same code twice:

Code:
		<tr bgcolor='" . $col . "'>
			<td>" . htmlentities($row['description']) . "</td>
			<td onclick=\"text('" . htmlentities($row['description']) . " " . $row['instgroup'] . " | Stock No: " . $row['stockNo'] . "')\">
			<span class='email' ><a href='#form'>email us</a></span></td>
			<td>" . $row['stockNo'] . "</td>";
			
			if($row['ssp']=='0.00') {
				echo "<td><span class='workshop'>In Workshop</span></td>";
			} else {
			echo "<td>&pound;" . number_format($row['ssp'],2) . "</td>";
			}

I tried putting it in a function, but that didn't work as the $_row['xxx'] fields are out of the fetch_array loop. What's the best way to do this?

Here' the full code:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
<link href="style_main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/functions.js"></script>
<script type="text/javascript" src="lib/ajax_framework.js"></script>

</head>
<body>

<?
include 'includes/header.shtml';
include 'includes/menu.shtml';
//HTML Variant<!--#include file="php/minibasket.inc.php" -->
include('php/minibasket.inc.php');

echo "<div id='middle'>\r\n\r\n";

// include connection:
include('php/connection.inc.php');
$c=0;

$query = "SELECT * FROM table ORDER BY instgroup";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
	
	if ($ig != $row['instgroup']) {
		$col ='#D9CDFF';
		
		if($c!='0') {
		echo "</table>
		<h1>" . $row['instgroup'] . "</h1>
		<table border='0' class='sh'>";
		} else {
			echo "<h1>" . $row['instgroup'] . "</h1><table border='0' class='sh'>";
		}
		
		echo "
		<tr class='header'>
			<th>Model</th>
			<th>Contact</th>
			<th>Stock No</th>
			<th>Price</th>
		</tr>
				
		
		<tr bgcolor='" . $col . "'>
			<td>" . htmlentities($row['description']) . "</td>
			<td onclick=\"text('" . htmlentities($row['description']) . " " . $row['instgroup'] . " | Stock No: " . $row['stockNo'] . "')\">
			<span class='email' ><a href='#form'>email us</a></span></td>
			<td>" . $row['stockNo'] . "</td>";
			
			if($row['ssp']=='0.00') {
				echo "<td><span class='x'>N/A</span></td>";
			} else {
			echo "<td>&pound;" . number_format($row['ssp'],2) . "</td>";
			}			
			
		
	} else {
		
	if($col=='#F9F8F8') {
	$col='#D9CDFF';
	} else {
	$col='#F9F8F8';}
	
	echo "
		<tr bgcolor='" . $col . "'>
			<td>" . htmlentities($row['description']) . "</td>
			<td onclick=\"text('" . htmlentities($row['description']) . " " . $row['instgroup'] . " | Stock No: " . $row['stockNo'] . "')\">
			<span class='email' ><a href='#form'>email us</a></span></td>
			<td>" . $row['stockNo'] . "</td>";
			
			if($row['ssp']=='0.00') {
				echo "<td><span class='workshop'>In Workshop</span></td>";
			} else {
			echo "<td>&pound;" . number_format($row['ssp'],2) . "</td>";
			}	
			
			
	} // End Else
	$ig = $row['instgroup'];
	$c++;
	
} // End Loop

echo "</table><h2>Instrument Contact Form</h2><a name='form'>
<span id='inst'></span>
</a>";

?>

<br style="clear:both">

</div><!--Close DIV Middle-->

</div><!--Close DIV Container-->

</body>
</html>
 
Put it into a function and pass the variable as an argument

Code:
function doloop($row){
stuff
}

while ($row =mysql_fetch_assoc($result)){
doloop(&$row);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top