Jump to content

Recommended Posts

As you will see below, I have a php file with loads of repeating code.

Is there a way I can simplify my script a little.  Im not an advanced user but I was thinking somwhere along the lines of,

having the 'type' in my sql query as an array so that the type would change after each query.

 

What would you reccommend.  I think my code looks like its wasting a lot of space.

 

 

<?php 
  include($_SERVER['DOCUMENT_ROOT'].'/include/db.php');
?>
<title>Untitled Document</title>
<style type="text/css">
<!--
-->
</style>
<link href="header.css" rel="stylesheet" type="text/css" />
<link href="a.css" rel="stylesheet" type="text/css" />
</head><body>

<div id="bodywrap">
<div id="banner">
<div id="bannerleft"></div>
<div id="logo"><img src="i-stevenage.png" height="100%" width="100%" alt="I-stevenage"/></div>
<div id="bannerright"><div id="navbar" title="">	 
						 <div id="b1"><p class="centre"><a href="index.html">Directory</a></p></div>
    					 	 <div id="b2"><p class="centre"><a href="business.html">Add Your Business</a></p></div>
   						 	 <div id="b3"><p class="centre"><a href="contact.html">Contact Us</a></p></div>
    	</div><!-- end of navbar-->
</div><!--end of bannerright-->
</div><!-- end of banner-->

<div id="wrapper">
<div id="listhold">

<!-- Next Catergory -->
<div class="list"><h1>Cars and Travel</h1>
<?php
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Cars and Travel' AND confirmed ='Yes' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
<h1>Legal</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Legal' AND confirmed ='Yes' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
<h1>Retail</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Retail' AND confirmed ='Yes' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
<h1>Hygene</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Hygene' AND confirmed ='Yes' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>	
<h1>Refuse</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Refuse' AND confirmed ='Yes' ORDER BY name");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>	
</div><!--end of list  -->
<!----------------------------------------------------------------------------------------------------------------------------------------------------->
<!----------------------------------------------------------------------------------------------------------------------------------------------------->
<!-- Next Catergory -->
<div class="list"><h1>Skilled Trades</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Skilled Trades' AND confirmed ='Yes' ORDER BY subtype");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach ($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
</div><!--end of list-->
<!----------------------------------------------------------------------------------------------------------------------------------------------------->
<!----------------------------------------------------------------------------------------------------------------------------------------------------->
<!-- next catergory -->
<div class="list">
<h1>Care</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Care' AND confirmed ='Yes' ORDER BY subtype");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach ($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
<h1>Education</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='education' AND confirmed ='Yes' ORDER BY subtype");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach ($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
<h1>Financial</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='financial' AND confirmed ='Yes' ORDER BY subtype");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach ($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
<h1>Home and Garden</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='home and garden' AND confirmed ='Yes' ORDER BY subtype");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach ($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>
<h1>Wellbeing</h1>
<?php
$nt = array();
$title ="TITLE GOES HERE";
$query = mysql_query("SELECT DISTINCT subtype FROM business WHERE type ='Wellbeing' AND confirmed ='Yes' ORDER BY subtype");
echo mysql_error();
while($ntx=mysql_fetch_row($query)) $nt[] = $ntx[0];
$i = -1;
foreach ($nt as $value)
{$i++;
$FileName = str_replace(' ','_',$nt[$i]) . ".php";
$FileUsed = str_replace('_',' ',$nt[$i]);
echo "<a href='" . str_replace(' ','_',$nt[$i]) . ".php?title=$title&subtype=$FileUsed'>"  . ucwords(strtolower (($nt[$i]))) . "</a>" . "<br/>";
$FileHandle = fopen($FileName, 'w') or die("cant open file");
$pageContents = file_get_contents("header.php");
fwrite($FileHandle,"$pageContents");}
fclose($FileHandle);
?>

Link to comment
https://forums.phpfreaks.com/topic/240256-makingmy-code-shorter/
Share on other sites

Using an array with a foreach or similar style loop would certainly make this code alot shorter, which would probably make it alot more manageable and easier to update (IE instead of copying that whole block again to add a section, you just add an entry onto the array.)

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.