Jump to content

$_GET & Echo


spfoonnewb

Recommended Posts

Basically heres what I want:

Its pretty self explanitory, except it obviously wont work, and thats why im here :)

So basically if the query string has dothis=1&n=123 then I want the rest of the script to execute. So what it would do is get the rest of the query string and select only the numbers in it, as there is more to the string. Then I want the numbers to base whats posted, and if theres no numbers, or they dont exist just continue loading the page.

[code]If @_$GET['dothis=1'];
then execute the rest, else do nothing.

--

Get the query string
@_$GET['QUERY STRING'];

(ie. dothis=1&n=23 )

Strip the numbers from it
$str_replace(Get only the digits in the string) = $n;

$n = $stripped;

if $stripped = 22; echo 'blah for 22';

if $stripped = 23 echo 'blah for 23';

else 'Do nothing';
[/code]
Link to comment
Share on other sites

That code is a mess.lol

Why don't you just pass the query string in the number format. If you are useing [code=php:0]$_GET['QUERY STRING'][/code] then you cannot name it $n. If you want to use $n then you need to call it $n before it is passed to this script.

Also, the  correct form for ifs and else's is:

[code=php:0]
if ($something == $something2) {
  //do something
}else{
  //do something else
}
[/code]

Good luck,
Tom
Link to comment
Share on other sites

Maybe I'm being naive, but I have no idea why you are coding that way... I would do it like this:

[code=php:0]
<?php

if ((isset($_GET["dothis"]) && (isset($_GET["n"])) {
    # Bulk of code
} else {
    echo "GET variables not loaded";
}

?>[/code]

I have no idea what your trying to do though, are you trying to make it so that if they enter something that isn't a number, you want to strip the number? Because of SQL injection or something else? Explain a little more and I might be able to help further.
Link to comment
Share on other sites

I wrote that in 3 minutes to make an example :D

I need the script to pull the numbers out of the query string to display extra another page will already be includded by the index.php?do=this[b]&23=Yes[/b]

So if the number is 23 I want to echo the information I put for 23 ...

I can easily do:

$_GET['QUERY STRING'] = $n;

[b][u]I mostly just need a code to get only the numbers from the query string.[/u][/b]

Link to comment
Share on other sites

[quote author=spfoonnewb link=topic=107489.msg431370#msg431370 date=1157839929]
[b][u]I mostly just need a code to get only the numbers from the query string.[/u][/b]
[/quote]

There isn't any method for 'only' getting numbers from a query string since you have no idea what's in the query string to start with.  What I suspect you want to do is abstract information from the query string AND THEN test certain variables.

[code]<?php
// example of receiving thispage.php?this=6&that=wombat&the_other=123

$this = $_GET['this']; // $this is 6
$that = $_GET['that']; // $that is wombat
$the_other = $_GET['the_other']; // $the_other is 123
// NOW you can test the variables as you see fit
...
[/code]
Link to comment
Share on other sites

#1 - you have [b]no idea[/b] what's in the query string.  Anybody could enter a query string in the browser window and send it to your page.

#2 - since php variables [b]cannot[/b] start with a number, your example of &123=the_other is doomed to failure.  Check the php manual for acceptable variable name constructs.
Link to comment
Share on other sites

You could try something like this:
[code]<?php
foreach($_GET as $key => $val)
    if(is_numeric($key)) {
//
//  do the processing for a number
//
      $the_number = $key;
//
//    etc...
//
    }
else {
//
// do the other processing
//
}?>[/code]

If you have control over the query string, I would suggest changing it to make processing easier.

Ken
Link to comment
Share on other sites

Or, if you want to only select the numerals from a query string, for example ?var1=d0qwe2 would be 02... you could try something like

[code=php:0]
$gVar = $_GET['gVar'];
function numbers_only ($var) {
$var2 = explode("", $var2);
foreach($var2 as $k => $v) {
if(is_numeric($v)) {
$output .= $v;
}
}
return $output;
}
[/code]

If you put in that script $var1 = numbers_only($_GET['var1']);  and you called to the script as http://yoursite.com/?var1=adf78adsfadsfasd576  $var1 would be 78576
Link to comment
Share on other sites

If you want a numbers_only()  function that

a ) uses the variable value that was passed to it
b ) doesn't try to use explode illegally by passing an empty delimiter
c ) was tested before posting

try
[code]<?php
function numbers_only ($var) {
    $L = strlen($var);
    $output = '';
    for ($i=0; $i<$L; $i++) {
        if(is_numeric($var{$i})) {
            $output .= $var{$i};
        }
    }
    return $output;
}

?>[/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.