TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Some one please explain me this part of code (http://www.talkphp.com/general/4483-some-one-please-explain-me-part-code.html)

dodgeqwe 06-05-2009 12:46 PM

Some one please explain me this part of code
 
I have completed PHP course , i know basics well .Im doing a case study on project to develop my PHP knowledge , if you have time, please explain me $GET[a],

1.why "a" is needed.
2.From where value is passed to "a"?

the above $GET[a] is repeated in many php pages on this project, i couldnt figure out , why?


if you need complete code,i could send you


Below is the some code of index page. Index page consist of book search and login section.


<?php
session_start();
include 'conn.php';

ConnectDB();
$errMsg = "";
if ($_GET[a]=='2')
{
//$a="search.php?txtsearch=" . $_POST[txtsearch];
$a="search.php?cboCategory=" . $_POST[cboCategory];
$_SESSION[searchtext] = $_POST[txtsearch];
$_SESSION[searchauthor] = $_POST[txtauthor];
$_SESSION[searchbook] = $_POST[txtbook];
$_SESSION[cboCategory] = $_POST[cboCategory];

header("Location: $a");
}

if ($_GET[a]=='1')
{

$sql="select nUserId,cUserType from Users where

Wildhoney 06-05-2009 01:02 PM

First off, welcome to the community :-) !

There are actually two types of these predominantly used to pass variables to various documents. You've probably seen them when browsing websites. Typically, but certainly not always, they consist of key/value pairs.

Example:

Code:

http://www.example.com/index.php?var1=I Love&var2=TalkPHP
Those are what we call GET variables. The other type are POST variables and they are sent with the HTTP request. They are in the same format as the GET variable (In key/value style) but are hidden away from the user as they are not present in the URL.

What I mean by key/value pairs is that the first item is the key, and the second being its value. Take a look at the following as shown in our example:

Code:

var2=TalkPHP
In there var2 would be the key, and TalkPHP is the value for that key.

Now that we know that, to answer your question. Using our example again we would extract the value TalkPHP from the URL using $_GET['var2'];. If you're currently knowledgeable about arrays, then $_GET is simply one big array that contains all the variables passed to it via GET (Not POST, for POST we have $_POST).

In your example, the GET variable a can be set to 1 or 2, or not at all. That is why there is an if statement checking its value.

To tie this up, using our above example of:

Code:

http://www.example.com/index.php?var1=I Love&var2=TalkPHP
We could print "I Love TalkPHP" by referencing the 2 keys (var1 and var2):

php Code:
/* GET: var1=I Love&var2=TalkPHP */
echo $_GET['var1'] . ' ' . $_GET['var2']
/* Prints: I Love TalkPHP

I hope I have explained it well for you :-) Please let me know if not.

dodgeqwe 06-05-2009 01:18 PM

Thank you , Wild honey for quick reply with informative examples . i think i confused you with my bad english sorry , my question was why does we use $GET[a] in the index page . Why that value is checked before using Login or Searching book.

Wildhoney 06-05-2009 01:24 PM

They're performing different actions based on what the a variable contains. I wouldn't recommend doing that though. Really you should have different pages for different actions.


All times are GMT. The time now is 06:47 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0