Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need hep with GET variable
Old 07-02-2010, 09:40 PM Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
How do I make something like this

yoursite.com/?id=1&act=act

I am basically asking how does the "&" part of it work?
Direct is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-02-2010, 09:47 PM Re: Need hep with GET variable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
It's a series of parameters you are passing. The & or & seperates the parameters apart from each other.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 09:54 PM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
Any way you can explain just a little I am new to PHP. Don't give me the answer just point me in the right direction, Thanks
Direct is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 10:10 PM Re: Need hep with GET variable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
index.php?id=1&act=act&this=that

The parameters are first taken from the URL string by PHP, and it is set off by the "?"

So the above example would be digested by PHP as: id=1&act=act&this=that

Then it is split into key=value pairs by the "&"

This would become:
id=1
act=act
this=that


Then each pair is split into key => value array into the $_GET array by the "="

This would result in the following array using print_r($_GET):
Array {
[id] => 1,
[act] => act,
[this] => that
}
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 10:21 PM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
ah so I would just set a if statement that executes code if the $_GET variables correspond? like if id=1 and act=act
Direct is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 10:27 PM Re: Need hep with GET variable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Yes, but it would be used like this:

if ($_GET['id'] == 1 AND $_GET['act'] == 'act')
{
...
}
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 10:52 PM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
lol Yeah that's what I meant I am just lazy

Another question I have is how to do this with forms. Like the first part of the form is the user selects a type then they select the second type

I need to keep it on one page like instead of going from

index.php?id=1

to

index.php?act=type2

I need

index.php?id=1&act=type2

I was thinking something with sessions but I am not sure
Direct is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 11:07 PM Re: Need hep with GET variable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
You could create a new merged array and then build the param string from that:

$params = array('act' => 'type2');

$params = array_merge($_GET, $params);

echo 'index.php?' . http_build_query($params, '', '&');
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 11:18 PM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
Yeah but my problem is I need to keep the selection from the first page Like if they choose

type1 that might have different choices on the next form than say type2

Here is a rough draft of my index page

PHP Code:
$link $_SERVER['PHP_SELF'] ;
      
      
$training_link $link ."?id=2" ;
      
$profile_link $link ."?id=1" ;
      
$occ_train_link $link ."?id=3" ;
      
      
      echo
"<a href='$profile_link'>profile</a> <br/>";
      echo
"<a href='$training_link'>Training</a> <br/>";
      echo
"<a href='$occ_train_link'>occ train</a> <br/>";
       
      if (
$_GET['id'] == "1"){
           
$user profile($user);
          }
      elseif (
$_GET['id'] == "2"){
           require(
"training.php") ;
       
$user training($user) ;
          }
      elseif (
$_GET['id'] == "3"){
           require(
"occtrain.php") ;
        
$user occ_train($user) ;
           } 
