Results 1 to 6 of 6

Thread: Extracting variables from a URL and displaying it

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    150

    Extracting variables from a URL and displaying it

    Hi Folks,

    My project is to track users in a discussion group (which user sends a post and who responds). It is like a forum board please.

    I want to be able to use PHP to extract variables and their values from a url query string like the example below please. It is about people communicating with one another in a discussion group by posting comments on a forum board.Just like yours http://www.vbforums.com/forumdisplay.php?27-PHP but my example is stated below. Am I currently going about it he right way, please?


    Code:
    <!doctype html>
    <html lang="en">
    <head>
    <title>Extracting variables from a URL and displaying it</title>
    </head>
    <body>
    <?php 
    
    <p>The user info are:</p>
    
    $chating = array("http://www.mywebsite.org/students.php?id=2027&name=lara",
                  "http://www.mywebsite.org/students.php,?id=3066&name=mathad",
                  "http://www.mywebsite.org/students.php?id=1224&name=bassa",
                  "http://www.mywebsite.org/students.php?id=4829&name=franco",
    
                  "http://www.mywebsite.org/students.php?topic=election&name=lara",
                  "http://www.mywebsite.org/students.php?topic=transport&name=juan"
                 );
    
    ?>
    </body>
    </html>
    My first question now is how do I use the $_GET["id"]; and $_GET["name"];
    to extract the string variables and display it on a page? Once I am able to do that I can start looking in to other parts of the task.

    Any suggestions will be appreciated please.
    Last edited by menre; Apr 30th, 2015 at 02:04 AM.

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Extracting variables from a URL and displaying it

    Are you users pasting the URLs in a form and you want the script to extract the information from a string (or in the case above an array).

    Or are you talking about how to get ID and name from within the script "students.php"?
    My usual boring signature: Something

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    150

    Re: Extracting variables from a URL and displaying it

    Quote Originally Posted by dclamp View Post
    Are you users pasting the URLs in a form and you want the script to extract the information from a string (or in the case above an array).

    Or are you talking about how to get ID and name from within the script "students.php"?
    Thanks for your quick response. "Are you users pasting the URLs in a form and you want the script to extract the information from a string (or in the case above an array)."

    That is the case. I have a page with a form that displays all the urls. That is why I suggested the idea of using an array. But if that is not the best approach, I will welcome any suggestions please.

    "Or are you talking about how to get ID and name from within the script "students.php"?" Again, I will be happy to be able to get ID and name from within the students.php file.

    I hope I am not asking for too much. It is all about learning and being able to do something please.

    Thanks.

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Extracting variables from a URL and displaying it

    GET & POST are two methods which is used to send data to the server. In GET method, all the data will be appended in the URL.

    For example:
    Code:
    http://www.mywebsite.org/students.php?id=3066&name=mathad
    Here, we are passing two attribute-value pairs. They are:
    Code:
    id = 3066
    name = mathad
    So, when a user visits this URL, that page, in your case it's students.php file, you can access these two values. In PHP, the data passed via GET method will be available by accessing the associative array called $_GET[] and those which are passed by POST method will be available in the associative array named $_POST[]

    So in your case, inside students.php, if you could access those values like:
    Code:
    echo $_GET['id'];
    echo $_GET['name'];
    And thus, when the user access this url: http://www.mywebsite.org/students.ph...66&name=mathad
    the output would be:
    Code:
    3066
    mathad
    Creating a discussion board might need you to use some database or XML or some other source to store the data. Maybe this is school assignment. So what I would suggest is, you should learn some basics of PHP from online tutorials websites like w3schools. Then you could ask your specific doubts here and we all will help you.

    Hope it helps

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    150

    Re: Extracting variables from a URL and displaying it

    Thanks for your response. This is not a school assignment. It is something that I am working on. I want to be able to monitor discussion between forum members. That is, find out which member has posted and who has responded to the post. This can be postings and responses for different topics on the forum board.

    The VB forum is a good example. When someone, posts a message in a thread, we can see it, know who has posted it and who has responded to it. You can see the links below please.
    http://www.vbforums.com/forumdisplay.php?27-PHP
    http://www.vbforums.com/search.php?searchid=7228927

    In my case, have I been able to follow your suggestions so far to extract it from the url.

    "Creating a discussion board might need you to use some database or XML or some other source to store the data." This is pointing me in the right direction. Could you advise more about this, I mean how to do it, please?

    I will really appreciate your help.
    Last edited by menre; May 13th, 2015 at 05:50 PM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    150

    Re: Extracting variables from a URL and displaying it

    I am still on this task.

    I have been able to solve part of my original problem with the code below.

    [code]
    <!doctype html>
    <html lang="en">
    <head>
    <title>Extracting variables from a URL and displaying it</title>
    </head>
    <body>
    <?php

    <p>The user info are:</p>

    $pk = "http://www.mywebsite.org/students.php?course=law&id=2027";

    $result = parse_url($pk);
    parse_str($result['query'], $query);

    echo "The results are:" . "<br />";
    echo "Course: " .$query['course'] ."<br/>";
    echo "Course ID: " .$query['id'] ."<br/>";

    ?>
    </body>
    </html>
    [code]


    When I run that code as test.php, I get the results below.

    The user info are:
    The results are:
    Course: law
    Course ID: 2027

    But my original post shows a situation where I need to extract information from multiple and different urls in one file. That was why I tried using an array for the urls. That solution doesn't work with this my working example above. Can someone point me in the right direction, please? Can I pull those data from an external file instead of using an array? If yes, how?

    Another question I have is, can I get/know the value of an ID through a url string query? For example, I mean from 'http://www.mywebsite.org/students.php?id=1388', finding out what 1388 is.

    I may just be going about it all the wrong way. Somebody mentioned a database or xml file before. So, you advise will really help please.
    Last edited by menre; May 14th, 2015 at 06:07 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width