Page 1 of 1
Generate dynamic page based on link clicked.
Posted: Sun May 19, 2013 9:48 am
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?
Re: Generate dynamic page based on link clicked.
Posted: Mon May 20, 2013 7:27 am
by Helx
Hi.
It's not all that difficult
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?
Re: Generate dynamic page based on link clicked.
Posted: Tue May 21, 2013 7:24 pm
by killfrog47
Helx wrote:Hi.
It's not all that difficult
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>';
Re: Generate dynamic page based on link clicked.
Posted: Tue May 21, 2013 7:43 pm
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
Re: Generate dynamic page based on link clicked.
Posted: Tue May 21, 2013 8:05 pm
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?
Re: Generate dynamic page based on link clicked.
Posted: Wed May 22, 2013 4:42 am
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
Re: Generate dynamic page based on link clicked.
Posted: Wed May 22, 2013 1:30 pm
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 =)