Now here is the page where I am sending the data for the "training"
PHP Code:
<?php
function training($user){
//check for Input
if(isset($_GET['train']) ){

if(
$_GET['train']=="weap"){
     if(
$_POST){
         if(
$_POST['type'] == "offense"){
             
$gain $user['stamina'] * 0.2;
             
$user['weapoff'] += $gain ;     
             
$expgain $gain ;
         
$user['exp'] += $expgain ;
             
$user['expneed'] -= $expgain 
             
$user['stamina'] -= $gain 5;
             
$user['maxsta'] += $gain 4;
             
             echo 
"You have gained " $gain " Weapon Offense! <br/>" ;
             echo 
"Doing this you have used " $gain " stamina! <br/>" ;
             echo 
"Your maximum stamina has increased by " $gain "!" 
             
            }
         if(
$_POST['type'] == "defense"){
             
$gain $user['stamina'] * 0.2;
             
$user['weapdef'] += $gain ;     
             
$expgain $gain ;
         
$user['exp'] += $expgain ;
             
$user['expneed'] -= $expgain 
             
$user['stamina'] -= $gain 5;     
             
$user['maxsta'] += $gain 4;
             
             echo 
"You have gained " $gain " Weapon Defense! <br/>" ;
             echo 
"You have used " $gain " stamina! <br/>" ;
             echo 
"Your maximum stamina has increased by " $gain "!" ;
             
            }
        }
     
     
     if( ! 
$_POST){
         echo
"<form method='post' action=''>
          <select name='type'>
          <option value='offense'>Weapon Offense </option>
          <option value='defense'>Weapon Defense </option>
          </select>
          <input type='submit' value='Train' />
          </form>" 
;
        }
    }
    

elseif(
$_GET['train'] == "snip"){

     if(
$_POST){
         if(
$_POST['type'] == "offense"){
             
$gain $user['stamina'] * 0.2;
             
$user['snipoff'] += $gain ;     
             
$expgain $gain ;
         
$user['exp'] += $expgain ;
             
$user['expneed'] -= $expgain 
             
$user['stamina'] -= $gain 5;     
             
             echo 
"You have gained " $gain " Sniper Offense! <br/>" ;
             echo 
"You have used " $gain " stamina! <br/>" ;
             echo 
"Your maximum stamina has increased by " $gain "!" ;
            }
         if(
$_POST['type'] == "defense"){
             
$gain $user['stamina'] * 0.2;
             
$user['snipdef'] += $gain ;     
             
$expgain $gain ;
         
$user['exp'] += $expgain ;
             
$user['expneed'] -= $expgain 
             
$user['stamina'] -= $gain 5;     
             
             echo 
"You have gained " $gain " Sniper Defense <br/>" ;
             echo 
"You have used " $gain " stamina <br/>" ;
             echo 
"Your maximum stamina has increased by " $gain "!" ;
            }
        }
     
     
     if( ! 
$_POST){
         echo
"<form method='post' action=''>
          <select name='type'>
          <option value='offense'>Sniper Offense </option>
          <option value='defense'>sniper Defense </option>
          </select>
          <input type='submit' value='Train' />
          </form>" 
;
        }
    }
         
     


elseif(
$_GET['train'] == "phys"){
     if(
$_POST){
         if(
$_POST['type'] == "offense"){
             
$gain $user['stamina'] * 0.2;
             
$user['physoff'] += $gain ;     
             
$expgain $gain ;
         
$user['exp'] += $expgain ;
             
$user['expneed'] -= $expgain 
             
$user['stamina'] -= $gain 5;     
             
             echo 
"You have gained " $gain " Weapon Offense! <br/>" ;
             echo 
"You have used " $gain " stamina! <br/>" ;
             echo 
"Your maximum stamina has increased by " $gain "!" ;
            }
         if(
$_POST['type'] == "defense"){
             
$gain $user['stamina'] * 0.2;
             
$user['physdef'] += $gain ;     
             
$expgain $gain ;
         
$user['exp'] += $expgain ;
             
$user['expneed'] -= $expgain 
             
$user['stamina'] -= $gain 5;     
             
             echo 
"You have gained " $gain " Physical defense! <br/>" ;
             echo 
"You have used " $gain " stamina! <br/>" ;
             
             echo 
"Your maximum stamina has increased by " $gain "!" ;
            }
        }
     
     
     if( ! 
$_POST){
         echo
"<form method='post' action=''>
          <select name='type'>
          <option value='offense'>Physical Offense </option>
          <option value='defense'>Physical Defense </option>
          </select>
          <input type='submit' value='Train' />
          </form>" 
;
        }
    }
         
     


elseif(
$_GET['train'] == "intel"){

     
$gain $user['stamina'] * 0.2;
     
$user['intel'] += $gain ;     
     
$expgain $gain ;
     
$user['exp'] += $expgain ;
     
$user['expneed'] -= $expgain 
     
$user['stamina'] -= $gain 5;     
     
    }

elseif(
$_GET['train'] == "str"){

     
$gain $user['stamina'] * 0.2;
     
$user['str'] += $gain ;     
     
$expgain $gain ;
     
$user['exp'] += $expgain ;
     
$user['expneed'] -= $expgain 
     
$user['stamina'] -= $gain 5;     
     
    }
  
elseif(
$_GET['train'] == "speed"){

     
$gain $user['stamina'] * 0.2;
     
$user['speed'] += $gain ;     
     
$expgain $gain ;
     
$user['exp'] += $expgain ;
     
$user['expneed'] -= $expgain 
     
$user['stamina'] -= $gain 5;     
     
    }
  
elseif(
$_GET['train'] == "will"){

     
$gain $user['stamina'] * 0.2;
     
$user['will'] += $gain ;     
     
$expgain $gain ;
     
$user['exp'] += $expgain ;
     
$user['expneed'] -= $expgain 
     
$user['stamina'] -= $gain 5;     
     
    }

elseif(
$_GET['train'] == "att"){
     require(
"purchase.php") ;
    }
}
if(! isset(
$_GET['train']) ){

//if no input
echo"<form action='' method='get'>
     <select name='train'>
     <option value='weap'>Weapon</option>
     <option value='snip'>Sniper</option>
     <option value='phys'>Physical</option>
     <option value='intel'>Intelligence</option>
     <option value='str'>Strength</option>
     <option value='speed'>Speed</option>
     <option value='will'>Willpower</option>
     <option value='att'>Attacks</option>
     </select>
     <input type='submit' value='Train'/>
     </form>" 
;
    }
return 
$user ;
}


