longhollow Posted March 11, 2009 Share Posted March 11, 2009 I'm not a PHP guy, just cut and paste code sometimes. This works but it isn't very elegant. My page gets lots of views and I would like to reduce machine overhead and deliver the page as fast as possible. Any suggestions? Thanks! I'm creating a list of links on a page. Each day the link thats 7 days old drops off and a new one appears. The .txt files have the html link code for one day. <?$today = date("z",(time()-3600))+1;?> <?$today1 = date("z",(time()-3600));?> <?$today2 = date("z",(time()-3600))-1;?> <?$today3 = date("z",(time()-3600))-2;?> <?$today4 = date("z",(time()-3600))-3;?> <?$today5 = date("z",(time()-3600))-4;?> <?$today6 = date("z",(time()-3600))-5;?> <ul> <li><?INCLUDE "/usr/home/bcmurray/public_html/features/$today.txt";?></li> <li><?INCLUDE "/usr/home/bcmurray/public_html/features/$today1.txt";?></li> <li><?INCLUDE "/usr/home/bcmurray/public_html/features/$today2.txt";?></li> <li><?INCLUDE "/usr/home/bcmurray/public_html/features/$today3.txt";?></li> <li><?INCLUDE "/usr/home/bcmurray/public_html/features/$today4.txt";?></li> <li><?INCLUDE "/usr/home/bcmurray/public_html/features/$today5.txt";?></li> <li><?INCLUDE "/usr/home/bcmurray/public_html/features/$today6.txt";?></li> </ul></li> Link to comment https://forums.phpfreaks.com/topic/149003-any-better-way-to-do-this/ Share on other sites More sharing options...
Mchl Posted March 11, 2009 Share Posted March 11, 2009 This should do the same. <ul> <?php for ($i =1; $i >= -5; $i--) { echo "\t<li>"; include ("/usr/home/bcmurray/public_html/features/".date("z",(time()-3600))+$i.".txt"); echo "</li>\n"; } ?> </ul> Link to comment https://forums.phpfreaks.com/topic/149003-any-better-way-to-do-this/#findComment-782418 Share on other sites More sharing options...
samshel Posted March 11, 2009 Share Posted March 11, 2009 not sure if u can make it faster, but u can definitely reduce the lines of code.. <ul> <?php for($i=1; $i>=-5; $i--) { $today = date("z",(time()-3600))+$i; ?> <li><? INCLUDE "/usr/home/bcmurray/public_html/features/$today.txt";?></li> <?php } ?> </ul> PS: the code is not tested.. beaten by Mchl Link to comment https://forums.phpfreaks.com/topic/149003-any-better-way-to-do-this/#findComment-782420 Share on other sites More sharing options...
longhollow Posted March 12, 2009 Author Share Posted March 12, 2009 Thanks, Worked fine. My less code. Wish I could find out if this loop method has less machine overhead than the original line by line approach. Anyone have any idea? Link to comment https://forums.phpfreaks.com/topic/149003-any-better-way-to-do-this/#findComment-783373 Share on other sites More sharing options...
redarrow Posted March 12, 2009 Share Posted March 12, 2009 There both near to same speed. looping and using arrays just quicker. Link to comment https://forums.phpfreaks.com/topic/149003-any-better-way-to-do-this/#findComment-783380 Share on other sites More sharing options...
Mchl Posted March 13, 2009 Share Posted March 13, 2009 You can easily check using microtime <?php $timeStart = microtime(true); //code to test $timeStop = microtime(true); echo "total execution time: ".$timeStop - $timeStart; ?> The difference will most likely be negligible. Link to comment https://forums.phpfreaks.com/topic/149003-any-better-way-to-do-this/#findComment-783581 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.