GroD
GroD

Reputation: 104

How to obtain a query string variable in PHP?

How can I get id of page?

I have list of 30 links, they look like:

http://test.com/composition.php?=9

Here is my code:

(index.php)
    <?
    $q = array();

    if (mysqli_num_rows($result) > 0) {

        while ($row = mysqli_fetch_assoc($result)) {
            $q[$row['id']]=$row['header'];

        }
    }
     ?>


 <?
       foreach($q as $href => $text) 
       {
           echo '<a href="http://test.com/composition.php?=' . $href . '">' .'<li>'. $text .'</li>' .'</a>';
       }

     ?>

How can i get $href at page composition.php when i click on link?

I tried $_SESSION[href]=$href; - but it always shows me the last id of all links (=30) and i need that one i have clicked.

I'm sorry for noob question, I'm new to php and don't know hove to solve this problem.

Upvotes: 0

Views: 55

Answers (2)

Waleed Al.Khaldi
Waleed Al.Khaldi

Reputation: 26

change

<a href="http://test.com/composition.php?=' . $href . '">

to

<a href="http://test.com/composition.php?get=' . $href . '">

and on php file to get id

$id = $_GET['get'];

welcome

Upvotes: 1

Jonathan Crowe
Jonathan Crowe

Reputation: 5803

You need to create a key for the $href value so you can access it using the $_GET array:

foreach($q as $href => $text) {
   echo '<a href="http://test.com/composition.php?id=' . $href . '">' .'<li>'. $text .'</li>' .'</a>';
}

then in composition.php:

$href = isset($_GET['id']) ? $_GET['id'] : null;

the query values (values passed into a url following a ?) are key-value pairs accessed using the $_REQUEST superglobal or the $_GET superglobal

Upvotes: 2

Related Questions