...

View Full Version : php noob trying to learn.



Keyser520
02-13-2006, 06:55 PM
I'm trying to get my hands wet with some simple php, as I've never used it before.

I know that I can read the contents of a file and print it out on screen by
$y=file_get_contents($a);
echo $y;

where $a is the name of the file.

What I want to do is have the users click different buttons, each of which would send them to a link such as /index.php?a=filename.txt or /index.php?a=filename2.txt or /index.php?a=filename3.txt

I can't figure out how to get my code to READ what that filename is.

I thought I could just send it to the url as /index.php?a=filename.txt and then do

$y=file_get_contents($a);
echo $y;

but it returns nothing.

Help please.

chump2877
02-13-2006, 06:59 PM
$y=file_get_contents($_GET['a']);
echo $y;

Keyser520
02-13-2006, 07:08 PM
awesome. thanks..

now what could i do to make the following happen?

Send a variable "a" , append a .txt to it, and then call it with the

$y=file_get_contents($_GET['a']);
echo $y;

vinyl-junkie
02-13-2006, 07:11 PM
$y=file_get_contents($_GET['a'].".txt");
echo $y;

chump2877
02-13-2006, 07:12 PM
$file = $_GET['a'] . ".txt";
$y=file_get_contents($file );
echo $y;

Keyser520
02-13-2006, 07:12 PM
awesome. thanks..

now what could i do to make the following happen?

Send a variable "a" , append a .txt to it, and then call it with the

$y=file_get_contents($_GET['a']);
echo $y;

answered my own question

<html>
<body>

<?php

$b=($_GET['a']).".txt";
echo $b;
$y=file_get_contents($_GET['a'].".txt");
echo $y;



?>

</html>
</body>



Now, will there be any problem with this method if more than one person is using the page at the same time? Will the variables get confused based on the last person to visit a site or is localally stored?

chump2877
02-13-2006, 07:15 PM
how many people open and read a file is only limited by the bandwidth on your server

Keyser520
02-13-2006, 07:21 PM
how many people open and read a file is only limited by the bandwidth on your server


ok, but i was asking if the variable being passed to it -- the "a" would change everytime someone clicked a different link.


Is there a way that I can assign the variable a certain string of text IF there is no variable passed in?

i.e if I am using the code

<?php
$b=($_GET['a']).".txt";
echo $b;
$y=file_get_contents($_GET['a'].".txt");
echo $y;
?>

then it will work find if the link is /index.php?a=welcome but I want it to display another message if the link is just /index.php

chump2877
02-13-2006, 07:29 PM
but i was asking if the variable being passed to it -- the "a" would change everytime someone clicked a different link.

yes, if a=file1 (in your URL), then $_GET['a'] will contain "file1"....$_GET['a'] equals whatever value "a" that was passed to it....so i thnk what you are asking is you can have multiple buttons pass "a" via the GET method, and you only need to declare $_GET['a'] once on the subsequent page...because you can only clikc on one button at a time...$_GET['a'] will reset itself the next time around when another button is clicked in which "a" has a different value...


if (empty($_GET['a']))
{
$file = "yourstring.txt"
}
else
{
$file = $_GET['a'] . ".txt";
}
$y=file_get_contents($file );
echo $y;

Keyser520
02-13-2006, 07:35 PM
yes, if a=file1 (in your URL), then $_GET['a'] will contain "file1"....$_GET['a'] equals whatever value "a" that was passed to it....so i thnk what you are asking is you can have multiple buttons pass "a" via the GET method, and you only need to declare $_GET['a'] once on the subsequent page...because you can only clikc on one button at a time...$_GET['a'] will reset itself the next time around when another button is clicked in which "a" has a different value...


if (empty($_GET['a']))
{
$file = "yourstring.txt"
}
else
{
$file = $_GET['a'] . ".txt";
}
$y=file_get_contents($file );
echo $y;


Ok, that code makes sense.. You've been a big help, and I will stop pestering you soon, I promise.

Do you have a book or course that you would recommend for someone wanting to become proficient in php?

The question that I was asking is this.

Imagine 3 users-- user 1 clicks on the link that brings them to /index.php?a=welcome and then user 2 clicks on the link for /index.php?a=aboutus .
When user 3 clicks on the link for /index.php will "a" have the value of "welcome" or of "aboutus" or of nul?

I need it to have a value of "nul"

chump2877
02-13-2006, 07:51 PM
lol, why are there 3 users? You can only control the experience of one user at a time....And how many different buttons/links are there?

Foremost, what is the larger picture here? What are you ultimately trying to do with this code?

As far as PHP references/books/tutorials, www.php.net is an excellent resource....but to actually learn PHP, I would get a book out of the library, or buy one from a bookstore (library is cheaper).....there are hundreds of books on PHP for beginners, with step-by-step tutorials thru the learning process...Sorry i cant think of any on hand, try Amazon.com

Keyser520
02-13-2006, 08:05 PM
lol, why are there 3 users? You can only control the experience of one user at a time....And how many different buttons/links are there?

Foremost, what is the larger picture here? What are you ultimately trying to do with this code?

As far as PHP references/books/tutorials, www.php.net is an excellent resource....but to actually learn PHP, I would get a book out of the library, or buy one from a bookstore (library is cheaper).....there are hundreds of books on PHP for beginners, with step-by-step tutorials thru the learning process...Sorry i cant think of any on hand, try Amazon.com

Ok, here is the "larger picture".

http://www.runeescape.net


Any ideas for better coding? :-)

Thanks for all your help, btw.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum