944,111 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 690
  • PHP RSS
Nov 2nd, 2009
0

Broken query string in PHP

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

PHP Syntax (Toggle Plain Text)
  1. <?
  2. if (!$_GET['day']) {
  3. $day = date('N');
  4. }
  5. elseif ($_GET['day']==1){ ?>
  6. <?php include("test1.php") ?>
  7. <? }else if($_GET['day']==2){ ?>
  8. <?php include("test2.php") ?>
  9. <? }else if($_GET['day']==3){ ?>
  10. <?php include("test3.php") ?>
  11. <? }else if($_GET['day']==4){ ?>
  12. <?php include("test4.php") ?>
  13. <? }else if($_GET['day']==5){ ?>
  14. <?php include("test5.php") ?>
  15. <? }else if($_GET['day']==6){ ?>
  16. <?php include("test6.php") ?>
  17. <? }else if($_GET['day']==5){ ?>
  18. <?php include("test7.php") ?>
  19. <? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?>
  20. invalid id (or page not found)
  21. <? } ?>
  22. ?>
  23. <?php
  24. $dat=date("w");
  25. switch ($dat)
  26. {
  27. case 1:
  28. include("test1.php");
  29. break;
  30. case 2:
  31. include("test2.php");
  32. break;
  33. case 3:
  34. include("test2.php");
  35. break;
  36. case 4:
  37. include("test4.php");
  38. break;
  39. case 5:
  40. include("test5.php");
  41. break;
  42. case 6:
  43. include("test6.php");
  44. break;
  45. }
  46. ?>

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!
Similar Threads
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Nov 2nd, 2009
0
Re: Broken query string in PHP
Basically what I'm trying to do... get the script to display a different page every day of the week.
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Nov 2nd, 2009
-1
Re: Broken query string in PHP
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['day'] 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 Syntax (Toggle Plain Text)
  1. <?php
  2. //if using 1 to 7 - the 'N' format
  3.  
  4. if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){
  5. $dat = $_GET['day'];
  6. }else{
  7. $dat = date('N');
  8. }
  9. switch ($dat)
  10. {
  11. case 1:
  12. include("test1.php");
  13. break;
  14. case 2:
  15. include("test2.php");
  16. break;
  17. case 3:
  18. include("test2.php");
  19. break;
  20. case 4:
  21. include("test4.php");
  22. break;
  23. case 5:
  24. include("test5.php");
  25. break;
  26. case 6:
  27. include("test6.php");
  28. break;
  29. case 7:
  30. include("test7.php");
  31. break;
  32. }
  33. ?>
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 954
Disgraced Poster
ardav is offline Offline
6,726 posts
since Oct 2006
Nov 3rd, 2009
1
Re: Broken query string in PHP
You could even simplify it further:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $dat = intval(@$_GET['day']);
  3. if($dat < 1 || $dat > 7) {
  4. $dat = date('N');
  5. }
  6. include("test{$dat}.html");
  7. ?>
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.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 3rd, 2009
0
Re: Broken query string in PHP
Click to Expand / Collapse  Quote originally posted by ardav ...
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['day'] 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.u...dule.php?day=3
(with the content changing per day).

Thanks for your help so far
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Nov 3rd, 2009
-1
Re: Broken query string in PHP
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.u...dule.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.


//EDIT

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

PHP Syntax (Toggle Plain Text)
  1. if(isset($_GET['day']) && is_int($_GET['day']) && $_GET['day'] > 0 && $_GET['day'] < 8){
  2. $dat = $_GET['day'];
  3. echo "Good querystring: " . $dat;
  4. }else{
  5. $dat = date('N');
  6. echo "Bad or no querystring, using today: " . $dat;
  7. }

Also, "case3:" later on refers to page2.php - should be page3.php.
Last edited by ardav; Nov 3rd, 2009 at 3:27 pm.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 954
Disgraced Poster
ardav is offline Offline
6,726 posts
since Oct 2006
Nov 3rd, 2009
0
Re: Broken query string in PHP
Click to Expand / Collapse  Quote originally posted by ardav ...
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.
Reputation Points: 15
Solved Threads: 0
Junior Poster
whitestream6 is offline Offline
169 posts
since Nov 2008
Nov 3rd, 2009
-1
Re: Broken query string in PHP
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.
Sponsor
Featured Poster
Reputation Points: 1067
Solved Threads: 954
Disgraced Poster
ardav is offline Offline
6,726 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to store files for a Content Management System (CMS)
Next Thread in PHP Forum Timeline: save file xls or txt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC