0

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

@GroD: be careful how you inject that value into a SQL query. Remember it can take any value, so it needs to be parameterised, escaped or cast (so people can't inject SQL of their choice into your query).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.