/    Sign up×
Community /Pin to ProfileBookmark

I know what you’re thinking: “Oh no, he’s back with more GET questions using that crazy showthread.php example.” Well, you’re right:p ?

I’ve been studying more on the PHP $_GET array, and I don’t understand something. Take this example:

[code=php]<?php // page leading to particular thread

$a = 8;

$b = 15;

echo // thread URL;
?>

<?php // showthread.php

$_GET[a];
$_GET[b]; ?>[/code]

So if you followed that URL, the URL would be something to the effect of [url]http://www.example.org/showthread.php?a=8&b=15[/url]. My question is how do you set up showthread.php correctly? Obviously, there would be many threads; it wouldn’t necessarily know it would be getting $a or $b; it might be getting a thread with variables such as $c and $d. You would have to set showthread.php up somehow where it just knew it would be getting SOME sort of thread information (not just $a and $b), wouldn’t you?

If some of my logic is off, I thank you for your patience; I’m well aware I might have made some mistakes in the examples:o

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@chazzyAug 07.2008 — No... setting up something like what you're describing (ie having any variable contain the thread information) would result in either really cumbersome and slow processing, or huge security threats, or both. you're better off keeping the variable the same, and only looking at that one variable

[code=php]
<?php
$threadId = $_GET['thread'];
?>
[/code]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 07.2008 — But if you used that bit of code, how would the page know which thread to get?
Copy linkTweet thisAlerts:
@MrCoderAug 07.2008 — What do $a and $b relate to?
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 07.2008 — Absolutely nothing. I'm just using them as examples to get my point across.
Copy linkTweet thisAlerts:
@MrCoderAug 07.2008 — I'm sorry to say, your question makes no sense to me.

What are you asking for?
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 07.2008 — Take the forum we're in right now (the PHP forum). Then take the URL to this thread (http://www.webdeveloper.com/forum/[B]showthread.php?t=188335[/B]). I made the last bit of the URL bold to emphasize my point.

Showthread.php is just a page; it doesn't display the thread unless it uses $_GET to retrieve the thread information. However, there are dozens of threads in this forum; showthread.php can't possibly always be set as $_GET['t=188335'], because it would be getting the same thread over and over. Somehow, showthread.php knows what to do with whatever thread information is sent to it. What I'm saying is it's got to know somehow what thread it's getting, even though it knows there are tons of different threads. $t isn't always going to be assigned the value of 188335; that is just ONE possible value because that's one thread that exists out of hundreds of threads.

Please tell me you understand; I've tried to explain this to lots of different people, and I'm quickly running out of ways to word it:o
Copy linkTweet thisAlerts:
@MrCoderAug 07.2008 — showthread.php would contain something like this...

[code=php]
$sql = "SELECT threads_content FROM threads WHERE threads_id = '".(int)$_GET["t"]."' LIMIT 1";
$threads = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($threads) == 1)
{
// Found the thread, display it.
$thread = mysql_fetch_assoc($threads);
echo $thread["threads_content"];
die();
} else {
die("Cannot find the thread you requested");
}
[/code]


Very simple example, but I hope it helps?

There would be a lot more to it then above, link tables for user data, posts within a thread etc.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 07.2008 — Thank you very much for your willingness to help (I really appreciate it), but it still doesn't answer by question.

showthread.php?t=00000

showthread.php?t=11111

[code=php]<?php // there are links to two different threads above

$_GET['t'];
?>[/code]


$t can be ANY thread. See what I'm saying? How does it get the right thread?
Copy linkTweet thisAlerts:
@MrCoderAug 07.2008 — <i>
</i>Forum
Forums
"PHP"
Threads
"Help with my PHP"
"What is $_GET"


The thread ID ("t") is generated when the thread is created (User posts a new thread) based off the last thread number plus one.

This kind of ID is normally a database auto-increment integer value.

So the page that links to showthread.php pulls the name of the thread and the ID ("t") of the thread when it is displaying the list of threads.

[code=php]
<?php

$sql = "SELECT threads_id, threads_name FROM threads";
$result = mysql_query($sql);

