SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    links using php generated variables

    I have the code

    try
    {
    $nstart = isset($_GET['n_start']) ? $_GET['n_start'] : 0;
    ......
    $all = $partCat->displayMakeInRange($_GET['make'], $nstart, 5) ;
    }
    ..................................

    ......................
    $n_next = $nstart + 5;

    .....

    if($nstart + 5 < $size_of_make)
    {
    echo '<a href="partphp2.php?make=Rex&amp;n_start=$n_next">Next</a>';
    }


    when i submit it to the function displayMakeInRange(x,x,x) it is checked that the first parameter is a string, and the second and third are integers. But is comes back with the error that $n_next is not an integer. Even though directly after $n_next = $nstart + 5;; i echo it, it gives the corect value. Hope that makes sense. Thanks again guys.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use var_dump() on a variable to see its type, and value.

    you can use typecasting to force a variable to be a certain type, which may change the value

    $var = (integer) $var;

    btw- you never pass the $n_next variable into that function.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    says int(5), which seems correct. Am i not right in passing a variable as a value in the link?

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you said the function gives you an error.

    use var_dump() on the variable that you are passing to the function.

    again, you never pass the $n_next variable into that function.

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually...what am i tal;king about. Start index is 0 so should be 4! But either way its an int so should be fine!

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats what i did. i did var_dump($n_next); said int(5). il go back and have another check over it.

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On the first call of the page

    $nstart = isset($_GET['n_start']) ? $_GET['n_start'] : 0;
    echo $nstart;

    echos 0

    it seems that when i click

    <a href="partphp2.php?make=Rex&amp;n_start=$n_next">Next</a>

    it recalls the page

    $nstart = isset($_GET['n_start']) ? $_GET['n_start'] : 0;
    echo $nstart;

    but echos "$nstart" where i want it to echo the new value of $nstart

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edit-
    ok now you see that it is indeed a string, and not an integer.

    now view the html source of your hyperlinks.
    do you see your problem yet?

    php does not expand variables inside strings delimited by single quotes.
    PHP Code:
    echo '<a href="partphp2.php?make=Rex&amp;n_start=$n_next">Next</a>';

    echo 
    '<a href="partphp2.php?make=Rex&amp;n_start=' $n_next '">Next</a>'

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i do! $nstart is passed to

    $all = $partCat->displayMakeInRange($_GET['make'], $nstart, 5) ;

  10. #10
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    O right, I see the problem. Thankyou. Appreciate your help, sorry it was put across a bit unclear on my half.

  11. #11
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by NewDeveloper06
    i do! $nstart is passed to

    $all = $partCat->displayMakeInRange($_GET['make'], $nstart, 5) ;
    $_GET['make'] and $nstart are passed to the function. but you were using var_dump() on $n_next

  12. #12
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right, i'm a step nearer. It now echos 5, yet when i use var_dump it says string(1) "5"

  13. #13
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the only types of variables that will natively exist in _GET/_POST/_COOKIE are strings and arrays. a numeric string is not type integer.

    http://www.php.net/manual/en/language.types.php

    you can use typecasting like i mentioned in a previous post if your function requires a value that is type integer, although your function may work fine with a string that has a numeric value.
    http://www.php.net/manual/en/languag...e-juggling.php

  14. #14
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    again, thankyou. simply did $n_next = (int)$n_next and it works!!! Made my day! Used to program in java at uni, shouldv remembered typecasting!


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
  •