doubleJ Posted February 16, 2011 Share Posted February 16, 2011 Hello... I know, a lot of times, I hack some code together. It's not necessarily the most efficient or cleanest way to do it, but it works. I thought it might be useful to have a place where people (maybe just myself) could post a chunk of code that others could analyze and recommend a better way of doing it. This looks to see if a file for a particular date is available and, if it is, creates a link to it. This is a prime example of what looks like it is way too much code for the task at hand. You'll note that nothing is indented. This isn't how I normally code, but when putty pastes vim tabs, it turns into spaces (which is annoying). I left-justified for ease of copy/paste, as I had to do it for multiple days (not just Friday). Just so it's clear, the code is looking for 110211Audio.mp3 (as of this week). JJ <?php if (date("l")=="Friday") { if(file_exists("services/2011/".date("ymd"))."Audio.mp3") { echo "<a href='services/2011/".date("ymd")."Audio.mp3'>Listen To Friday Sermon</a>"; } } elseif (date("l")=="Saturday") { if(file_exists("services/2011/".date("ymd", strtotime("-1 day"))."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", strtotime("-1 day"))."Audio.mp3'>Listen To Friday Sermon</a>"; } } elseif (date("l")=="Sunday") { if(file_exists("services/2011/".date("ymd", strtotime("-2 day"))."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", strtotime("-2 day"))."Audio.mp3'>Listen To Friday Sermon</a>"; } } elseif (date("l")=="Monday") { if(file_exists("services/2011/".date("ymd", strtotime("-3 day"))."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", strtotime("-3 day"))."Audio.mp3'>Listen To Friday Sermon</a>"; } } elseif (date("l")=="Tuesday") { if(file_exists("services/2011/".date("ymd", strtotime("-4 day"))."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", strtotime("-4 day"))."Audio.mp3'>Listen To Friday Sermon</a>"; } } elseif (date("l")=="Wednesday") { if(file_exists("services/2011/".date("ymd", strtotime("-5 day"))."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", strtotime("-5 day"))."Audio.mp3'>Listen To Friday Sermon</a>"; } } elseif (date("l")=="Thursday") { if(file_exists("services/2011/".date("ymd", strtotime("-6 day"))."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", strtotime("-6 day"))."Audio.mp3'>Listen To Friday Sermon</a>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227925-code-cleanup/ Share on other sites More sharing options...
MasterACE14 Posted February 16, 2011 Share Posted February 16, 2011 could use a 'switch' http://php.net/manual/en/control-structures.switch.php <?php if ($i == 0) { echo "i equals 0"; } elseif ($i == 1) { echo "i equals 1"; } elseif ($i == 2) { echo "i equals 2"; } switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } ?> -php.net Quote Link to comment https://forums.phpfreaks.com/topic/227925-code-cleanup/#findComment-1175323 Share on other sites More sharing options...
doubleJ Posted February 17, 2011 Author Share Posted February 17, 2011 I'll check it out. Thanks... BTW... Would your name be referencing Masta Ace Inc.? JJ Quote Link to comment https://forums.phpfreaks.com/topic/227925-code-cleanup/#findComment-1175834 Share on other sites More sharing options...
jcbones Posted February 17, 2011 Share Posted February 17, 2011 As suggested: //A switch switch(date('l')) { case 'Saturday': $time = strtotime('-1 day'); break; case 'Sunday': $time = strtotime('-2 day'); break; case 'Monday': $time = strtotime('-3 day'); break; case 'Tuesday': $time = strtotime('-4 day'); break; case 'Wednesday': $time = strtotime('-5 day'); break; case 'Thursday': $time = strtotime('-6 day'); break; default: $time = time(); } if(file_exists("services/2011/".date("ymd", $time)."Audio.mp3")) { echo "<a href='services/2011/".date("ymd", $time)."Audio.mp3'>Listen To Friday Sermon</a>"; } //or an Array: $days = array(-2,-3,-4,-5,-6,0, -1); if(file_exists('services/2011/' . date('ymd',strtotime($days[date('w')] . ' day')) . 'Audio.mp3')) { echo "<a href='services/2011/" . date('ymd',strtotime($days[date('w')] . ' day')) . "Audio.mp3'>Listen To Friday Sermon</a>"; } Of course, provided UN-TESTED! as usual. Quote Link to comment https://forums.phpfreaks.com/topic/227925-code-cleanup/#findComment-1175872 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.