Generate dynamic page based on link clicked.

Ask about a PHP problem here.
Post Reply
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Generate dynamic page based on link clicked.

Post by killfrog47 »

Hello all so here is my problem. I am making a page for my minecraft members just like http://somethingcraft.co.uk/stats.html Now the one problem I am having is that i dont know how to get the stats.html?player=playername I have been through google many times but im not sure what to search to get good answers. Any pointers?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Generate dynamic page based on link clicked.

Post by Helx »

Hi.

It's not all that difficult :D
If you had something like:
player.php?player=test1&foo=1&what=loljk
you could access it all in code like so:
<?php

echo $_GET['player']; // Displays "test1"
echo $_GET['foo']; // Displays "1"
echo $_GET['what']; // Displays "loljk"

?>
Just like that!

Any more questions or do you have it down pat?
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Generate dynamic page based on link clicked.

Post by killfrog47 »

Helx wrote:Hi.

It's not all that difficult :D
If you had something like:
player.php?player=test1&foo=1&what=loljk
you could access it all in code like so:
<?php

echo $_GET['player']; // Displays "test1"
echo $_GET['foo']; // Displays "1"
echo $_GET['what']; // Displays "loljk"

?>
Just like that!

Any more questions or do you have it down pat?
That helps out alot actually! So my link on my website could look something like this
echo '<a href="player.php?player='.$row['players'].'">'.$row['players'].'</a>';
and the link would lead to the page "player.php" and on that page my code would look something like
echo $_GET['player']; echo '<body>Stats</body>';
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Generate dynamic page based on link clicked.

Post by Helx »

I personally would set up the link like:
echo "<a href=\"player.php?player={$row['players']}\">{$row['players']}</a>";
(Note the double quotes and backslashes)

You might also want to be careful with displaying content direct from the GET variable, anybody can just type in what they want including code. Think about cleaning it, there's a good PHP function here: http://tpa.im/jm
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Generate dynamic page based on link clicked.

Post by killfrog47 »

Helx wrote:I personally would set up the link like:
echo "<a href=\"player.php?player={$row['players']}\">{$row['players']}</a>";
(Note the double quotes and backslashes)

You might also want to be careful with displaying content direct from the GET variable, anybody can just type in what they want including code. Think about cleaning it, there's a good PHP function here: http://tpa.im/jm
Alright that is a good idea. I took a look at the function though i thought i understood what was going on i started to think how i would implement it. What would the $value be? Is that my $row['players']? Then I noticed this
$sample = "<a href='#'>test</a>";
$sample = clean($sample);
echo $sample;
That also confused me haha. Please forgive me im still new to PHP. Im taking classes in college next semester to learn php better but till then im dead in the water lol
WAIT! I figured it out! lol Idk why it took me so long but I got it. It makes sense now. I had a moment of slowness. I forgot how to use functions =P also question: Why would you use the \"stuff\" anyway?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: Generate dynamic page based on link clicked.

Post by Helx »

killfrog47 wrote:Why would you use the \"stuff\" anyway?
Well, I like to be able to put variables straight into the string, so instead of having to break the whole thing by doing:
'something '. $var .' sdf';
I can just:
"something {$var} sdf";
Which requires that double quotes are used, and because there are double quotes in the string (<a href=""> etc) PHP picks up on that and thinks you're ending the string. Rather than changing the string to accommodate it (eg. by using single quotes on the href: href='#'), it's easier to just tell PHP to ignore it by using a backslash before each conflicting character.

It also applies if you use single quotes:
echo '<a href=\'lol\'>et cetera</a>';
TL;DR: It tells PHP that the double quotes in the string isn't code.

Hope it helps :)
User avatar
killfrog47
Posts: 106
Joined: Tue Mar 12, 2013 2:52 am
Location: Tempe, AZ
Contact:

Re: Generate dynamic page based on link clicked.

Post by killfrog47 »

Helx wrote:
killfrog47 wrote:Why would you use the \"stuff\" anyway?
Well, I like to be able to put variables straight into the string, so instead of having to break the whole thing by doing:
'something '. $var .' sdf';
I can just:
"something {$var} sdf";
Which requires that double quotes are used, and because there are double quotes in the string (<a href=""> etc) PHP picks up on that and thinks you're ending the string. Rather than changing the string to accommodate it (eg. by using single quotes on the href: href='#'), it's easier to just tell PHP to ignore it by using a backslash before each conflicting character.

It also applies if you use single quotes:
echo '<a href=\'lol\'>et cetera</a>';
TL;DR: It tells PHP that the double quotes in the string isn't code.

Hope it helps :)
That does help alot! Now I have the pages being created and everything is working =) I am cleaning all variables before they get entered to the db and I set up DB permissions to read only. Thank you for all your help =)
Post Reply