I think my code is horrible. Granted it works, but everything is a jumbled mess, and cluttered, everything shoved into one page. I was wondering if I could get some pointers on some ways to clean my code up.
Here’s an example of one of my pages:
[code=php]<?php
$id = $_GET[‘id’];
if($id==””){
header(“Location: restaurants.php”);
}
include ‘include/session.php’;
include ‘includes/prep3.php’;
include ‘includes/header.php’;
include ‘includes/functions.php’;
include ‘includes/happenings.class.php’;
include ‘includes/businesses.class.php’;
$query = “SELECT page FROM businesses b JOIN business_categories bc on bc.id = b.category WHERE b.id = ?”;
$stmt = $mysqli->stmt_init();
$stmt->prepare($query) or die (“Could not prepare statement:” . $stmt->error);
$stmt->bind_param(‘i’, $id) or die (“Could not bind parameters statement:” . $stmt->error);
$stmt->bind_result($page_name) or die (“Could not bind results:” . $stmt->error);
$stmt->execute() or die (“Could not execute statement:” . $stmt->error);
$stmt->fetch();
if($_SERVER[‘SCRIPT_NAME’] != (“/”.$page_name))
header(“Location: $page_name?id=$id”);
$stmt->reset();
//Lets pull the data
$stmt = $mysqli->stmt_init();
$stmt->prepare(“SELECT id, name, address, city, zip, lat, lng, phone, area, website, category, tags, description, hours, picture FROM businesses WHERE id=?”) or die (“Could not prepare statement:” . $stmt->error);
$stmt->bind_param(‘i’, $id) or die (“Could not bind parameters statement:” . $stmt->error);
$stmt->bind_result($id, $name, $address, $city, $zip, $lat, $lng, $phone, $area, $website, $category, $tags, $description, $hours, $picture) or die (“Could not bind results:” . $stmt->error);
$stmt->execute() or die (“Could not execute statement:” . $stmt->error);
$stmt->fetch();
$stmt->reset();
$stmt->prepare(“SELECT * FROM restaurants WHERE id=?”) or die (“Could not prepare statement:” . $stmt->error);
$stmt->bind_param(‘i’, $id) or die (“Could not bind parameters statement:” . $stmt->error);
$stmt->bind_result($id, $cuisine_1, $cuisine_2, $cuisine_3, $nightlife_1, $nightlife_2, $nightlife_3, $happy_hour, $late_happy_hour) or die (“Could not bind results:” . $stmt->error);
$stmt->execute() or die (“Could not execute statement:” . $stmt->error);
$stmt->fetch();
$stmt->close();
//Some header fun, sets header tags
$header = new HeaderClass();
$header->title = htmlspecialchars($name). ‘ – Bellevue WA & Eastside | Bellevue.com’;
$header->js = ‘js/restaurant.js’;
$header->js =’http://maps.google.com/maps?file=api&v=2&key=AB’;
$header->css = ‘css/restaurant.css’;
//$header->meta = Array(‘http-equiv’=>’Content-Type’, ‘content’=>’text/html; charset=utf-8’);
$header->meta = Array(‘name’=>’description’, ‘content’=>’some text here for search engines’);
unset($header);
if(!empty($cuisine_1))
$cuisines[] = $cuisine_1;
if(!empty($cuisine_2))
$cuisines[] = $cuisine_2;
if(!empty($cuisine_3))
$cuisines[] = $cuisine_3;
if(!empty($nightlife_1))
$nightlife[] = $nightlife_1;
if(!empty($nightlife_2))
$nightlife[] = $nightlife_2;
if(!empty($nightlife_3))
$nightlife[] = $nightlife_3;
$happenings = new Happenings();
$businesses = new Businesses();
$name = htmlspecialchars($name);
$category = htmlspecialchars($category);
?>
<div id=”leftCol”>
<div id=”rest-info”>
<div id=”rest-img”>
<?
if($picture==””)
$picture=”images/bellevue300x250.gif”;
else $picture = “uploads/images/business/”.$picture;
?>
<img src=”<?=$picture?>” alt=”Restaurant”/>
</div>
<div id=”rest-text”>
<h1><?=$name?></h1>
<?
$topinfo = “”;
if(is_array($cuisines))
$topinfo .= “<p>”.implode(“, “,$cuisines).”</p>”;
$rating = round($rating);
$stars=””;
for($i=0; $i<$rating; $i++){
$stars .= “<img src=”images/star.gif” alt=”full star” />”;
}
for($i=0; $i<(5-$rating); $i++){
$stars .= “<img src=”images/star_empty.gif” alt=”empty star” />”;
}
$topinfo .= “<p>$stars (“.(int)$row[‘votes’].”) Votes | <a href=’#’>rate & share your review</a></p><br />”;
if($website != “”)
$topinfo .= “<p><a target=’_blank’ href=’$website’>”.shorten_url($website).”</a></p>”;
if($phone != “”)
$topinfo .= “<p><strong>Phone:</strong> $phone</p><br />”;
if(is_array($nightlife))
$topinfo .= “<p><strong>Nightlife:</strong> “.implode(“, “,$nightlife).”</p>”;
if($happy_hour != “”)
$topinfo .= “<p><strong>Happy Hour:</strong> $happy_hour</p>”;
if($late_happy_hour != “”)
$topinfo .= “<p><strong>Late Happy Hour:</strong> $late_happy_hour</p><br />”;
if($tags != “”)
$topinfo .= “<p><strong>Tags:</strong> $tags</p>”;
echo $topinfo;
?>
</div>
<br class=”clear” />
</div>
<div id=”map-wrapper”>
<?
$full_address = $address.”, “.$city.” “.$zip;
?>
<p><?=$full_address?> | <a target=”_blank” href=”http://maps.google.com/maps?saddr=&daddr=<?=$full_address?>&hl=en”>Directions</a></p>
<div id=”map” style=”height:370px; width:460px;”></div>
<?
$businesses->whats_nearby();
?>
<br class=”clear” />
</div>
<div id=”rest-descrip”>
<div class=”menu-items”> <a id=”profile” class=”restcontrols selected”>Profile</a><a class=”restcontrols” id=”reviews”>Reviews</a><a class=”restcontrols” id=”happenings”>Happenings</a><br class=”clear” /></div>
<div class=”forms”>
<div id=”profile_control” class=”x”>
<? if($description!=””){ ?>
<h2>About <?=$name?></h2>
<p><?=$description?></p><br />
<? } ?>
<div class=”col-wrapper”>
<div class=”left-col”>
<?
if($hours != “”)
$botinfo .= “<p><strong>Hours:</strong> $hours</p>”;
if($happy_hour != “”)
$botinfo .= “<p><strong>Happy Hour:</strong> $happy_hour</p>”;
if($late_happy_hour != “”)
$botinfo .= “<p><strong>Late Happy Hour:</strong> $late_happy_hour</p><br />”;
echo $botinfo;
?>
</div>
<div class=”right-col”>
<h3>Happenings at <?=$name?></h3>
<p>Got an event or happening? <a href=”happenings_entry.php”>Post your event here!</a></p>
<br />
<?
$happenings->list_id_condensed($id);
?>
</div>
<br class=”clear” />
</div>
<div id=”rest-reviews”>
<h2>User reviews for <?=$name?></h2>
<p><a href=”#”>Log in and share your review</a></p>
</div>
</div>
<div id=”reviews_control”></div>
<div id=”happenings_control”>
<h2>Happenings at <?=$name?></h2>
<p>Got an event or happening? <a href=”happenings_entry.php”>Post your event here!</a></p>
<br />
<?
$happenings->list_id($id);
?>
</div>
</div>
</div>
</div>
<div id=”rightCol”>
<?php
include(“modules/search.php”);
include(“modules/rest_featured.php”);
include(“modules/happenings.php”);
?>
</div>
<?php
include(“includes/footer.php”);
?>
It uses some classes, includes some files, but it just looks [I]messy
My input forms are even more horrid and I can include one of those too if anyone is willing to help me pick up some better coding styles.
Any help will be greatly appreciated!