?>
I need the user to choose a type of training then choose which type of the training to do

Last edited by Direct; 07-02-2010 at 11:23 PM..
Direct is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 11:24 PM Re: Need hep with GET variable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by Direct View Post
Yeah but my problem is I need to keep the selection from the first page Like if they choose

type1 that might have different choices on the next form than say type2
I guess I don't understand what you mean then. The example I gave would merge the original GET parameters with the new ones. The example also works with the GET parameters, and not the POST parameters.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 11:33 PM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
Hmm, sorry about this I tend to complicate things for myself so much and it makes it hard on the people who are trying to explain stuff to me let me see if I can make some type of algorithm for what I want

1. User selects which page of my site to go to

2. User selects training

3. for example user selects weapon training

4. the user then selects either offense or defense training

5. Based on selection execute code and update user stats based on selection.
Direct is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 11:50 PM Re: Need hep with GET variable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
If you want to keep the get params from the previous request and modifiy/add to them, then you can use the previous example or create a copy of the $_GET array and change the values of that to create a new parameter string.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-03-2010, 12:08 AM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
The problem I am having is the way you said doesn't effect the url at all. Which is my problem I need it to go from

?id=2
to
?id=2&train=weap

but instead it replaces the ?id=1 in the url with train=weap which messes up my script since I need to keep $_GET['id'] to be same as 2 to require the training page if you look above at my index page
Direct is offline
Reply With Quote
View Public Profile
 
Old 07-03-2010, 02:04 AM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
-cough- I added and || to my index page and it works now. I know its not the same as what i am used to seeing but it works. that's all I can ask for with my skill level
Direct is offline
Reply With Quote
View Public Profile
 
Old 07-03-2010, 06:13 PM Re: Need hep with GET variable
Extreme Talker

Posts: 173
Trades: 0
You could store the values as $_SESSION variable and build up a series of values that can be checked each time the person goes to a new screen

If (isset($_GET['id'])) $_SESSION['id']=$_GET['id'];

Then on the next screen you can check to see what the value of the $_SESSION value it and go from there.

The values are strored until you erase the value.
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 07-04-2010, 04:25 AM Re: Need hep with GET variable
Experienced Talker

Posts: 35
Trades: 0
Yeah I was gonna do that, but the way I have it set up now is if $_GET['id'] or $_GET['train'] isset then require my training script
Direct is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need hep with GET variable
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47434 seconds with 12 queries