This is my code which is intended to produce an URL like the following:
www.mysite.com/test.php?day=1

<? 
if (!$_GET['day']) {
$day = date('N');
}
elseif ($_GET['day']==1){ ?>
 <?php include("test1.php") ?>
<? }else if($_GET['day']==2){ ?>
 <?php include("test2.php") ?>
<? }else if($_GET['day']==3){ ?>
 <?php include("test3.php") ?>
 <? }else if($_GET['day']==4){ ?>
 <?php include("test4.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("test5.php") ?>
 <? }else if($_GET['day']==6){ ?>
 <?php include("test6.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("test7.php") ?>
<? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?>
invalid id (or page not found)
<? } ?>
?>
<?php
$dat=date("w");
switch ($dat)
{
  case 1:
    include("test1.php");
    break;
  case 2:
   include("test2.php");
    break;
  case 3:
   include("test2.php");
    break;
  case 4:
   include("test4.php");
    break;
  case 5:
   include("test5.php");
    break;
    case 6:
   include("test6.php");
    break;
  }
  ?>

The query string does work, but shows the same page - test1.php, no matter what day I call up in the PHP URL.

Anyone managed to get this working, as I tried and tried but can't figure it out!

Basically what I'm trying to do... get the script to display a different page every day of the week.

Member Avatar for diafol

The querystring handling looks like just a shocking mess (sorry!). You've got short tags all over the place, multiple if/elseifs.

I really don't understand why you're using 'N' and 'w' - surely you need consistency?

You need to check for a $_GET and ensure that it's between 1 and 7 if using 'N' (Mon-Sun) or between 0 and 6 if using 'w' (Sun-Mon).

<?php
//if using 1 to 7 - the 'N' format

if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){
  $dat = $_GET['day'];
}else{
  $dat = date('N');
}  
switch ($dat)
{
  case 1:
    include("test1.php");
    break;
  case 2:
   include("test2.php");
    break;
  case 3:
   include("test2.php");
    break;
  case 4:
   include("test4.php");
    break;
  case 5:
   include("test5.php");
    break;
    case 6:
   include("test6.php");
    break;
    case 7:
   include("test7.php");
    break;
  }
  ?>

You could even simplify it further:

<?php
$dat = intval(@$_GET['day']);
if($dat < 1 || $dat > 7) {
    $dat = date('N');
}
include("test{$dat}.html");
?>

The intval function always returns a number, or 0 if the input is invalid, which would be the case if no "day" value was passed.

commented: Yes, very nice cleverclogs! +4

The querystring handling looks like just a shocking mess (sorry!). You've got short tags all over the place, multiple if/elseifs.

I really don't understand why you're using 'N' and 'w' - surely you need consistency?

You need to check for a $_GET and ensure that it's between 1 and 7 if using 'N' (Mon-Sun) or between 0 and 6 if using 'w' (Sun-Mon).

Your code almost worked... except it displayed the same page repeatedly, despite the contents of 1, 2, 3, 4, 5, 6 and 7.php all being different.

How would I fix this? This code is for my localhost radio station site (virtualhost name on Apache www.mylocalhostradiosite.co.uk)
and the URLs are meant to be:
http://www.mylocalhostradiosite.co.uk/schedule.php?day=3
(with the content changing per day).

Thanks for your help so far :)

Member Avatar for diafol

Your code almost worked... except it displayed the same page repeatedly, despite the contents of 1, 2, 3, 4, 5, 6 and 7.php all being different.

How would I fix this? This code is for my localhost radio station site (virtualhost name on Apache www.mylocalhostradiosite.co.uk)
and the URLs are meant to be:
http://www.mylocalhostradiosite.co.uk/schedule.php?day=3
(with the content changing per day).

Thanks for your help so far :)

Your links don't work.

BTW: Atli's solution is much nicer than mine - I attempted to fix your code, whereas he's done a complete makeover. His code works as long as you synchronize the 'day number' with the include file name. However, if you were to have different filenames for certain days or even the same filenames for certain days (e.g. Sat/Sun both using the same file), the code wouldn't work 'as is'.

With regard to forcing an integer value on any $_GET value, this is useful if you don't care about assigning a default day or applying a day if the value is 'close'. This should be fine if using the date('N') - Atli points out, valid days = 1 to 7, therefore a totally off the wall value will return a '0' - which can be used to throw an error. However, if you go with the 0 to 6 (date('w')), which I have to admit to favouring, you will find Sunday as the default page for incorrect querystrings.

Anyway, enough rambling. I'd give Atli's solution a go as it's much prettier.:cool:


//EDIT

If you want to see what's going on, try this:

if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){
  $dat = $_GET['day'];
  echo "Good querystring: " . $dat;
}else{
  $dat = date('N');
  echo "Bad or no querystring, using today: " . $dat;
}

Also, "case3:" later on refers to page2.php - should be page3.php.

Your links don't work.

BTW: Atli's solution is much nicer than mine - I attempted to fix your code, whereas he's done a complete makeover. His code works as long as you synchronize the 'day number' with the include file name. However, if you were to have different filenames for certain days or even the same filenames for certain days (e.g. Sat/Sun both using the same file), the code wouldn't work 'as is'.

With regard to forcing an integer value on any $_GET value, this is useful if you don't care about assigning a default day or applying a day if the value is 'close'. This should be fine if using the date('N') - Atli points out, valid days = 1 to 7, therefore a totally off the wall value will return a '0' - which can be used to throw an error. However, if you go with the 0 to 6 (date('w')), which I have to admit to favouring, you will find Sunday as the default page for incorrect querystrings.

Also, "case3:" later on refers to page2.php - should be page3.php.

I tried atli's suggestion, and it partly worked.
As for the URLs not being able to be found, they're only accessible locally, mapped via virtualhosts in Apache and a HOSTS file - otherwise non-existent on the Internet.

Member Avatar for diafol

Sounds like a stupid question, but are the contents of the different files actually different? Try the 'echo' code in my previous post to see which day is being passed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.