Help on code optimisation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spaceman33
Forum Newbie
Posts: 14
Joined: Mon Jul 25, 2005 3:35 pm

Help on code optimisation

Post by spaceman33 »

I think this is the best place to post this. I am not a business so presume that section of the forum is not for me.

I am a total novice to php. I started doing this yesterday and have come up with something that works. Problem is it takes minutes to load the php file. Before I added the block after Anticipated Rank, it would load straight away for me.

I have a form on a htm file, with fields where 5 figures are entered. Submit is then pressed to go to the php file to do calculations.

I have done this in Excel in the past, hence my calculations not taking that long to do.

There are 2 main blocks towards the bottom that are almost duplicated. I'm guessing that an array may be of use, but I can't get my head around it.

If anybody would be willing to offer some suggestions to optimising this code, it would be greatly appreciated.

This is for personal use, to be given out later when working, but no money or advertising by myself will be taking place.

Anyway, here is my code:

Code: Select all

<html>
<body>
<?php
$emaps=$_REQUEST['ExistingMaps'];
$fmaps=$_REQUEST['FutureMaps'];
$eactivity=$_REQUEST['ExistingActivity'];
$epopularity=$_REQUEST['ExistingPopularity'];
$eexperience=$_REQUEST['ExistingExperience'];
?>
<br />
<br />
<br />
<br />
<br />
<br />
Number of Existing Maps: <?php echo $emaps; ?>.<br />
Existing:<br />
 Activity: <?php echo $eactivity; ?>.<br />
 Popularity: <?php echo $epopularity; ?>.<br />
 Experience: <?php echo $eexperience; ?>.<br />
<br />
<?php $averageexisting = intval((($eactivity + $epopularity+$eexperience)/3)+0.5) ;?>

Current Rank: 
<?php if ($averageexisting <=100) $erank="General";
if ($averageexisting <90) $erank="Lieutenant General";
if ($averageexisting <85) $erank="Major General";
if ($averageexisting <80) $erank="Colonel";
if ($averageexisting <75) $erank="Major";
if ($averageexisting <70) $erank="Captain";
if ($averageexisting <65) $erank="First Lieutenant";
if ($averageexisting <60) $erank="Second Lieutenant";
if ($averageexisting <55) $erank="Sergeant Major";
if ($averageexisting <50) $erank="First Sergeant";
if ($averageexisting <45) $erank="Master Sergeant";
if ($averageexisting <40) $erank="Sergeant First Class";
if ($averageexisting <35) $erank="Staff Sergeant";
if ($averageexisting <30) $erank="Sergeant";
if ($averageexisting <25) $erank="Corporal";
if ($averageexisting <20) $erank="Specialist";
if ($averageexisting <15) $erank="Private First Class";
if ($averageexisting <10) $erank="Private";
if ($averageexisting <5) $erank="Recruit";
echo $erank;?>
<br />
<br />
Number of Future Maps: <?php echo $fmaps; ?>.<br />
<br />
Anticipated:  <br />
 Activity: <?php echo $factivity = round($eactivity*($emaps/2)/($fmaps/2),0) ;?><br />
 Popularity: <?php echo $fpopularity = round($epopularity*($emaps/2)/($fmaps/2),0) ;?><br />
 Experience <?php echo $fexperience = round($eexperience*($emaps/2)/($fmaps/2),0) ;?><br />
<br />
<br />
<?php $averagefuture = intval((($factivity + $fpopularity+$fexperience)/3)+0.5) ;?>
Anticipated Rank: 
<?php if ($averagefuture<=100) $frank="General";
if ($averagefuture<90) $frank="Lieutenant General";
if ($averagefuture<85) $frank="Major General";
if ($averagefuture<80) $frank="Colonel";
if ($averagefuture<75) $frank="Major";
if ($averagefuture<70) $frank="Captain";
if ($averagefuture<65) $frank="First Lieutenant";
if ($averagefuture<60) $frank="Second Lieutenant";
if ($averagefuture<55) $frank="Sergeant Major";
if ($averagefuture<50) $frank="First Sergeant";
if ($averagefuture<45) $frank="Master Sergeant";
if ($averagefuture<40) $frank="Sergeant First Class";
if ($averagefuture<35) $frank="Staff Sergeant";
if ($averagefuture<30) $frank="Sergeant";
if ($averagefuture<25) $frank="Corporal";
if ($averagefuture<20) $frank="Specialist";
if ($averagefuture<15) $frank="Private First Class";
if ($averagefuture<10) $frank="Private";
if ($averagefuture<5) $frank="Recruit";
echo $frank;?>
</body>
</html>
Again, any assistance would be greatly appreciated.