while($thread = mysql_fetch_assoc($result))
{
?>
<a href="showthread.php?t=<?php echo $thread["threads_id"]; ?>" >
<?php echo $thread["threads_name"]; ?>
</a>
<?php
}
?>
[/code]
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 07.2008 — But what I'm saying is how does showthread.php get the right thread? All of the threads have their information stored in the $t variable. If a user clicks the link to thread1, what's to keep showthread.php from displaying thread2 by mistake, since all showthread.php is doing is $_GET['t']? There are a lot of different $ts out there when there are multiple threads?
Copy linkTweet thisAlerts:
@MrCoderAug 07.2008 — Because you tell it what "t" is when creating the link that the user clicks.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 07.2008 — So it just depends on which link the user clicks?
Copy linkTweet thisAlerts:
@SyCoAug 07.2008 — So it just depends on which link the user clicks?[/QUOTE]
Exactly!

You have a link, it has GET vars, the script takes and uses the GET vars to display the page.

The link is created usually by a script by first looking up the id of the item it wants to display then creating the link.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorAug 07.2008 — *breaths a sigh of relief*

It's taken me so long to get that answered. Thanks everyone?

Now, after showthread.php has received the thread ID, how does it go about displaying all of the appropriate information from the thread, like the different posts, each persons' username, etc. I'm guessing that they're stored in variables as well, but I don't think that I've seen a URL on a forum that has every single bit of information from the thread passed to the URL. Obviously, it would be a lot of work for the end of the URL to be showthread.php?t=1223&post=2&username=josephwitchard. Can someone please clarify?
Copy linkTweet thisAlerts:
@QuidamAug 07.2008 — The post itself is stored in the database with several different fields, such as:

Author

Title

Message

Creation time

Id

And so on..

So all you need is the Id of the thread which is unique, then when you GET that id, you can query the database to output all the other fields in that post.
Copy linkTweet thisAlerts:
@SyCoAug 07.2008 — All you need it the id to get the thread

The other information is stored in database tables. Different info in a different tables, eg user info in the users table, thread info in the thread table, comments in the comments table.

Using ids it can all be joined together to gather the required information to draw the page.

so the database tables could be filled like
<i>
</i>comments table
commentid threadid userid comment
1 1 35 I need some help
2 1 57 what is the problem
3 1 35 How do I...
4 1 12 Here is the answer
5 2 35 I have a new question
6 2 19 I can help
7 2 12 or a better way is....

users table
userid name
12 bob
19 sam
35 jon
57 bill

threads table
threadid thread_name
1 Heeelp
2 more help
3 Fries or onion rings?


Then if the threadid is passed in the url eg

www.site.com/showthread.php?threadid=1

we can select all comments with that id and join the tables on the userid as its common to both.

[code=php]$query="SELECT * FROM comments
INNER JOIN users ON comments.userid=users.userid
WHERE comments.threadid=".$_GET['threadid'];[/code]


would return a set like
<i>
</i>commentid threadid userid comment userid name
1 1 35 I need some help 35 jon
2 1 57 what is the problem 57 bill
3 1 35 How do I... 35 jon
4 1 12 Here is the answer 12 bob


or to get all the info
[code=php]$query="SELECT * FROM comments
INNER JOIN users ON comments.userid=users.userid
INNER JOIN threads ON comments.threadid=threads.threadid
WHERE comments.threadid=".$_GET['threadid'];
[/code]

would return a set like
<i>
</i>commentid threadid userid commentid userid name threadid thread_name
1 1 35 I need some help 35 jon 1 Heeelp
2 1 57 what is the problem 57 bill 1 Heeelp
3 1 35 How do I... 35 jon 1 Heeelp
4 1 12 Here is the answer 12 bob 1 Heeelp



so you now have all the information and can loop through using PHP functions to extract the info.

And maybe present it like this
<i>
</i>Thread Name - Heeelp

jon I need some help
bill what is the problem
jon How do i...
bob Here is the answer


(database lookup for following thread, retrieve the id, add it to the link)

Nextthead>>

showthread.php?threadid.php=2


This is a vey basic introduction but covered some pretty complex ideas. I'd recommend next you google for a tutorial (php mysql tutorial) as its been said over and over and there are alot of tutorials out there. I can recommend on as we all learn in different ways and like different style. Google try one if yo dont like it try another.

**READ THIS**

This example is not secure and is open to all sorts of security issues. The tutorials should cover the issues but come back and post when you have questions
Copy linkTweet thisAlerts:
@MrCoderAug 08.2008 — Very good answer SyCo ?
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...