SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_GET not filling var

    Hi
    I'm very new to php and can't seem to get this simple bit of code to do what i want.
    Basically I'm trying to pass a value to a var via the url.
    Heres the relevant code

    <?
    $pID="\"default\"";
    if (isset($_GET['pID'])) {
    $pID=$_GET['pID'];
    }

    if ($pID="cDesign"){
    $design="\"design\"";
    $flash="\"flash\"";
    $prog="\"prog\"";
    } else {
    $design="\"design1\"";
    $flash="\"flash1\"";
    $prog="\"prog1\"";
    }
    ?>

    The value is passed thus:
    <div id=<? echo $design ?>>
    <a href="index_test.php?pID=cDesign" class="bigLink">Design</a></div>
    <div id="flash"><a href="index_test.php?pID=2" class="bigLink">Flash</a></div>
    <div id="prog"><a href="index_test.php?pID=3" class="bigLink">Programming</a></div>

    When I test the $_GET is being filled but it's not changing the value of $pID

    all this is on page index_test.php

    Thanks
    Sue

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I cant work out exactly what you want to do.
    but here's a few pointers and a simple example.
    PHP Code:
    <?php
    //when developing look at what is being passed to your script
    print_r$_GET );
    echo 
    '<hr />';

    $pid 0// default value set for a var

    if( isset ($_GET['pID']) ) {
     
    $pid $_GET['pID'];

    }

    echo 
    "Chosen pid is " $pid ' <hr />';

    $vals = array(  // create an array of values
    "default",
    "design",
    "flash",
    "programming",
    );

    echo 
    $vals[$pid] . ' <hr />';  // echo the chosen array key's values

    //otherwise show the links again.
    // return;  // uncomment this line to stop the script and not show links
    ?>

    <p><a href="?pID=1" class="bigLink">Design</a><br />
    <a href="?pID=2" class="bigLink">Flash</a><br />
    <a href="?pID=3" class="bigLink">Programming</a></p>

  3. #3
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups
    Thanks for your reply. To explain a little more. I have 3 divs, these hold links, I want to change the postion of each div depending on which link has been pressed, I want to do this by passing a different id for the div - the div position is then set in the CSS hence passing the value of $design etc into the div id. Each link carries a different value for pID which is used to determine the values for $design, $flash $prog. Thus:

    Code:
    //First div 
    <div id=<? echo $design ?>>
    <a href="index_test.php?pID=cDesign" class="bigLink">Design</a></div>
    //Second div
    <div id=<? echo $flash ?>><a href="index_test.php?pID=2" class="bigLink">Fash</a></div>
    //third div
    <div id=<? echo $prog ?>><a href="index_test.php?pID=3" class="bigLink">Programming</a></div>
    I have set up testing thus:

    Code:
    this is pid: <? echo $pID; ?>
    This is request: <? echo $_GET["pID"]; ?>
    Heres the code that sets the values:

    Code:
    <?
    $pID="\"default\"";
    if (isset($_GET['pID'])) {
    $pID=$_GET['pID'];
    } 
    
    if ($pID="cDesign"){
    $design="\"design\"";
    $flash="\"flash\"";
    $prog="\"prog\"";
    } else {
    $design="\"design1\"";
    $flash="\"flash1\"";
    $prog="\"prog1\"";
    }
    ?>
    As you can see I'm setting a default value of "default" for $pID

    Whats happening is that $pID seems to default to "cDesign" once a link is clicked the value in my test area for $_GET changes but the value of $pID remains as "cDesign"

    Should I change the var name being passed in the url to say u_pID, could that be confusing the script ?

    Thanks again for any help
    Sue

  4. #4
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($pID="cDesign")

    should be

    if ($pID=="cDesign")

  5. #5
    SitePoint Member
    Join Date
    Apr 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thankyou

    Hi Sonya
    Thankyou so much it now works
    Can't believe it was that simple ... well actually I can

    Thanks again
    Sue

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Finally I understand what you are doing with all that double quoting variables ... I have never seen that done before, I can understand why you are doing it though ...

    You want to end up with:
    Code:
    <div id="design">
    So you are doing it like this:
    PHP Code:
    if ($pID="cDesign"){
    $design="\"design\"";

    //First div 
    <div id=<? echo $design ?>>
    Please don't, its very confusing for others trying to help you.

    Try and do it this way instead:
    PHP Code:
    if ($pID="cDesign"){
    $design="design"; // just quote strings once

    //First div 
    <div id="<?php echo $design?>">  
    // leave it to when you output your variable 
    // to decide whether/howto quote it
    Also notice I used the full <?php echo $thing; ?> notation.

    Using the short tag notation (<? ?>) may cause your scripts not to work on some servers, depending on their configuration, and may give away embarrassing information written in your php code.
    Last edited by Cups; Apr 21, 2008 at 06:13. Reason: short tag explanation

  7. #7
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a side note:
    If you have text constant, that does not contain variable, then better use single quotes like so:
    $design='design';
    because then PHP parser does not have to look for variables inside text.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •