I tried looking on Google for info on this, but not much came to light.
I'm trying to do a query string that takes info from text files and renders them as PAGENAME.php?id=1 - without using a database, if possible.

My file is called test.php and this is its content:

<html> 
<body> 
<?php 
print("<B>This is 100 FM for North Loamshire. Info coming soon</B>"); 
?> 
</body> 
</html>

However, I would like to make pages in the format test.php?id=1, test.php?id=2, similar to how http://www.brmb.co.uk/schedule.asp is http://www.brmb.co.uk/schedule.asp?id=1

I know ASP and PHP are totally different, but the idea's the same - using a query string.

If anyone could help me it would be much appreciated. :icon_cheesygrin:

Recommended Answers

All 10 Replies

Hey,

What are you really trying to do? Are you trying to display different information on a page based on query string provided in url? If yes you could simply do this:

- when calling page: test.php?id=1

<html>
<body>

<? if($_GET['id']==1){ ?>
here comes the content for id #1
<? }else if($_GET['id']==2){ ?>
here comes the content for id #2
<? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?>
invalid id (or page not found)
<? } ?>

</body>
</html>

if you are trying to load the content from the text file you could simply substitute the php code with:

<?
$contents = file_get_contents('./text_file_'.$_GET['id'].'.txt', true);
if($contents)  print $contents;
else               print 'Page not found';
?>

which would load contents of text file located in the same directory named: text_file_1.php for id=1 etc...

Good Luck

//make it TAB delimited file

$thefile = "test.php" 

$fd = fopen ($thefile, "r");
while (!feof ($fd))
{
   $buffer = fgets($fd, 4096);
   $lines[] = $buffer;
}
fclose ($fd); 

//THE FIRST REFERENCE NUMBER INSIDE THE FILE SUBMITTED
$TheFirstRecord=trim($lines[0]);
$RefNum = explode("\t", $TheFirstRecord); //detect the TAB to explode

then put every $Refnum[] array value in your code.

Thanks to all for that - my site should work properly now.
One more question... I'm planning to use a switch statement so that it shows the correct page on certain days.

This is my code:

<html>
<body>

<? if($_GET['day']==1){ ?>
 <?php include("day1.php") ?>
<? }else if($_GET['day']==2){ ?>
 <?php include("day2.php") ?>
<? }else if($_GET['day']==3){ ?>
 <?php include("day3.php") ?>
 <? }else if($_GET['day']==4){ ?>
 <?php include("day4.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("day5.php") ?>
 <? }else if($_GET['day']==6){ ?>
 <?php include("day6.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("day7.php") ?>
<? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?>
invalid id (or page not found)
<? } ?>

</body>
</html>

If you do something like this:

<?php
$Day = date('N');
?>

This would return the number of the day E.G.
Monday = 1
Tuesday = 2
..
Friday = 5
..
Sunday = 7

If you do something like this:

<?php
$Day = date('N');
?>

This would return the number of the day E.G.
Monday = 1
Tuesday = 2
..
Friday = 5
..
Sunday = 7

Thanks for that. How would I use it with my code:

<html>
<body>

<? if($_GET['day']==1){ ?>
 <?php include("day1.php") ?>
<? }else if($_GET['day']==2){ ?>
 <?php include("day2.php") ?>
<? }else if($_GET['day']==3){ ?>
 <?php include("day3.php") ?>
 <? }else if($_GET['day']==4){ ?>
 <?php include("day4.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("day5.php") ?>
 <? }else if($_GET['day']==6){ ?>
 <?php include("day6.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("day7.php") ?>
<? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?>
invalid id (or page not found)
<? } ?>

</body>
</html>

Whenever I used it, it gave errors for me, but that's because I'm new to PHP! I'm past the beginner's stage, but still learning...

use switch my friend...

$i=$_GET['day'];


switch ($i) {
case 0:
    include("day0.php");
    break;
case 1:
    include("day1.php");
    break;
case 2:
     include("day2.php");
    break;
.
.
.
.
.
case 7:
      include("day7.php");
    break;
default:
    echo "page not found!!!!";
}

Thanks, rm_daniweb, that worked very well!
One question - how do I get it to automatically show the right page on the right day, e.g. it show's day 1's page on Monday etc. without people having to manually go to the URL , e.g. http://www.mysite.com/schedule.php?day=1

Hi

By using the below code it will automatically take your user to the page corresponding to the date given in the $day variable.
Try it first though because i'm very tired and should be asleep. I've probably made a simple mistake :P

Hope this helps anywho

<html>
<body>

<? 
if (!$_GET['day']) {
$day = date('N');
header('Location: http://www.yourwebsite.com?id=$day');
}
elseif($_GET['day']==1){ ?>
 <?php include("day1.php") ?>
<? }else if($_GET['day']==2){ ?>
 <?php include("day2.php") ?>
<? }else if($_GET['day']==3){ ?>
 <?php include("day3.php") ?>
 <? }else if($_GET['day']==4){ ?>
 <?php include("day4.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("day5.php") ?>
 <? }else if($_GET['day']==6){ ?>
 <?php include("day6.php") ?>
 <? }else if($_GET['day']==5){ ?>
 <?php include("day7.php") ?>
<? }else if(!is_numeric($_GET['id']) || $_GET['id']<1){ ?>
invalid id (or page not found)
<? } ?>

</body>
</html>

Thanks, rm_daniweb, that worked very well!
One question - how do I get it to automatically show the right page on the right day, e.g. it show's day 1's page on Monday etc. without people having to manually go to the URL , e.g. http://www.mysite.com/schedule.php?day=1

use the

header("Location: day0.php");
header("Location: day1.php");
header("Location: day2.php");
.
.
.
header("Location: day7.php");

hello whitestream6

i just saw your post, and different users of deniweb gave you great ideas and solutions of your problem.But i want to request you some thing. Use a new post for each problem. You have asked 3 questions in same post and even didnt marked it solved. If you will use different posts and marked them as solved then the users who are helping you, there ranking will increase and they will feel more confident. so keep it in you mind.

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.