Jump to content

Recommended Posts

I have got this code I just wondered if there was a better way.

<?php 
    $month = date("m"); 
    switch($month)
    {
        case 06:
            include('includes/no.php');
        break;
        case 07:
            include('includes/yes.php');
        break;
    }
?> 

 

Mod edit: change [m][/m] (manual/doc) tags to


tags

There are so many different answers to the question your asking. It comes down to your style of programming and your current methods. If your including files like this in your system you shouldn't really change the way your doing it as you then get inconsistencies with your code.

 

Also, wrap your code in code tags - [ code ] without spaces - because what you've done so far makes it difficult to help you at all.

$pages = array("jan.php", "feb.php", "mar.php", "apr.php", "may.php", "jun.php", "jul.php", "aug.php", "sep.php", "oct.php", "nov.php", "dec.php");
$month = date("m");
include($pages[$month - 1]);

 

Probably overkill now. Just giving you another option.

Here's a slightly different slant on the problem.

 

You should not have a bunch of different files that only differ in the content they contain. You should have one file, then use the power of the php server side scripting language to dynamically get or produce the correct content within that one file. This will mean that your displayed output will be consistent and easier to maintain or alter. To change the layout, you only have one place that needs changing and to add or change the content, you simply add or change the data that defines the content.

 

$pages = array("jan.php", "feb.php", "mar.php", "apr.php", "may.php", "jun.php", "jul.php", "aug.php", "sep.php", "oct.php", "nov.php", "dec.php");
$month = date("m");
include($pages[$month - 1]);

 

Probably overkill now. Just giving you another option.

 

More consolidated:

$pages = array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
$month = date("m");
include "$pages[$month - 1]".'.php';

 

But:

 

Here's a slightly different slant on the problem.

 

You should not have a bunch of different files that only differ in the content they contain. You should have one file, then use the power of the php server side scripting language to dynamically get or produce the correct content within that one file. This will mean that your displayed output will be consistent and easier to maintain or alter. To change the layout, you only have one place that needs changing and to add or change the content, you simply add or change the data that defines the content.

 

Agree 100%.

You should not have a bunch of different files that only differ in the content they contain. You should have one file, then use the power of the php server side scripting language to dynamically get or produce the correct content within that one file. This will mean that your displayed output will be consistent and easier to maintain or alter. To change the layout, you only have one place that needs changing and to add or change the content, you simply add or change the data that defines the content.

 

Must be too old, I do not know what you mean. I use the include in the index.php page. Usually a swf file that changes due to season. Would you explain what you meant in a different context and then I may understand.

I still don't know about including images.

You should not have a bunch of different files that only differ in the content they contain. You should have one file, then use the power of the php server side scripting language to dynamically get or produce the correct content within that one file. This will mean that your displayed output will be consistent and easier to maintain or alter. To change the layout, you only have one place that needs changing and to add or change the content, you simply add or change the data that defines the content.

 

Must be too old, I do not know what you mean. I use the include in the index.php page. Usually a swf file that changes due to season. Would you explain what you meant in a different context and then I may understand.

I still don't know about including images.

 

 

Instead of having multiple pages you can do one page with something like this in it:

 

switched_content.php

$month=$_GET['month'];
$month=strtolower($month); //Only necessary if switch statement cases are case sensitive - not sure
switch ($month)
case "jan":
$data="content here";
break;
case "feb":
$data="content here";
break;
case "mar":
$data="content here";
break;
case "apr":
$data="content here";
break;
case "jun":
$data="content here";
break;
case "jul":
$data="content here";
break;
case "aug":
$data="content here";
break;
etc...

 

Then on the page you want to include the item in:

 

$_GET['month']=date("M");
include 'switched_content.php';
echo $data;

 

This would pull the data inside the switch statement for the case of the current month. You could also condense it down to one file, but two files in my opinion is the way to go, keeps the bulk of the code out of the display file.

Instead of having multiple pages you can do one page with something like this in it:

 

switched_content.php

$month=$_GET['month'];
$month=strtolower($month); //Only necessary if switch statement cases are case sensitive - not sure
switch ($month)
case "jan":
$data="content here";
break;
case "feb":
$data="content here";
break;
case "mar":
$data="content here";
break;
case "apr":
$data="content here";
break;
case "jun":
$data="content here";
break;
case "jul":
$data="content here";
break;
case "aug":
$data="content here";
break;
etc...

 

Then on the page you want to include the item in:

 

$_GET['month']=date("M");
include 'switched_content.php';
echo $data;

 

This would pull the data inside the switch statement for the case of the current month. You could also condense it down to one file, but two files in my opinion is the way to go, keeps the bulk of the code out of the display file.

 

That seems to be the format I will use. The code didn't work but the idea is good. For some reason I had to insert{ } and change from jun to 06 and on the page M to m and it worked fine. I also managed to include an image with your idea. Thank you.

Usually a swf file that changes due to season. ...

I still don't know about including images.

Do you want to change a page or an image (flash or otherwise) depending on month?

 

include ('<img src='images/test.png'>'); didnt work

 

echo "<img src='images/test.png'> "; did work. just wanted to know how to insert image to include

 

For some reason I had to insert{ }

 

You mean after the switch condition, and the end of the switch; like this:

 

switch ($month){
case "jan":
$data="content here";
break;
case "feb":
$data="content here";
break;
case "mar":
$data="content here";
break;
case "apr":
$data="content here";
break;
case "jun":
$data="content here";
break;
case "jul":
$data="content here";
break;
case "aug":
$data="content here";
break;
etc...
}

 

Yeah, I forgot those. That is correct.

 

Just as a reminder, there's a "Topic Solved" button below the thread on the left. Click that once you find a solution.

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.