I know this may e messy, but I have no knowledge at all of php.
Last edited by spaceman33 on Mon Jul 25, 2005 4:12 pm, edited 3 times in total.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

You could just eliminate about 90% of your php tags and combine all of your php stuff into one <? ?> tag...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

you could also do something like:

Code: Select all

$rankNames=array("Recruit","Private","PFC","Spc","Cpl","Sgt","SSgt","SFC","MSgt","FSgt","SgtMaj","2Lt","1Lt","Capt","Maj","Col","MGen","Gen");

$rankIndex=floor($averageexisting/5); // (divide score by 5 and round down to nearest integer
if ($rankIndex>=count($rankNames)) $rankIndex=count($rankNames)-1;

$eRank=$rankNames[$rankIndex];

$rankIndex=floor($averagefuture/5); // (divide score by 5 and round down to nearest integer
if ($rankIndex>=count($rankNames)) $rankIndex=count($rankNames)-1;

$fRank=$rankNames[$rankIndex];
spaceman33
Forum Newbie
Posts: 14
Joined: Mon Jul 25, 2005 3:35 pm

Post by spaceman33 »

Thanks for the tips.

Reedited my post with most of the exessive php sections gone.

Didn't see that until you mentioned it.

Thanks for the code nielsene. Will adapt it into my code tomorrow. Bed time now I suppose seeing as web site down/slow that I was using for testing (must read up about installing php on the PC sometime).

I thought about dividing by 5 and rounding, just couldn't get my head around arrays at the moment.

Thanks again people.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Hi,

In addition to consolidating your <? ?> code, I would look at "switch"...

http://us2.php.net/manual/en/control-st ... switch.php

to replace your relentless if statements.

Good luck!

Josh
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think switch only works when you are checking for exact variable values.

You should be able to put your multitude if statements into some if..else statements. That way, not every if condition will be evaluated all the time - only as many as is needed.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Here is a cute switch (you shouln't even consider using this :p)

Code: Select all

switch (TRUE)
{
  case ($value >= 0 && $value < 10):
    do stuff;
    break;
  case ($value >= 10 && $value < 20):
    do other;
    break; 
}
spaceman33
Forum Newbie
Posts: 14
Joined: Mon Jul 25, 2005 3:35 pm

Post by spaceman33 »

Thanks people.

I have one (hopefully last question):

How to display a jpg file?

I've tried searching and found

Code: Select all

<?php
   $filename = 'http://members.lycos.co.uk/spaceman33/images/recruit.jpg';
   header('Content-type: image/jpeg');
   header('Content-transfer-encoding: binary');
   header('Content-length: '.filesize($filename));
   readfile($filename);
?>
but it doesn't seem to work.

I want to display an image determined by a value from earlier, so will be passing a variable to $filename(or $filename will be the web address for the graphic, hence me not being able to link to an image at a definite web address, the address for the picture will change depending on the value from earlier.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Could you just do

Code: Select all

$baseURL="http://members.lycos.co.ul/spaceman33/images";
echo "<img src=\"$baseURL/$eRank.jpg\" alt=\"$eRank Avatar\" \>";
You can't "inline" the binary of the images into the body of the HTML document. The approach you were using is used when you do something like:
http://example.net/getimage.php?filename=foo.jpg

Code: Select all

<?php   
$filename =$_GET["filename"];
header('Content-type: image/jpeg');   
header('Content-transfer-encoding: binary');   
header('Content-length: '.filesize($filename));   
readfile($filename);
?>
Ie when the ENTIRE PHP script only returns a specified image.
Last edited by nielsene on Tue Jul 26, 2005 3:11 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

When developping, start your code with:

Code: Select all

ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
The following code does work overhere:

Code: Select all

// ...

$filename = 'http://members.lycos.co.uk/spaceman33/images/recruit.jpg';
header('Content-type: image/jpeg');
readfile($filename);
But if you know the URL i think the following is easier ;)

Code: Select all

// ...

$filename = 'http://members.lycos.co.uk/spaceman33/images/recruit.jpg';
header('Location: ' . $filename);
spaceman33
Forum Newbie
Posts: 14
Joined: Mon Jul 25, 2005 3:35 pm

Post by spaceman33 »

Thanks nielsene and timvw.

I will try one and if that doesn't work will try the other.

Thanks for the errors tip timvw.

Looks like it's similar to Option Explicit in Excel, to provide more detailed errors.

Cheers.
Post Reply