Jump to content

Recommended Posts

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>";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/227925-code-cleanup/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/227925-code-cleanup/#findComment-1175323
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/227925-code-cleanup/#findComment-1175872
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.