Jump to content

Help With Formatting And Refactoring Of Code


spertuit

Recommended Posts

Good morning everyone,

I am trying to improve the speed of this website im working on and just improve my code in general. My pages are working, but sometimes I am getting connection errors to the database, database resides on the same server as the site, and I am aslo seeing slow load times. I am just trying to improve my code and learn the proper way to format everything, so if anyone has time please take a lot and leave some suggestions or anything you can to help. The site is www.tlgnewspaper.com if you want to take a look.

<?php require_once("_includes/session.php");?>
<?php require_once("_includes/connection.php");?>
<?php require_once("_includes/functions.php"); ?>
<?php include_once("_includes/formFunctions.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>The Lafourche Gazette</title>
<link type="text/css" href="_stylesheets/public.css" rel="stylesheet" />
<style type="text/css">
</style>
<!--[if lt IE 9]>=
<script type="text/javascript" src="_javascripts/modernizer.js"></script>
<![endif]-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
(function() {
   function createPlayer(jqe, video, options) {
    var ifr = $('iframe', jqe);
    if (ifr.length === 0) {
	    ifr = $('<iframe scrolling="no">');
	    ifr.addClass('player');
    }
    var src = 'http://www.youtube.com/embed/' + video.id;
    if (options.playopts) {
	    src += '?';
	    for (var k in options.playopts) {
		    src += k + '=' + options.playopts[k] + '&';
	    }
	    src += '_a=b';
    }
    ifr.attr('src', src);
    jqe.append(ifr);
   }
   function createCarousel(jqe, videos, options) {
    var car = $('div.carousel', jqe);
    if (car.length === 0) {
	    car = $('<div>');
	    car.addClass('carousel');
	    jqe.append(car);
    }
    $.each(videos, function(i, video) {
	    options.thumbnail(car, video, options);
    });
   }
   function createThumbnail(jqe, video, options) {
    var imgurl = video.thumbnails[0].url;
    var img = $('img[src="' + imgurl + '"]');
    var desc;
    var container;
    if (img.length !== 0) return;
    img = $('<img align="left">');
    img.addClass('thumbnail');
    jqe.append(img);
    img.attr('src', imgurl);
    img.attr('title', video.title);
    img.click(function() {
	    options.player(options.maindiv, video, $.extend(true, {}, options, {
		    playopts: {
			    autoplay: 1
		    }
	    }));
    });
    desk = $('<p class="yt-descript">' + video.title + '</p>');
    jqe.append(desk);
    desk.click(function() {
	    options.player(options.maindiv, video, $.extend(true, {}, options, {
		    playopts: {
			    autoplay: 1
		    }
	    }));
    });
   }
   var defoptions = {
    autoplay: false,
    user: null,
    carousel: createCarousel,
    player: createPlayer,
    thumbnail: createThumbnail,
    loaded: function() {},
    playopts: {
	    autoplay: 0,
	    egm: 1,
	    autohide: 1,
	    fs: 1,
	    showinfo: 1
    }
   };
   $.fn.extend({
    youTubeChannel: function(options) {
	    var md = $(this);
	    md.addClass('youtube');
	    md.addClass('youtube-channel');
	    var allopts = $.extend(true, {}, defoptions, options);
	    allopts.maindiv = md;
	    $.getJSON('http://gdata.youtube.com/feeds/users/' + allopts.user + '/uploads?alt=json-in-script&format=5&callback=?', null,	   function(data) {
		    var feed = data.feed;
		    var videos = [];
		    $.each(feed.entry, function(i, entry) {
			    var video = {
				    title: entry.title.$t,
				    id: entry.id.$t.match('[^/]*$'),
				    thumbnails: entry.media$group.media$thumbnail
			    };
			    videos.push(video);
		    });
		    allopts.allvideos = videos;
		    allopts.carousel(md, videos, allopts);
		    allopts.player(md, videos[0], allopts);
		    allopts.loaded(videos, allopts);
	    });
    }
   });
})();
$(function() {
   $('#player').youTubeChannel({
    user: 'tlgnewspaper'
   });
});
//]]>
});//]]>
function changeImage(a) {
    document.getElementById("img").src=a;
   }
</script>
<?php $CSS=1; require("calendar/calendar.php"); ?>
</head>
<body>
<?php include("_includes/header.php");?>
<section class = "wrapper">
<section class="col1">
<?php include("_includes/leftColumn.php");?>
</section>
<section class="col2">
<h1>Top News</h1>
<?php include("_includes/topArticle.php");?>
<?php include("_includes/newArticles.php");?>
<section class = "gallery">
<h2>Newest Photos</h2>
<h3>Click image to view the Gallery</h3>
<div id="thumb_img" style="cursor:pointer">
<ul>
<?php list ($title, $path, $description, $category) = getLastPhoto('school');?>
   <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>School</li></a>
<?php list ($title, $path, $description, $category) = getLastPhoto('clubs');?>
   <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>Clubs</li></a>
<?php list ($title, $path, $description, $category) = getLastPhoto('sports');?>
  <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>Sports</li></a>
<?php list ($title, $path, $description, $category) = getLastPhoto('user');?>
   <a><li onclick='changeImage("_images/_photo/<?php echo $path; ?>");'>User Submitted</li></a>
   </ul>
</div>​
<div id="main_img" style="width:350px">
<?php list ($title, $path, $description, $category) = getLastPhoto('school');?>
   <a href="galleryChoice.php"><img id="img" width="350px" src="_images/_photo/<?php echo $path; ?>"></a>
</div>
</section>
<section class = "test">
<h2>Video Gallery</h2>
<div id="player"></div>
</section>
</section>
<section class="col3">
<?php include("_includes/rightColumn.php");?>
</section>
</article>
<?php include("_includes/footer.php");?>

Link to comment
Share on other sites

The code you have posted is basically just a template'ed page being built using php include statements. Other than the php code in your included files, the only php statements are the getLastPhoto() function calls.

 

You would start by pinning down where the code spends the greatest amount of time, either in one or more of the included files or making calls to the getLastPhoto() function. You can use $some_var = microtime(true);, before and after any block of code to get the starting/ending times and subtract the two times to get the amount of time taken for any code block.

Link to comment
Share on other sites

Using a proper profiler will also help a lot, as it'll give you a detailed breakdown of everything that happens in your code and how long it takes.

Personally I'm using Zend Studios, but I'm sure there are others out there that doesn't cost money. As well as other commercial profilers, it's just to look around. ;)

Link to comment
Share on other sites

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.