-4

I need to extruct the last letter after '?p=' in a URL with GET?

Example: http://www.example.com/test.php?p=1

How can I get only those numbers 1

I think this is the right methode to get my URL and put it in a variable:

$numbers = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

If so, how can I get only those numbers in order to put them in a variable?

It seems like an obvious questions but the $_GET["p"] doesn't work??

Here is the code.

if(isset($_GET["p"]))
{
    //var_dump($_GET["p"]); // This var_dump displays the correct number 1 which is correct but when I put it in a variable and use that variable I get an error that says "Notice: Trying to get property of non-object in C:\wamp\www\Store_Locator\test2.php on line 21". 

 $myVariable = $_GET["p"]; // When I write $myVariable = 1; it works!

        echo '<p><strong>ID:</strong> '.$xml->ROW[$myVariable]->FIELD1.'</p>'; // This is line 21
}   

By the way, I am trying to display data from an XML file.

Best Regards,

Matt
  • 74,352
  • 26
  • 153
  • 180
fendert12
  • 11
  • 1
  • 6

2 Answers2

1

Did you try $_GET super global variable

 $_GET['p']
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
1
$myVariable = $_GET["p"];

This is the GET method, try to dig deeper into the questions you're asking, a google search might come in handy

EDIT:

The problem here isn't as described, I will update your question, but your solution is to cast the value you're getting to int. For some reason it's not auto-casting as usual.

on line 18, after getting your variable, do the following:

$i = $_GET["p"]; 
$i = (int)$i;

It will work then. Cheers!

Ralph Melhem
  • 767
  • 5
  • 12
  • I asked this question because when I use $myVariable = $_GET["p"]; in the following code I get an error message: Notice: Trying to get property of non-object in C:\wamp\www\Store_Locator\test2.php on line 23. However, when I use $myVariable = 1; everything works fine! This is my problem. I can't say that$myVariable = 1 because it depends on the $_GET. – fendert12 Oct 20 '15 at 09:50
  • if(isset($_GET["p"])) { //var_dump($_GET["p"]); // This var_dump displays the correct number which is correct but when I put it in a variable I get the error $myVariable = $_GET["p"]; // When I write $myVariable = 1; it works! echo '

    ID: '.$xml->ROW[$myVariable]->FIELD1.'

    '; // ID: 1 } By the way, I am trying to display data from an XML file.
    – fendert12 Oct 20 '15 at 09:56
  • Line 23 is the line where you're getting you're data from the xml i'm assuming? Manually set $myvariable = 215; does it work? The error you're getting means you don't have anything on index 215 – Ralph Melhem Oct 20 '15 at 10:00
  • Yes, when I set $myvariable = 1; It works fine. But when I do $myVariable = $_GET["p"]; It seems like the number changes when I get it with the $_get methode? – fendert12 Oct 20 '15 at 10:04
  • Your example shows you're trying to get the number 215, not 1, try this: http://www.example.com/test.php?p=1 – Ralph Melhem Oct 20 '15 at 10:06
  • I don't know how to write my code here in a normal way?? – fendert12 Oct 20 '15 at 10:07
  • Yes, 215 is a generic example. In my code I have: example.com/test.php?p=1 – fendert12 Oct 20 '15 at 10:09
  • Don't write your code here, edit your question to add stuff. Get your variable and echo it, and see the result: $myvariable = $_GET["p"]; echo $myvariable;die; – Ralph Melhem Oct 20 '15 at 10:12
  • Try to echo your variable and show us the results ? $myvariable = $_GET["p"]; echo $myvariable;die; – Ralph Melhem Oct 20 '15 at 10:19
  • $myvariable = $_GET["p"]; echo $myvariable;die; Displays 1 which is correct. But $myvariable doesn't behave as '1' in the code?? – fendert12 Oct 20 '15 at 10:22
  • Then your problem isn't with getting your variable, it's with fetching from the xml file. on Line 20, execute this: var_dump($xml->ROW[$myVariable]);die; what do you get? – Ralph Melhem Oct 20 '15 at 10:25
  • When I write $myvariable = 1; The variable $myvariable works just fine in the code?? Strange. – fendert12 Oct 20 '15 at 10:26
  • Keep $myvariable = $_GET["p"], and try the var dump i mentioned above. – Ralph Melhem Oct 20 '15 at 10:27
  • var_dump($xml->ROW[$myVariable]);die => null – fendert12 Oct 20 '15 at 10:28
  • is it possible to send you the code by PM or email? – fendert12 Oct 20 '15 at 10:30
  • send it to admin@ralphmelhem.com with the xml you're trying to fetch from – Ralph Melhem Oct 20 '15 at 10:31
  • I made it work and updated my answer. will update your question to make it compatible with the issue. Cheers – Ralph Melhem Oct 20 '15 at 10:56
  • In my answer above, under the EDIT – Ralph Melhem Oct 20 '15 at 11:38